]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
fixed tooltip labelling on Bar Chart when min is defined
authorJerry Chang <therealjerrycan@gmail.com>
Tue, 15 Nov 2016 04:30:18 +0000 (20:30 -0800)
committerEvert Timberg <evert.timberg+github@gmail.com>
Mon, 28 Nov 2016 23:29:56 +0000 (18:29 -0500)
added helper method to adjust the index

pass in chartConfig rather than access within method, make it easier to
test

added semi-colon at the end of helper method

added test for adjustIndex helper method

fixed lint issues

added integration test for the interaction of trigger an event over the
bar

.

.

moved adjustIndex into element helper

removed method from helper and adjusted method in core.interaction

added test for the element adjustIndex helper

added a skipIndexAdjustment method to handle when to skip the adjustment
along with test cases

fixed lint issues

removed the test for the helper method

src/core/core.element.js
src/core/core.interaction.js
test/core.element.tests.js
test/core.interaction.tests.js

index d8dc57208a40f3c602d0bac9f8ae0432df81168e..778f4119443fb2203e19f5758b49134db43895e8 100644 (file)
@@ -88,6 +88,24 @@ module.exports = function(Chart) {
 
                hasValue: function() {
                        return helpers.isNumber(this._model.x) && helpers.isNumber(this._model.y);
+               },
+
+
+               skipIndexAdjustment: function(config) {
+                       var moreThanOneAxes = config.options.scales.xAxes.length > 1;
+                       var min = config.options.scales.xAxes[0].ticks.min;
+                       return this._adjustedIndex || min === undefined || moreThanOneAxes;
+               },
+
+               adjustIndex: function(config) {
+                       var min = config.options.scales.xAxes[0].ticks.min;
+
+                       if (this.skipIndexAdjustment(config)) {
+                               return;
+                       }
+
+                       this._index -= config.data.labels.indexOf(min);
+                       this._adjustedIndex = true;
                }
        });
 
index aacdda19c3845fc8fe36f3f1a52ccb26e48ab08f..e3bd4bac6be916b9de6da0816211f687afed4153 100644 (file)
@@ -21,6 +21,7 @@ module.exports = function(Chart) {
                        for (j = 0, jlen = meta.data.length; j < jlen; ++j) {
                                var element = meta.data[j];
                                if (!element._view.skip) {
+                                       element.adjustIndex(chart.config);
                                        handler(element);
                                }
                        }
index 7d194562e73bd890bf9daa46653da35344f47f9e..d18685dca3a9b1e994444cfae1df4471876d3bc4 100644 (file)
@@ -42,4 +42,88 @@ describe('Core element tests', function() {
                        colorProp: 'rgb(64, 64, 0)',
                });
        });
+
+       it ('should adjust the index of the element passed in', function() {
+               var chartConfig = {
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               ticks: {
+                                                       min: 'Point 2'
+                                               }
+                                       }]
+                               }
+                       },
+                       data: {
+                               labels: ['Point 1', 'Point 2', 'Point 3']
+                       }
+               };
+
+               var element = new Chart.Element({
+                       _index: 1
+               });
+
+               element.adjustIndex(chartConfig);
+
+               expect(element._adjustedIndex).toEqual(true);
+               expect(element._index).toEqual(0);
+       });
+
+       describe ('skipIndexAdjustment method', function() {
+               var element;
+
+               beforeEach(function() {
+                       element = new Chart.Element({});
+               });
+
+               it ('should return true when min is undefined', function() {
+                       var chartConfig = {
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       ticks: {
+                                                               min: undefined
+                                                       }
+                                               }]
+                                       }
+                               }
+                       };
+                       expect(element.skipIndexAdjustment(chartConfig)).toEqual(true);
+               });
+
+               it ('should return true when index is already adjusted (_adjustedIndex = true)', function() {
+                       var chartConfig = {
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       ticks: {
+                                                               min: 'Point 1'
+                                                       }
+                                               }]
+                                       }
+                               }
+                       };
+                       element._adjustedIndex = true;
+                       expect(element.skipIndexAdjustment(chartConfig)).toEqual(true);
+               });
+
+               it ('should return true when more than one xAxes is defined', function() {
+                       var chartConfig = {
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       ticks: {
+                                                               min: 'Point 1'
+                                                       }
+                                               }, {
+                                                       ticks: {
+                                                               min: 'Point 2'
+                                                       }
+                                               }]
+                                       }
+                               }
+                       };
+                       expect(element.skipIndexAdjustment(chartConfig)).toEqual(true);
+               });
+       });
 });
index 7d8e339caf97d5dc8051fe47ce49e8ed232b78f2..9d704ad342dfb2968016cdeb532ee4f98350c363 100644 (file)
@@ -43,6 +43,57 @@ describe('Core.Interaction', function() {
                        expect(elements).toEqual([point, meta1.data[1]]);
                });
 
+               it ('should start at index 0 within sliced dataset when min is defined', function() {
+                       var chartInstance = window.acquireChart({
+                               type: 'line',
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       ticks: {
+                                                               min: 'March',
+                                                               max: 'May'
+                                                       },
+                                                       categoryPercentage: 1,
+                                                       barPercentage: 1,
+                                               }]
+                                       }
+                               },
+                               data: {
+                                       datasets: [{
+                                               label: 'Dataset 1',
+                                               data: [10, 30, 39, 20, 25, 34, 1],
+                                       }, {
+                                               label: 'Dataset 2',
+                                               data: [10, 30, 39, 20, 25, 34, 1],
+                                       }],
+                                       labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
+                               }
+                       });
+
+                       // Trigger an event over top of the
+                       var meta0 = chartInstance.getDatasetMeta(0);
+                       var point = meta0.data[2];
+
+                       var node = chartInstance.chart.canvas;
+                       var rect = node.getBoundingClientRect();
+
+                       var evt = {
+                               view: window,
+                               bubbles: true,
+                               cancelable: true,
+                               clientX: rect.left + point._model.x,
+                               clientY: rect.top + point._model.y,
+                               currentTarget: node
+                       };
+
+                       var elements = Chart.Interaction.modes.point(chartInstance, evt);
+
+                       elements.forEach(function(element) {
+                               expect(element._index).toEqual(0);
+                               expect(element._adjustedIndex).toBeTruthy();
+                       });
+               });
+
                it ('should return an empty array when no items are found', function() {
                        var chartInstance = window.acquireChart({
                                type: 'line',