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

Fix setActiveElements behavior after a mouse event #9992

Merged
merged 2 commits into from Dec 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/core/core.controller.js
Expand Up @@ -504,9 +504,12 @@ class Chart {

this._layers.sort(compare2Level('z', '_idx'));

// Replay last event from before update
if (this._lastEvent) {
this._eventHandler(this._lastEvent, true);
// Replay last event from before update, or set hover styles on active elements
const {_active, _lastEvent} = this;
if (_lastEvent) {
this._eventHandler(_lastEvent, true);
} else if (_active.length) {
this._updateHoverStyles(_active, _active, true);
}

this.render();
Expand Down Expand Up @@ -1086,6 +1089,8 @@ class Chart {

if (changed) {
this._active = active;
// Make sure we don't use the previous mouse event to override the active elements in update.
this._lastEvent = null;
this._updateHoverStyles(active, lastActive);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/plugin.tooltip.js
Expand Up @@ -1004,6 +1004,7 @@ export class Tooltip extends Element {
if (changed || positionChanged) {
this._active = active;
this._eventPosition = eventPosition;
this._ignoreReplayEvents = true;
this.update(true);
}
}
Expand All @@ -1016,6 +1017,11 @@ export class Tooltip extends Element {
* @returns {boolean} true if the tooltip changed
*/
handleEvent(e, replay, inChartArea = true) {
if (replay && this._ignoreReplayEvents) {
return false;
}
this._ignoreReplayEvents = false;

const options = this.options;
const lastActive = this._active || [];
const active = this._getActiveElements(e, lastActive, replay, inChartArea);
Expand Down
36 changes: 36 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -2287,6 +2287,42 @@ describe('Chart', function() {
});
});

it('should not replace the user set active elements by event replay', async function() {
var chart = acquireChart({
type: 'line',
data: {
labels: [1, 2, 3],
datasets: [{
data: [1, 2, 3],
borderColor: 'red',
hoverBorderColor: 'blue',
}]
}
});

const meta = chart.getDatasetMeta(0);
const point0 = meta.data[0];
const point1 = meta.data[1];

let props = meta.data[0].getProps(['borderColor']);
expect(props.options.borderColor).toEqual('red');

await jasmine.triggerMouseEvent(chart, 'mousemove', {x: point0.x, y: point0.y});
expect(chart.getActiveElements()).toEqual([{datasetIndex: 0, index: 0, element: point0}]);
expect(point0.options.borderColor).toEqual('blue');
expect(point1.options.borderColor).toEqual('red');

chart.setActiveElements([{datasetIndex: 0, index: 1}]);
expect(chart.getActiveElements()).toEqual([{datasetIndex: 0, index: 1, element: point1}]);
expect(point0.options.borderColor).toEqual('red');
expect(point1.options.borderColor).toEqual('blue');

chart.update();
expect(chart.getActiveElements()).toEqual([{datasetIndex: 0, index: 1, element: point1}]);
expect(point0.options.borderColor).toEqual('red');
expect(point1.options.borderColor).toEqual('blue');
});

describe('platform', function() {
it('should use the platform constructor provided in config', function() {
const chart = acquireChart({
Expand Down
36 changes: 36 additions & 0 deletions test/specs/plugin.tooltip.tests.js
Expand Up @@ -1556,6 +1556,42 @@ describe('Plugin.Tooltip', function() {
expect(chart.tooltip.getActiveElements()[0].element).toBe(meta.data[0]);
});

it('should not replace the user set active elements by event replay', async function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
label: 'Dataset 1',
data: [10, 20, 30],
pointHoverBorderColor: 'rgb(255, 0, 0)',
pointHoverBackgroundColor: 'rgb(0, 255, 0)'
}],
labels: ['Point 1', 'Point 2', 'Point 3']
},
options: {
events: ['pointerdown', 'pointerup']
}
});

const meta = chart.getDatasetMeta(0);
const point0 = meta.data[0];
const point1 = meta.data[1];

await jasmine.triggerMouseEvent(chart, 'pointerdown', {x: point0.x, y: point0.y});
expect(chart.tooltip.opacity).toBe(1);
expect(chart.tooltip.getActiveElements()).toEqual([{datasetIndex: 0, index: 0, element: point0}]);

chart.tooltip.setActiveElements([{datasetIndex: 0, index: 1}]);
chart.update();
expect(chart.tooltip.opacity).toBe(1);
expect(chart.tooltip.getActiveElements()).toEqual([{datasetIndex: 0, index: 1, element: point1}]);

chart.tooltip.setActiveElements([]);
chart.update();
expect(chart.tooltip.opacity).toBe(0);
expect(chart.tooltip.getActiveElements().length).toBe(0);
});

it('should not change the active elements on events outside chartArea, except for mouseout', async function() {
var chart = acquireChart({
type: 'line',
Expand Down