mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-06 20:29:12 +02:00
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:
parent
4cbff0278e
commit
6269e2c437
@ -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];
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user