From 4db7cdb21be70bd629bb72c63cb05d2c6cfe1d60 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 15 Oct 2017 10:41:49 -0400 Subject: [PATCH 1/2] When update() is called, we need to reset the tooltip. --- src/core/core.controller.js | 5 +++++ src/core/core.tooltip.js | 1 + 2 files changed, 6 insertions(+) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 241448dadd7..10e524680a6 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -371,6 +371,11 @@ module.exports = function(Chart) { me.updateDatasets(); + // Need to reset tooltip in case it is displayed with elements that are removed + // after update. + me.tooltip.initialize(); + me.lastActive = []; + // Do this before render so that any plugins that need final scale updates can use it plugins.notify(me, 'afterUpdate'); diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index 76e06d06141..73460f8d106 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -384,6 +384,7 @@ module.exports = function(Chart) { Chart.Tooltip = Element.extend({ initialize: function() { this._model = getBaseModel(this._options); + this._lastActive = []; }, // Get the title From 98e59d18909b2262043b6143252abafb950ee286 Mon Sep 17 00:00:00 2001 From: etimberg Date: Thu, 26 Oct 2017 21:21:49 -0400 Subject: [PATCH 2/2] Add note about lastActive and add test --- src/core/core.controller.js | 3 ++ test/specs/core.controller.tests.js | 48 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 10e524680a6..9e4984a9af6 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -374,6 +374,9 @@ module.exports = function(Chart) { // Need to reset tooltip in case it is displayed with elements that are removed // after update. me.tooltip.initialize(); + + // Last active contains items that were previously in the tooltip. + // When we reset the tooltip, we need to clear it me.lastActive = []; // Do this before render so that any plugins that need final scale updates can use it diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index fe73d02a0c2..3ec8da50b76 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -822,6 +822,54 @@ describe('Chart', function() { expect(chart.tooltip._options).toEqual(jasmine.objectContaining(newTooltipConfig)); }); + it ('should reset the tooltip on update', function() { + var chart = acquireChart({ + type: 'line', + data: { + labels: ['A', 'B', 'C', 'D'], + datasets: [{ + data: [10, 20, 30, 100] + }] + }, + options: { + responsive: true, + tooltip: { + mode: 'nearest' + } + } + }); + + // Trigger an event over top of a point to + // put an item into the tooltip + var meta = chart.getDatasetMeta(0); + var point = meta.data[1]; + + var node = chart.canvas; + var rect = node.getBoundingClientRect(); + + var evt = new MouseEvent('mousemove', { + view: window, + bubbles: true, + cancelable: true, + clientX: rect.left + point._model.x, + clientY: 0 + }); + + // Manually trigger rather than having an async test + node.dispatchEvent(evt); + + // Check and see if tooltip was displayed + var tooltip = chart.tooltip; + + expect(chart.lastActive).toEqual([point]); + expect(tooltip._lastActive).toEqual([]); + + // Update and confirm tooltip is reset + chart.update(); + expect(chart.lastActive).toEqual([]); + expect(tooltip._lastActive).toEqual([]); + }); + it ('should update the metadata', function() { var cfg = { data: {