import Scale from '../core/core.scale';
+function findOrAddLabel(labels, raw, index) {
+ const first = labels.indexOf(raw);
+ if (first === -1) {
+ return typeof raw === 'string' ? labels.push(raw) - 1 : index;
+ }
+ const last = labels.lastIndexOf(raw);
+ return first !== last ? index : first;
+}
+
export default class CategoryScale extends Scale {
constructor(cfg) {
parse(raw, index) {
const labels = this.getLabels();
- if (labels[index] === raw) {
- return index;
- }
- const first = labels.indexOf(raw);
- const last = labels.lastIndexOf(raw);
- return first === -1 || first !== last ? index : first;
+ return isFinite(index) && labels[index] === raw
+ ? index : findOrAddLabel(labels, raw, index);
}
determineDataLimits() {
expect(getLabels(scale)).toEqual(labels);
});
+ it('Should generate missing labels', function() {
+ var labels = ['a', 'b', 'c', 'd'];
+ var chart = window.acquireChart({
+ type: 'line',
+ data: {
+ datasets: [{
+ data: {a: 1, b: 3, c: -1, d: 10}
+ }]
+ },
+ options: {
+ scales: {
+ x: {
+ type: 'category',
+ labels: ['a']
+ }
+ }
+ }
+ });
+
+ var scale = chart.scales.x;
+ expect(getLabels(scale)).toEqual(labels);
+
+ });
+
it('should get the correct label for the index', function() {
var chart = window.acquireChart({
type: 'line',