]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Return correct label for value type axis (#5920)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Thu, 17 Jan 2019 15:04:08 +0000 (17:04 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Thu, 17 Jan 2019 15:04:08 +0000 (16:04 +0100)
src/controllers/controller.bar.js
src/controllers/controller.horizontalBar.js
src/core/core.datasetController.js
src/scales/scale.category.js
test/specs/scale.category.tests.js

index cd6116572755b1a33bf8c13dff85951efda1fd9b..aef53b68afe75d53dd2e3588df0060e3dd3ca698 100644 (file)
@@ -173,7 +173,7 @@ module.exports = DatasetController.extend({
        _updateElementGeometry: function(rectangle, index, reset) {
                var me = this;
                var model = rectangle._model;
-               var vscale = me.getValueScale();
+               var vscale = me._getValueScale();
                var base = vscale.getBasePixel();
                var horizontal = vscale.isHorizontal();
                var ruler = me._ruler || me.getRuler();
@@ -188,34 +188,6 @@ module.exports = DatasetController.extend({
                model.width = horizontal ? undefined : ipixels.size;
        },
 
-       /**
-        * @private
-        */
-       getValueScaleId: function() {
-               return this.getMeta().yAxisID;
-       },
-
-       /**
-        * @private
-        */
-       getIndexScaleId: function() {
-               return this.getMeta().xAxisID;
-       },
-
-       /**
-        * @private
-        */
-       getValueScale: function() {
-               return this.getScaleForId(this.getValueScaleId());
-       },
-
-       /**
-        * @private
-        */
-       getIndexScale: function() {
-               return this.getScaleForId(this.getIndexScaleId());
-       },
-
        /**
         * Returns the stacks based on groups and bar visibility.
         * @param {Number} [last] - The dataset index
@@ -225,7 +197,7 @@ module.exports = DatasetController.extend({
        _getStacks: function(last) {
                var me = this;
                var chart = me.chart;
-               var scale = me.getIndexScale();
+               var scale = me._getIndexScale();
                var stacked = scale.options.stacked;
                var ilen = last === undefined ? chart.data.datasets.length : last + 1;
                var stacks = [];
@@ -275,7 +247,7 @@ module.exports = DatasetController.extend({
         */
        getRuler: function() {
                var me = this;
-               var scale = me.getIndexScale();
+               var scale = me._getIndexScale();
                var stackCount = me.getStackCount();
                var datasetIndex = me.index;
                var isHorizontal = scale.isHorizontal();
@@ -310,7 +282,7 @@ module.exports = DatasetController.extend({
                var me = this;
                var chart = me.chart;
                var meta = me.getMeta();
-               var scale = me.getValueScale();
+               var scale = me._getValueScale();
                var isHorizontal = scale.isHorizontal();
                var datasets = chart.data.datasets;
                var value = +scale.getRightValue(datasets[datasetIndex].data[index]);
@@ -326,7 +298,7 @@ module.exports = DatasetController.extend({
 
                                if (imeta.bar &&
                                        imeta.stack === stack &&
-                                       imeta.controller.getValueScaleId() === scale.id &&
+                                       imeta.controller._getValueScaleId() === scale.id &&
                                        chart.isDatasetVisible(i)) {
 
                                        ivalue = +scale.getRightValue(datasets[i].data[index]);
@@ -385,7 +357,7 @@ module.exports = DatasetController.extend({
        draw: function() {
                var me = this;
                var chart = me.chart;
-               var scale = me.getValueScale();
+               var scale = me._getValueScale();
                var rects = me.getMeta().data;
                var dataset = me.getDataset();
                var ilen = rects.length;
index b761c3444035af3948dd9ed2a2c2e231fd8cadf0..85d31ba515ed77aed7f07448daf3d68dde21e618 100644 (file)
@@ -65,14 +65,14 @@ module.exports = BarController.extend({
        /**
         * @private
         */
-       getValueScaleId: function() {
+       _getValueScaleId: function() {
                return this.getMeta().xAxisID;
        },
 
        /**
         * @private
         */
-       getIndexScaleId: function() {
+       _getIndexScaleId: function() {
                return this.getMeta().yAxisID;
        }
 });
index ab7ca1282c89b2b5d4d2e90427bea934a7095f92..303549849aea7b9d7cd4a72e50ca256e12f0b8ed 100644 (file)
@@ -131,6 +131,34 @@ helpers.extend(DatasetController.prototype, {
                return this.chart.scales[scaleID];
        },
 
+       /**
+        * @private
+        */
+       _getValueScaleId: function() {
+               return this.getMeta().yAxisID;
+       },
+
+       /**
+        * @private
+        */
+       _getIndexScaleId: function() {
+               return this.getMeta().xAxisID;
+       },
+
+       /**
+        * @private
+        */
+       _getValueScale: function() {
+               return this.getScaleForId(this._getValueScaleId());
+       },
+
+       /**
+        * @private
+        */
+       _getIndexScale: function() {
+               return this.getScaleForId(this._getIndexScaleId());
+       },
+
        reset: function() {
                this.update(true);
        },
index 3a4d20645b317ca211d314a167a17023a1c1bac9..d837a7365da6845da2f345a5a5b31d4bb3ecc704 100644 (file)
@@ -49,12 +49,12 @@ module.exports = Scale.extend({
 
        getLabelForIndex: function(index, datasetIndex) {
                var me = this;
-               var data = me.chart.data;
-               var isHorizontal = me.isHorizontal();
+               var chart = me.chart;
 
-               if (data.yLabels && !isHorizontal) {
-                       return me.getRightValue(data.datasets[datasetIndex].data[index]);
+               if (chart.getDatasetMeta(datasetIndex).controller._getValueScaleId() === me.id) {
+                       return me.getRightValue(chart.data.datasets[datasetIndex].data[index]);
                }
+
                return me.ticks[index - me.minIndex];
        },
 
index 7bc6a8a57ad37c92dd85977efd991a89b96d5df0..d6529324ec38d112692d49c9a4644d2c89a7b1a7 100644 (file)
@@ -156,35 +156,38 @@ describe('Category scale tests', function() {
                expect(scale.ticks).toEqual(labels);
        });
 
-       it ('should get the correct label for the index', function() {
-               var scaleID = 'myScale';
-
-               var mockData = {
-                       datasets: [{
-                               yAxisID: scaleID,
-                               data: [10, 5, 0, 25, 78]
-                       }],
-                       labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
-               };
-
-               var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
-               var Constructor = Chart.scaleService.getScaleConstructor('category');
-               var scale = new Constructor({
-                       ctx: {},
-                       options: config,
-                       chart: {
-                               data: mockData
+       it('should get the correct label for the index', function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               datasets: [{
+                                       xAxisID: 'xScale0',
+                                       yAxisID: 'yScale0',
+                                       data: [10, 5, 0, 25, 78]
+                               }],
+                               labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
                        },
-                       id: scaleID
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               id: 'xScale0',
+                                               type: 'category',
+                                               position: 'bottom'
+                                       }],
+                                       yAxes: [{
+                                               id: 'yScale0',
+                                               type: 'linear'
+                                       }]
+                               }
+                       }
                });
 
-               scale.determineDataLimits();
-               scale.buildTicks();
+               var scale = chart.scales.xScale0;
 
-               expect(scale.getLabelForIndex(1)).toBe('tick2');
+               expect(scale.getLabelForIndex(1, 0)).toBe('tick2');
        });
 
-       it ('Should get the correct pixel for a value when horizontal', function() {
+       it('Should get the correct pixel for a value when horizontal', function() {
                var chart = window.acquireChart({
                        type: 'line',
                        data: {
@@ -227,7 +230,7 @@ describe('Category scale tests', function() {
                expect(xScale.getValueForPixel(417)).toBe(4);
        });
 
-       it ('Should get the correct pixel for a value when there are repeated labels', function() {
+       it('Should get the correct pixel for a value when there are repeated labels', function() {
                var chart = window.acquireChart({
                        type: 'line',
                        data: {
@@ -258,7 +261,7 @@ describe('Category scale tests', function() {
                expect(xScale.getPixelForValue('tick_1', 1, 0)).toBeCloseToPixel(143);
        });
 
-       it ('Should get the correct pixel for a value when horizontal and zoomed', function() {
+       it('Should get the correct pixel for a value when horizontal and zoomed', function() {
                var chart = window.acquireChart({
                        type: 'line',
                        data: {
@@ -299,7 +302,7 @@ describe('Category scale tests', function() {
                expect(xScale.getPixelForValue(0, 3, 0)).toBeCloseToPixel(429);
        });
 
-       it ('should get the correct pixel for a value when vertical', function() {
+       it('should get the correct pixel for a value when vertical', function() {
                var chart = window.acquireChart({
                        type: 'line',
                        data: {
@@ -344,7 +347,7 @@ describe('Category scale tests', function() {
                expect(yScale.getValueForPixel(437)).toBe(4);
        });
 
-       it ('should get the correct pixel for a value when vertical and zoomed', function() {
+       it('should get the correct pixel for a value when vertical and zoomed', function() {
                var chart = window.acquireChart({
                        type: 'line',
                        data: {