UNPKG

2.24 kBJavaScriptView Raw
1import {Transform, ingest} from 'vega-dataflow';
2import {tickCount, tickFormat, tickValues, validTicks} from 'vega-scale';
3import {inherits} from 'vega-util';
4
5/**
6 * Generates axis ticks for visualizing a spatial scale.
7 * @constructor
8 * @param {object} params - The parameters for this operator.
9 * @param {Scale} params.scale - The scale to generate ticks for.
10 * @param {*} [params.count=10] - The approximate number of ticks, or
11 * desired tick interval, to use.
12 * @param {Array<*>} [params.values] - The exact tick values to use.
13 * These must be legal domain values for the provided scale.
14 * If provided, the count argument is ignored.
15 * @param {function(*):string} [params.formatSpecifier] - A format specifier
16 * to use in conjunction with scale.tickFormat. Legal values are
17 * any valid d3 4.0 format specifier.
18 * @param {function(*):string} [params.format] - The format function to use.
19 * If provided, the formatSpecifier argument is ignored.
20 */
21export default function AxisTicks(params) {
22 Transform.call(this, null, params);
23}
24
25var prototype = inherits(AxisTicks, Transform);
26
27prototype.transform = function(_, pulse) {
28 if (this.value && !_.modified()) {
29 return pulse.StopPropagation;
30 }
31
32 var locale = pulse.dataflow.locale(),
33 out = pulse.fork(pulse.NO_SOURCE | pulse.NO_FIELDS),
34 ticks = this.value,
35 scale = _.scale,
36 tally = _.count == null ? (_.values ? _.values.length : 10) : _.count,
37 count = tickCount(scale, tally, _.minstep),
38 format = _.format || tickFormat(locale, scale, count, _.formatSpecifier, _.formatType, !!_.values),
39 values = _.values ? validTicks(scale, _.values, count) : tickValues(scale, count);
40
41 if (ticks) out.rem = ticks;
42
43 ticks = values.map(function(value, i) {
44 return ingest({
45 index: i / (values.length - 1 || 1),
46 value: value,
47 label: format(value)
48 });
49 });
50
51 if (_.extra && ticks.length) {
52 // add an extra tick pegged to the initial domain value
53 // this is used to generate axes with 'binned' domains
54 ticks.push(ingest({
55 index: -1,
56 extra: {value: ticks[0].value},
57 label: ''
58 }));
59 }
60
61 out.source = ticks;
62 out.add = ticks;
63 this.value = ticks;
64
65 return out;
66};