import Scale from '../core/core.scale';
-import {isNullOrUndef, valueOrDefault} from '../helpers';
+import {isNullOrUndef, valueOrDefault, _limitValue} from '../helpers';
const addIfString = (labels, raw, index) => typeof raw === 'string'
? labels.push(raw) - 1
return first !== last ? index : first;
}
+const validIndex = (index, max) => index === null ? null : _limitValue(Math.round(index), 0, max);
+
export default class CategoryScale extends Scale {
constructor(cfg) {
return null;
}
const labels = this.getLabels();
- return isFinite(index) && labels[index] === raw
- ? index : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
+ index = isFinite(index) && labels[index] === raw ? index
+ : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
+ return validIndex(index, labels.length - 1);
}
determineDataLimits() {
});
});
-
it('Should generate ticks from the data xLabels', function() {
var labels = ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'];
var chart = window.acquireChart({
});
+ it('should parse only to a valid index', function() {
+ var chart = window.acquireChart({
+ type: 'line',
+ data: {
+ datasets: [{
+ xAxisID: 'x',
+ yAxisID: 'y',
+ data: [10, 5, 0, 25, 78]
+ }],
+ labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
+ },
+ options: {
+ scales: {
+ x: {
+ type: 'category',
+ position: 'bottom'
+ },
+ y: {
+ type: 'linear'
+ }
+ }
+ }
+ });
+
+ var scale = chart.scales.x;
+
+ expect(scale.parse(-10)).toEqual(0);
+ expect(scale.parse(-0.1)).toEqual(0);
+ expect(scale.parse(4.1)).toEqual(4);
+ expect(scale.parse(5)).toEqual(4);
+ expect(scale.parse(1)).toEqual(1);
+ expect(scale.parse(1.4)).toEqual(1);
+ expect(scale.parse(1.5)).toEqual(2);
+ expect(scale.parse('tick2')).toEqual(1);
+ });
+
it('should get the correct label for the index', function() {
var chart = window.acquireChart({
type: 'line',