From: Jerry Chang Date: Mon, 31 Oct 2016 00:34:06 +0000 (-0700) Subject: Fixed Issue with tooltip label display when given null data value (#3528) X-Git-Tag: v2.5.0~1^2~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4fbb1bdbbcfc25addf74cd6da3315c0a5f187011;p=thirdparty%2FChart.js.git Fixed Issue with tooltip label display when given null data value (#3528) When datasets.data contains a null value, the label displays incorrect value. code additions: - unit tests for truthy label values (when data is null) - checks to ensure handling of null value in getLabelByIndex method added mock data sets from issue #3528 example expect the return value from getLabelForIndex method to be valid (truthy) added check for null of first data value in getLabelForIndex fixed indentation and null comparison operator in code fixed mistake in definition of firstData variable changed testing for data on index 0 to using index variable changed firstData to use value instead condense the statments to use value variable --- diff --git a/.gitignore b/.gitignore index 8ef0139ea..172413437 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ .idea .vscode bower.json + +*.swp diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 0057dd92a..61796c6eb 100755 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -360,9 +360,10 @@ module.exports = function(Chart) { getLabelForIndex: function(index, datasetIndex) { var me = this; var label = me.chart.data.labels && index < me.chart.data.labels.length ? me.chart.data.labels[index] : ''; + var value = me.chart.data.datasets[datasetIndex].data[index]; - if (typeof me.chart.data.datasets[datasetIndex].data[0] === 'object') { - label = me.getRightValue(me.chart.data.datasets[datasetIndex].data[index]); + if (value !== null && typeof value === 'object') { + label = me.getRightValue(value); } // Format nicely diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js index 896ff00d4..d3d5d7b7f 100755 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -428,7 +428,7 @@ describe('Time scale tests', function() { datasets: [{ xAxisID: 'xScale0', yAxisID: 'yScale0', - data: [] + data: [null, 10, 3] }], labels: ['2015-01-01T20:00:00', '2015-01-02T21:00:00', '2015-01-03T22:00:00', '2015-01-05T23:00:00', '2015-01-07T03:00', '2015-01-08T10:00', '2015-01-10T12:00'], // days }, @@ -449,6 +449,7 @@ describe('Time scale tests', function() { }); var xScale = chart.scales.xScale0; + expect(xScale.getLabelForIndex(0, 0)).toBeTruthy(); expect(xScale.getLabelForIndex(0, 0)).toBe('2015-01-01T20:00:00'); expect(xScale.getLabelForIndex(6, 0)).toBe('2015-01-10T12:00'); });