Skip to content

Commit b3a42e3

Browse files
committed
Fix for getLabelAndValue on null controller
I encountered chartjs#11315 under the following circumstances: 1. Position the cursor over the chart area, such that it causes a tooltip to be shown. 2. Move the cursor out of the chart area, such that the tooltip remains visible. 3. Cause the chart contents to be changed, such that the dataset referenced by the active tooltip element is no longer valid. 4. Move the mouse again. This triggers an `inChartArea = false` event, so it reuses the previous, now invalid, active elements. This fixes chartjs#11315 under the circumstances for which I've reproduced it, but there may be others.
1 parent e92d104 commit b3a42e3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/plugins/plugin.tooltip.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,8 @@ export class Tooltip extends Element {
11821182

11831183
if (!inChartArea) {
11841184
// Let user control the active elements outside chartArea. Eg. using Legend.
1185-
return lastActive;
1185+
// But make sure that active elements are still valid.
1186+
return lastActive.filter(i => this.chart.data.datasets[i.datasetIndex]);
11861187
}
11871188

11881189
// Find Active Elements for tooltips

test/specs/plugin.tooltip.tests.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,48 @@ describe('Plugin.Tooltip', function() {
16671667
});
16681668
});
16691669

1670+
it('should tolerate datasets removed or added on events outside chartArea', async function() {
1671+
const dataset1 = {
1672+
label: 'Dataset 1',
1673+
data: [10, 20, 30],
1674+
};
1675+
const dataset2 = {
1676+
label: 'Dataset 2',
1677+
data: [10, 25, 35],
1678+
};
1679+
const chart = window.acquireChart({
1680+
type: 'line',
1681+
data: {
1682+
datasets: [dataset1, dataset2],
1683+
labels: ['Point 1', 'Point 2', 'Point 3']
1684+
},
1685+
options: {
1686+
plugins: {
1687+
tooltip: {
1688+
mode: 'index',
1689+
intersect: false
1690+
}
1691+
}
1692+
}
1693+
});
1694+
1695+
var meta = chart.getDatasetMeta(0);
1696+
var point = meta.data[1];
1697+
var expectedPoints = [jasmine.objectContaining({datasetIndex: 0, index: 1}), jasmine.objectContaining({datasetIndex: 1, index: 1})];
1698+
1699+
await jasmine.triggerMouseEvent(chart, 'mousemove', point);
1700+
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: chart.chartArea.left - 5, y: point.y});
1701+
1702+
expect(chart.tooltip.getActiveElements()).toEqual(expectedPoints);
1703+
1704+
chart.data.datasets = [dataset1];
1705+
chart.update();
1706+
1707+
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: 2, y: 1});
1708+
1709+
expect(chart.tooltip.getActiveElements()).toEqual([expectedPoints[0]]);
1710+
});
1711+
16701712
describe('events', function() {
16711713
it('should not be called on events not in plugin events array', async function() {
16721714
var chart = window.acquireChart({

0 commit comments

Comments
 (0)