]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
CategoryScale: automatically add missing labels (#8053)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 14 Nov 2020 22:00:17 +0000 (00:00 +0200)
committerGitHub <noreply@github.com>
Sat, 14 Nov 2020 22:00:17 +0000 (17:00 -0500)
CategoryScale: automatically add missing labels

src/scales/scale.category.js
test/fixtures/controller.bar/bar-skip-null-object-data.js
test/specs/scale.category.tests.js

index 1c8c933284a3ca9c10a44cc045ddb81522a06f1b..d61d4c5ff07ad963e13f879c235400deca74ee94 100644 (file)
@@ -1,5 +1,14 @@
 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) {
@@ -12,12 +21,8 @@ export default class CategoryScale extends Scale {
 
        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() {
index 3be0fc964bdaaaafb726b9a4bb87542a372c1a7d..8690aec27182eb85a0585f49a89f666f52b166c7 100644 (file)
@@ -2,7 +2,6 @@ module.exports = {
        config: {
                type: 'bar',
                data: {
-                       labels: [0, 1, 3, 4],
                        datasets: [
                                {
                                        data: {0: 5, 1: 20, 2: 1, 3: 10},
index fe9c74c68c8df3e967b53c3f73066ea4f7274931..97183a822146f3c00ce7a0db26f07d979ed695dc 100644 (file)
@@ -90,6 +90,30 @@ describe('Category scale tests', function() {
                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',