From 6269e2c437a8beccc2516a1635491ab60371950f Mon Sep 17 00:00:00 2001 From: courchef Date: Sat, 3 Sep 2016 12:06:09 -0400 Subject: [PATCH] Error possible when using the plugin Chart.Zoom.js used with time scale (#3194) 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 | 4 ++++ test/scale.time.tests.js | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index c7b273f44..ab935a12a 100755 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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]; } diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js index 9e8a281ad..8a3852a8c 100755 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -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(); }); });