]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Error possible when using the plugin Chart.Zoom.js used with time scale (#3194)
authorcourchef <courchef@gmail.com>
Sat, 3 Sep 2016 16:06:09 +0000 (12:06 -0400)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 3 Sep 2016 16:06:09 +0000 (18:06 +0200)
Zooming in can cause the params `datasetIndex` and `index` to be null in method `getLabelMoment`. This happens when no ticks are visible on scale. It also throws an error and the chart becomes broken.

src/scales/scale.time.js
test/scale.time.tests.js

index c7b273f44755e91dd7e63dbffd0eb2e34b306bc8..ab935a12a0a0ca1f5157c67bc7a8a8ec6dc3bfe1 100755 (executable)
@@ -76,6 +76,10 @@ module.exports = function(Chart) {
                        Chart.Scale.prototype.initialize.call(this);
                },
                getLabelMoment: function(datasetIndex, index) {
+                       if (datasetIndex === null || index === null) {
+                               return null;
+            }
+            
                        if (typeof this.labelMoments[datasetIndex] != 'undefined') {
                                return this.labelMoments[datasetIndex][index];
                        }
index 9e8a281ad1330263b80a01a5e3954984b52a2734..8a3852a8c05524cedbf542af96989445b4e657d6 100755 (executable)
@@ -504,10 +504,44 @@ describe('Time scale tests', function() {
 
                var xScale = chartInstance.scales.xScale0;
 
-               var getOutOfBoundPixelForValue = function() {
+               var getOutOfBoundLabelMoment = function() {
                        xScale.getLabelMoment(12, 0);
                };
 
-               expect(getOutOfBoundPixelForValue).not.toThrow();
+               expect(getOutOfBoundLabelMoment).not.toThrow();
+       });
+       
+       it("should not throw an error if the datasetIndex or index are null", function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               labels: ["2016-06-26"],
+                               datasets: [{
+                                       type: "line",
+                                       data: [5]
+                               }]
+                       },
+                       options: {
+                               scales: {
+                                       xAxes: [{
+                                               display: true,
+                                               type: "time",
+                                       }]
+                               }
+                       }
+               });
+
+               var xScale = chartInstance.scales.xScale0;
+
+               var getNullDatasetIndexLabelMoment = function() {
+                       xScale.getLabelMoment(null, 1);
+               };
+               
+               var getNullIndexLabelMoment = function() {
+                       xScale.getLabelMoment(1, null);
+               };
+
+               expect(getNullDatasetIndexLabelMoment).not.toThrow();
+               expect(getNullIndexLabelMoment).not.toThrow();
        });
 });