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.
This commit is contained in:
courchef 2016-09-03 12:06:09 -04:00 committed by Simon Brunel
parent 4cbff0278e
commit 6269e2c437
2 changed files with 40 additions and 2 deletions

View File

@ -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];
}

View File

@ -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();
});
});