From 24c531b90370cd87bc346fce550092d13ae53e35 Mon Sep 17 00:00:00 2001 From: pearmini Date: Sat, 8 May 2021 20:28:38 +0800 Subject: [PATCH] feat(constant): add getTicks --- __tests__/unit/scales/constant.spec.ts | 35 ++++++++++++++++++++++++-- docs/scales/constant.md | 6 +++++ src/scales/constant.ts | 11 ++++++++ src/types.ts | 2 +- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/__tests__/unit/scales/constant.spec.ts b/__tests__/unit/scales/constant.spec.ts index 7e592d6a..0efbcc7a 100644 --- a/__tests__/unit/scales/constant.spec.ts +++ b/__tests__/unit/scales/constant.spec.ts @@ -1,12 +1,15 @@ -import { Constant, ConstantOptions } from '../../../src'; +import { Constant, ConstantOptions, d3Ticks } from '../../../src'; describe('Constant', () => { test('Constant() has expected defaults', () => { const s = new Constant(); - expect(s.getOptions()).toEqual({ + const { tickMethod, ...options } = s.getOptions(); + expect(options).toEqual({ range: [0], domain: [0, 1], + tickCount: 5, }); + expect(tickMethod).toBe(d3Ticks); }); test('Constant(options) override defaults', () => { @@ -73,6 +76,34 @@ describe('Constant', () => { expect(s.getOptions()).toEqual(s1.getOptions()); }); + test('getTicks() returns desired ticks', () => { + const s = new Constant({ + tickMethod: (a, b, n) => { + expect(a).toBe(0); + expect(b).toBe(1); + expect(n).toBe(n); + return [a, b]; + }, + }); + + expect(s.getTicks()).toEqual([0, 1]); + + s.update({ + domain: [1, 'a'], + }); + expect(s.getTicks()).toEqual([]); + + s.update({ + domain: ['a', 1], + }); + expect(s.getTicks()).toEqual([]); + + s.update({ + domain: ['a', 'b'], + }); + expect(s.getTicks()).toEqual([]); + }); + test('clone() returns a scale isolating change with the original one', () => { const s = new Constant(); const s1 = s.clone(); diff --git a/docs/scales/constant.md b/docs/scales/constant.md index c38b6993..8be6809e 100644 --- a/docs/scales/constant.md +++ b/docs/scales/constant.md @@ -30,6 +30,8 @@ x.invert('2'); // [0, 10] | domain | Sets the scale’s domain to the specified array of values. | number[] | string[] | `[0, 1]` | | range | Sets the scale’s range to the specified array of values. | `number[]` | `[0]` | | unknown | Sets the output value of the scale for `undefined` (or `NaN`) input values. | `any` | `undefined` | +| tickCount | Sets approximately count representative values from the scale’s domain. | `number` | `5` | +| tickMethod | Sets the method for computing representative values from the scale’s domain. | `(options?: ConstantOptions) => number[]` | r-pretty| ## Methods @@ -52,3 +54,7 @@ Returns the scale's current options. # **clone**(): Constant Returns a new constant scale with the independent and same options as the original one. + +# **getTicks**(): number[] + +Returns representative values from the scale’s domain computed by specified `options.tickMethod` with `options.tickCount` if options.domain are numbers, otherwise `[]`. diff --git a/src/scales/constant.ts b/src/scales/constant.ts index 0438f546..e4cddb23 100644 --- a/src/scales/constant.ts +++ b/src/scales/constant.ts @@ -1,3 +1,5 @@ +import { isNumber } from '@antv/util'; +import { d3Ticks } from '../tick-methods/d3-ticks'; import { ConstantOptions, Domain, Range } from '../types'; import { Base } from './base'; @@ -11,6 +13,8 @@ export class Constant extends Base { range: [0], domain: [0, 1], unknown: undefined, + tickCount: 5, + tickMethod: d3Ticks, }; } @@ -35,6 +39,13 @@ export class Constant extends Base { return x === v && v !== undefined ? this.options.domain : []; } + public getTicks() { + const { tickMethod, domain, tickCount } = this.options; + const [a, b] = domain; + if (!isNumber(a) || !isNumber(b)) return []; + return tickMethod(a, b, tickCount); + } + /** * 克隆 Constant Scale * @returns 拥有相同选项且独立的 Constant Scale diff --git a/src/types.ts b/src/types.ts index e97c2924..58b00599 100644 --- a/src/types.ts +++ b/src/types.ts @@ -48,7 +48,7 @@ export type Unknown = O['unknown']; export type IdentityOptions = BaseOptions & TickOptions; /** Constant 比例尺的选项 */ -export type ConstantOptions = BaseOptions; +export type ConstantOptions = BaseOptions & TickOptions; /** Continuous 比例尺的选项 */ export type ContinuousOptions = BaseOptions &