A special case of linear scales which mapping input to a fixed output. Each range value y can be expressed as a function of the domain value x: y = b
. (online demo)
- Basic usage
import { Constant, ConstantOptions } from '@antv/scale';
const options: ConstantOptions = {
domain: [0, 10],
range: [5],
unknown: 'dirty',
};
const x = new Constant(options);
x.map(1); // 5
x.map('1'); // 5
x.map(undefined); // 'dirty'
x.invert(2); // [0, 10]
x.invert('2'); // [0, 10]
- Customize tickMethod
import { Constant, ConstantOptions, rPretty, wilkinsonExtended } from '@antv/scale';
const options: ConstantOptions = {
domain: [2, 17],
tickCount: 6,
};
const x0 = new Constant(options); // default tickMethod is d3Ticks
const x1 = new Constant({
tickMethod: rPretty
});
const x2 = new Constant({
tickMethod: wilkinsonExtended
});
const x3 = new Constant({
tickMethod: (min: number, max: number, count: number) => {
const step = (max - min) / count;
return new Array(count).fill(0).map((_, i) => min + i * step);
}
});
x0.getTicks(); // [2, 4, 6, 8, 10, 12, 14, 16]
x1.getTicks(); // [2, 4, 6, 8, 10, 12, 14, 16, 18]
x2.getTicks(); // [0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5]
x3.getTicks(); // [2, 4.5, 7, 9.5, 12, 14.5]
Key | Description | Type | Default |
---|---|---|---|
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[] | string[] |
[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. The specified tickCount in options is only a hint: the scale may return more or fewer values depending on the domain. |
number |
5 |
tickMethod | Sets the method for computing representative values from the scale’s domain. | (options?: ConstantOptions) => number[] |
d3-ticks |
# map(x: number | string): number | any
Returns the first element of range if it is not undefined
(or NaN
), otherwise options.unknown
.
# invert(x: number): (number | string)[]
Returns the domain.
# update(options: ConstantOptions): void
Updates the scale's options and rescale.
# getOptions(): ConstantOptions
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 []
.