The method receives 3 arguments:
-* `value` - the tick value in the **internal data format** of the associated scale.
+* `value` - the tick value in the **internal data format** of the associated scale. For time scale, it is a timestamp.
* `index` - the tick index in the ticks array.
* `ticks` - the array containing all of the [tick objects](../api/interfaces/Tick).
#### Specific changes
* The radialLinear grid indexable and scriptable options don't decrease the index of the specified grid line anymore.
+* Ticks callback on time scale now receives timestamp instead of a formatted label.
#### Type changes
* The order of the `ChartMeta` parameters have been changed from `<Element, DatasetElement, Type>` to `<Type, Element, DatasetElement>`
*/
source: 'auto',
+ callback: false,
+
major: {
enabled: false
}
*/
_tickFormatFunction(time, index, ticks, format) {
const options = this.options;
+ const formatter = options.ticks.callback;
+
+ if (formatter) {
+ return call(formatter, [time, index, ticks], this);
+ }
+
const formats = options.time.displayFormats;
const unit = this._unit;
const majorUnit = this._majorUnit;
const majorFormat = majorUnit && formats[majorUnit];
const tick = ticks[index];
const major = majorUnit && majorFormat && tick && tick.major;
- const label = this._adapter.format(time, format || (major ? majorFormat : minorFormat));
- const formatter = options.ticks.callback;
- return formatter ? call(formatter, [label, index, ticks], this) : label;
+
+ return this._adapter.format(time, format || (major ? majorFormat : minorFormat));
}
/**
},
ticks: {
source: 'auto',
+ callback: false,
major: {
enabled: false
}
}
},
ticks: {
- callback: function(value) {
- return '<' + value + '>';
+ callback: function(_, i) {
+ return '<' + i + '>';
}
}
}
var labels = getLabels(this.scale);
expect(labels.length).toEqual(21);
- expect(labels[0]).toEqual('<8:00:00>');
- expect(labels[labels.length - 1]).toEqual('<8:01:00>');
+ expect(labels[0]).toEqual('<0>');
+ expect(labels[labels.length - 1]).toEqual('<60>');
});
it('should update ticks.callback correctly', function() {
var chart = this.chart;
- chart.options.scales.x.ticks.callback = function(value) {
- return '{' + value + '}';
+ chart.options.scales.x.ticks.callback = function(_, i) {
+ return '{' + i + '}';
};
chart.update();
var labels = getLabels(this.scale);
expect(labels.length).toEqual(21);
- expect(labels[0]).toEqual('{8:00:00}');
- expect(labels[labels.length - 1]).toEqual('{8:01:00}');
+ expect(labels[0]).toEqual('{0}');
+ expect(labels[labels.length - 1]).toEqual('{60}');
});
});
expect(chartOptions).toEqual(chart.options);
});
+
+ it('should pass timestamp to ticks callback', () => {
+ let callbackValue;
+ window.acquireChart({
+ type: 'line',
+ data: {
+ datasets: [{
+ xAxisID: 'x',
+ data: [0, 0]
+ }],
+ labels: ['2015-01-01T20:00:00', '2015-01-01T20:01:00']
+ },
+ options: {
+ scales: {
+ x: {
+ type: 'time',
+ ticks: {
+ callback(value) {
+ callbackValue = value;
+ return value;
+ }
+ }
+ }
+ }
+ }
+ });
+
+ expect(typeof callbackValue).toBe('number');
+ });
});