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
This commit is contained in:
Jerry Chang
2016-10-30 17:34:06 -07:00
committed by Evert Timberg
parent 48cb8b78e7
commit 4fbb1bdbbc
3 changed files with 7 additions and 3 deletions

2
.gitignore vendored
View File

@@ -8,3 +8,5 @@
.idea
.vscode
bower.json
*.swp

View File

@@ -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

View File

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