Skip to content

Commit 42577a0

Browse files
committed
Further fixes for elements added / changed
This possibly fixes chartjs#11365.
1 parent b3a42e3 commit 42577a0

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/plugins/plugin.tooltip.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,10 @@ export class Tooltip extends Element {
11831183
if (!inChartArea) {
11841184
// Let user control the active elements outside chartArea. Eg. using Legend.
11851185
// But make sure that active elements are still valid.
1186-
return lastActive.filter(i => this.chart.data.datasets[i.datasetIndex]);
1186+
return lastActive.filter(i =>
1187+
this.chart.data.datasets[i.datasetIndex] &&
1188+
this.chart.getDatasetMeta(i.datasetIndex).controller.getParsed(i.index) !== undefined
1189+
);
11871190
}
11881191

11891192
// Find Active Elements for tooltips

test/specs/plugin.tooltip.tests.js

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

1670-
it('should tolerate datasets removed or added on events outside chartArea', async function() {
1670+
it('should tolerate datasets removed on events outside chartArea', async function() {
16711671
const dataset1 = {
16721672
label: 'Dataset 1',
16731673
data: [10, 20, 30],
@@ -1692,9 +1692,9 @@ describe('Plugin.Tooltip', function() {
16921692
}
16931693
});
16941694

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})];
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})];
16981698

16991699
await jasmine.triggerMouseEvent(chart, 'mousemove', point);
17001700
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: chart.chartArea.left - 5, y: point.y});
@@ -1709,6 +1709,49 @@ describe('Plugin.Tooltip', function() {
17091709
expect(chart.tooltip.getActiveElements()).toEqual([expectedPoints[0]]);
17101710
});
17111711

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+
17121755
describe('events', function() {
17131756
it('should not be called on events not in plugin events array', async function() {
17141757
var chart = window.acquireChart({

0 commit comments

Comments
 (0)