Skip to content

Commit

Permalink
Merge 24c531b into 67d6936
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed May 8, 2021
2 parents 67d6936 + 24c531b commit 9698a44
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
35 changes: 33 additions & 2 deletions __tests__/unit/scales/constant.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions docs/scales/constant.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ x.invert('2'); // [0, 10]
| domain | Sets the scale’s domain to the specified array of values. | <code>number[] &#124; string[]</code> | `[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

Expand All @@ -52,3 +54,7 @@ Returns the scale's current options.
<a name="constant_clone" href="#constant_clone">#</a> **clone**<i>(): Constant</i>

Returns a new constant scale with the independent and same options as the original one.

<a name="constant_getTicks" href="#constant_getTicks">#</a> **getTicks**<i>(): number[]</i>

Returns representative values from the scale’s domain computed by specified `options.tickMethod` with `options.tickCount` if options.domain are numbers, otherwise `[]`.
11 changes: 11 additions & 0 deletions src/scales/constant.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -11,6 +13,8 @@ export class Constant extends Base<ConstantOptions> {
range: [0],
domain: [0, 1],
unknown: undefined,
tickCount: 5,
tickMethod: d3Ticks,
};
}

Expand All @@ -35,6 +39,13 @@ export class Constant extends Base<ConstantOptions> {
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
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type Unknown<O extends BaseOptions> = O['unknown'];
export type IdentityOptions = BaseOptions<number> & TickOptions;

/** Constant 比例尺的选项 */
export type ConstantOptions = BaseOptions<number | string>;
export type ConstantOptions = BaseOptions<number | string> & TickOptions;

/** Continuous 比例尺的选项 */
export type ContinuousOptions<D = any, R = D> = BaseOptions<D, R> &
Expand Down

0 comments on commit 9698a44

Please sign in to comment.