Skip to content

Commit 429d99d

Browse files
authored
Tooltip fixes (getLabelAndValue on null controller, null getParsed) (#11596)
* Fix for getLabelAndValue on null controller I encountered #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 #11315 under the circumstances for which I've reproduced it, but there may be others. * Further fixes for elements added / changed This possibly fixes #11365.
1 parent e92d104 commit 429d99d

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/plugins/plugin.tooltip.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,11 @@ 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 =>
1187+
this.chart.data.datasets[i.datasetIndex] &&
1188+
this.chart.getDatasetMeta(i.datasetIndex).controller.getParsed(i.index) !== undefined
1189+
);
11861190
}
11871191

11881192
// Find Active Elements for tooltips

test/specs/plugin.tooltip.tests.js

+85
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,91 @@ describe('Plugin.Tooltip', function() {
16671667
});
16681668
});
16691669

1670+
it('should tolerate datasets removed 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+
const meta = chart.getDatasetMeta(0);
1696+
const point = meta.data[1];
1697+
const 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+
1712+
it('should tolerate elements removed on events outside chartArea', async function() {
1713+
const dataset1 = {
1714+
label: 'Dataset 1',
1715+
data: [10, 20, 30],
1716+
};
1717+
const dataset2 = {
1718+
label: 'Dataset 2',
1719+
data: [10, 25, 35],
1720+
};
1721+
const chart = window.acquireChart({
1722+
type: 'line',
1723+
data: {
1724+
datasets: [dataset1, dataset2],
1725+
labels: ['Point 1', 'Point 2', 'Point 3']
1726+
},
1727+
options: {
1728+
plugins: {
1729+
tooltip: {
1730+
mode: 'index',
1731+
intersect: false
1732+
}
1733+
}
1734+
}
1735+
});
1736+
1737+
const meta = chart.getDatasetMeta(0);
1738+
const point = meta.data[1];
1739+
const expectedPoints = [jasmine.objectContaining({datasetIndex: 0, index: 1}), jasmine.objectContaining({datasetIndex: 1, index: 1})];
1740+
1741+
await jasmine.triggerMouseEvent(chart, 'mousemove', point);
1742+
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: chart.chartArea.left - 5, y: point.y});
1743+
1744+
expect(chart.tooltip.getActiveElements()).toEqual(expectedPoints);
1745+
1746+
dataset1.data = dataset1.data.slice(0, 1);
1747+
chart.data.datasets = [dataset1];
1748+
chart.update();
1749+
1750+
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: 2, y: 1});
1751+
1752+
expect(chart.tooltip.getActiveElements()).toEqual([]);
1753+
});
1754+
16701755
describe('events', function() {
16711756
it('should not be called on events not in plugin events array', async function() {
16721757
var chart = window.acquireChart({

0 commit comments

Comments
 (0)