Skip to content

Commit 80a159e

Browse files
nagixsimonbrunel
authored andcommitted
Enforce tooltip item label and value to be strings (#6030)
Also update the docs for `xLabel` and `yLabel` to also accept a `number`.
1 parent 97da221 commit 80a159e

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed

docs/configuration/tooltip.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ The tooltip items passed to the tooltip callbacks implement the following interf
170170

171171
// X Value of the tooltip
172172
// (deprecated) use `value` or `label` instead
173-
xLabel: string,
173+
xLabel: number | string,
174174

175175
// Y value of the tooltip
176176
// (deprecated) use `value` or `label` instead
177-
yLabel: string,
177+
yLabel: number | string,
178178

179179
// Index of the dataset the item comes from
180180
datasetIndex: number,

src/core/core.tooltip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ function createTooltipItem(element) {
217217
return {
218218
xLabel: xScale ? xScale.getLabelForIndex(index, datasetIndex) : '',
219219
yLabel: yScale ? yScale.getLabelForIndex(index, datasetIndex) : '',
220-
label: indexScale ? indexScale.getLabelForIndex(index, datasetIndex) : '',
221-
value: valueScale ? valueScale.getLabelForIndex(index, datasetIndex) : '',
220+
label: indexScale ? '' + indexScale.getLabelForIndex(index, datasetIndex) : '',
221+
value: valueScale ? '' + valueScale.getLabelForIndex(index, datasetIndex) : '',
222222
index: index,
223223
datasetIndex: datasetIndex,
224224
x: element._model.x,

test/specs/core.tooltip.tests.js

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -704,64 +704,73 @@ describe('Core.Tooltip', function() {
704704
}));
705705
});
706706

707-
it('Should have dataPoints', function() {
708-
var chart = window.acquireChart({
709-
type: 'line',
710-
data: {
711-
datasets: [{
712-
label: 'Dataset 1',
713-
data: [10, 20, 30],
714-
pointHoverBorderColor: 'rgb(255, 0, 0)',
715-
pointHoverBackgroundColor: 'rgb(0, 255, 0)'
716-
}, {
717-
label: 'Dataset 2',
718-
data: [40, 40, 40],
719-
pointHoverBorderColor: 'rgb(0, 0, 255)',
720-
pointHoverBackgroundColor: 'rgb(0, 255, 255)'
721-
}],
722-
labels: ['Point 1', 'Point 2', 'Point 3']
723-
},
724-
options: {
725-
tooltips: {
726-
mode: 'single'
707+
['line', 'bar', 'horizontalBar'].forEach(function(type) {
708+
it('Should have dataPoints in a ' + type + ' chart', function() {
709+
var chart = window.acquireChart({
710+
type: type,
711+
data: {
712+
datasets: [{
713+
label: 'Dataset 1',
714+
data: [10, 20, 30],
715+
pointHoverBorderColor: 'rgb(255, 0, 0)',
716+
pointHoverBackgroundColor: 'rgb(0, 255, 0)'
717+
}, {
718+
label: 'Dataset 2',
719+
data: [40, 40, 40],
720+
pointHoverBorderColor: 'rgb(0, 0, 255)',
721+
pointHoverBackgroundColor: 'rgb(0, 255, 255)'
722+
}],
723+
labels: ['Point 1', 'Point 2', 'Point 3']
724+
},
725+
options: {
726+
tooltips: {
727+
mode: 'single'
728+
}
727729
}
728-
}
729-
});
730+
});
730731

731-
// Trigger an event over top of the
732-
var pointIndex = 1;
733-
var datasetIndex = 0;
734-
var meta = chart.getDatasetMeta(datasetIndex);
735-
var point = meta.data[pointIndex];
736-
var node = chart.canvas;
737-
var rect = node.getBoundingClientRect();
738-
var evt = new MouseEvent('mousemove', {
739-
view: window,
740-
bubbles: true,
741-
cancelable: true,
742-
clientX: rect.left + point._model.x,
743-
clientY: rect.top + point._model.y
744-
});
732+
// Trigger an event over top of the element
733+
var pointIndex = 1;
734+
var datasetIndex = 0;
735+
var meta = chart.getDatasetMeta(datasetIndex);
736+
var point = meta.data[pointIndex];
737+
var node = chart.canvas;
738+
var rect = node.getBoundingClientRect();
739+
var evt = new MouseEvent('mousemove', {
740+
view: window,
741+
bubbles: true,
742+
cancelable: true,
743+
clientX: Math.round(rect.left + point._model.x),
744+
clientY: Math.round(rect.top + point._model.y)
745+
});
745746

746-
// Manually trigger rather than having an async test
747-
node.dispatchEvent(evt);
747+
// Manually trigger rather than having an async test
748+
node.dispatchEvent(evt);
748749

749-
// Check and see if tooltip was displayed
750-
var tooltip = chart.tooltip;
750+
// Check and see if tooltip was displayed
751+
var tooltip = chart.tooltip;
751752

752-
expect(tooltip._view instanceof Object).toBe(true);
753-
expect(tooltip._view.dataPoints instanceof Array).toBe(true);
754-
expect(tooltip._view.dataPoints.length).toEqual(1);
755-
expect(tooltip._view.dataPoints[0].index).toEqual(pointIndex);
756-
expect(tooltip._view.dataPoints[0].datasetIndex).toEqual(datasetIndex);
757-
expect(tooltip._view.dataPoints[0].xLabel).toEqual(
758-
chart.data.labels[pointIndex]
759-
);
760-
expect(tooltip._view.dataPoints[0].yLabel).toEqual(
761-
chart.data.datasets[datasetIndex].data[pointIndex]
762-
);
763-
expect(tooltip._view.dataPoints[0].x).toBeCloseToPixel(point._model.x);
764-
expect(tooltip._view.dataPoints[0].y).toBeCloseToPixel(point._model.y);
753+
expect(tooltip._view instanceof Object).toBe(true);
754+
expect(tooltip._view.dataPoints instanceof Array).toBe(true);
755+
expect(tooltip._view.dataPoints.length).toBe(1);
756+
757+
var tooltipItem = tooltip._view.dataPoints[0];
758+
759+
expect(tooltipItem.index).toBe(pointIndex);
760+
expect(tooltipItem.datasetIndex).toBe(datasetIndex);
761+
var indexLabel = type !== 'horizontalBar' ? 'xLabel' : 'yLabel';
762+
expect(typeof tooltipItem[indexLabel]).toBe('string');
763+
expect(tooltipItem[indexLabel]).toBe(chart.data.labels[pointIndex]);
764+
var valueLabel = type !== 'horizontalBar' ? 'yLabel' : 'xLabel';
765+
expect(typeof tooltipItem[valueLabel]).toBe('number');
766+
expect(tooltipItem[valueLabel]).toBe(chart.data.datasets[datasetIndex].data[pointIndex]);
767+
expect(typeof tooltipItem.label).toBe('string');
768+
expect(tooltipItem.label).toBe(chart.data.labels[pointIndex]);
769+
expect(typeof tooltipItem.value).toBe('string');
770+
expect(tooltipItem.value).toBe('' + chart.data.datasets[datasetIndex].data[pointIndex]);
771+
expect(tooltipItem.x).toBeCloseToPixel(point._model.x);
772+
expect(tooltipItem.y).toBeCloseToPixel(point._model.y);
773+
});
765774
});
766775

767776
it('Should not update if active element has not changed', function() {

0 commit comments

Comments
 (0)