Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooltip fixes (getLabelAndValue on null controller, null getParsed) #11596

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/plugins/plugin.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,11 @@ export class Tooltip extends Element {

if (!inChartArea) {
// Let user control the active elements outside chartArea. Eg. using Legend.
return lastActive;
// But make sure that active elements are still valid.
return lastActive.filter(i =>
this.chart.data.datasets[i.datasetIndex] &&
this.chart.getDatasetMeta(i.datasetIndex).controller.getParsed(i.index) !== undefined
);
}

// Find Active Elements for tooltips
Expand Down
85 changes: 85 additions & 0 deletions test/specs/plugin.tooltip.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,91 @@ describe('Plugin.Tooltip', function() {
});
});

it('should tolerate datasets removed on events outside chartArea', async function() {
const dataset1 = {
label: 'Dataset 1',
data: [10, 20, 30],
};
const dataset2 = {
label: 'Dataset 2',
data: [10, 25, 35],
};
const chart = window.acquireChart({
type: 'line',
data: {
datasets: [dataset1, dataset2],
labels: ['Point 1', 'Point 2', 'Point 3']
},
options: {
plugins: {
tooltip: {
mode: 'index',
intersect: false
}
}
}
});

const meta = chart.getDatasetMeta(0);
const point = meta.data[1];
const expectedPoints = [jasmine.objectContaining({datasetIndex: 0, index: 1}), jasmine.objectContaining({datasetIndex: 1, index: 1})];

await jasmine.triggerMouseEvent(chart, 'mousemove', point);
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: chart.chartArea.left - 5, y: point.y});

expect(chart.tooltip.getActiveElements()).toEqual(expectedPoints);

chart.data.datasets = [dataset1];
chart.update();

await jasmine.triggerMouseEvent(chart, 'mousemove', {x: 2, y: 1});

expect(chart.tooltip.getActiveElements()).toEqual([expectedPoints[0]]);
});

it('should tolerate elements removed on events outside chartArea', async function() {
const dataset1 = {
label: 'Dataset 1',
data: [10, 20, 30],
};
const dataset2 = {
label: 'Dataset 2',
data: [10, 25, 35],
};
const chart = window.acquireChart({
type: 'line',
data: {
datasets: [dataset1, dataset2],
labels: ['Point 1', 'Point 2', 'Point 3']
},
options: {
plugins: {
tooltip: {
mode: 'index',
intersect: false
}
}
}
});

const meta = chart.getDatasetMeta(0);
const point = meta.data[1];
const expectedPoints = [jasmine.objectContaining({datasetIndex: 0, index: 1}), jasmine.objectContaining({datasetIndex: 1, index: 1})];

await jasmine.triggerMouseEvent(chart, 'mousemove', point);
await jasmine.triggerMouseEvent(chart, 'mousemove', {x: chart.chartArea.left - 5, y: point.y});

expect(chart.tooltip.getActiveElements()).toEqual(expectedPoints);

dataset1.data = dataset1.data.slice(0, 1);
chart.data.datasets = [dataset1];
chart.update();

await jasmine.triggerMouseEvent(chart, 'mousemove', {x: 2, y: 1});

expect(chart.tooltip.getActiveElements()).toEqual([]);
});

describe('events', function() {
it('should not be called on events not in plugin events array', async function() {
var chart = window.acquireChart({
Expand Down
Loading