-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Update tooltip content and styling on update() #6181
Changes from 3 commits
3357e05
fb8ac49
167af95
7d4fb13
d6f5b0b
f8407a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -488,8 +488,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
// 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 | ||
// Last active contains items that were previously hovered. | ||
me.lastActive = []; | ||
|
||
// Do this before render so that any plugins that need final scale updates can use it | ||
|
@@ -504,6 +503,11 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
} else { | ||
me.render(config); | ||
} | ||
|
||
// Replay last event from before update | ||
if (me._lastEvent) { | ||
me.eventHandler(me._lastEvent); | ||
} | ||
}, | ||
|
||
/** | ||
|
@@ -659,14 +663,26 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
*/ | ||
transition: function(easingValue) { | ||
var me = this; | ||
var options = me.options || {}; | ||
var hoverOptions = options.hover; | ||
var i, ilen; | ||
|
||
for (var i = 0, ilen = (me.data.datasets || []).length; i < ilen; ++i) { | ||
for (i = 0, ilen = (me.data.datasets || []).length; i < ilen; ++i) { | ||
if (me.isDatasetVisible(i)) { | ||
me.getDatasetMeta(i).controller.transition(easingValue); | ||
} | ||
} | ||
|
||
me.tooltip.transition(easingValue); | ||
|
||
if (me._lastEvent && me.animating) { | ||
// If, during animation, element under mouse changes, let's react to that. | ||
me.active = me.getElementsAtEventForMode(me._lastEvent, hoverOptions.mode, hoverOptions); | ||
if (!helpers.arrayEquals(me.active, me.lastActive)) { | ||
me._updateHoverStyles(); | ||
me.lastActive = me.active; | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
|
@@ -918,6 +934,25 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_updateHoverStyles: function() { | ||
var me = this; | ||
var options = me.options || {}; | ||
var hoverOptions = options.hover; | ||
|
||
// Remove styling for last active (even if it may still be active) | ||
if (me.lastActive.length) { | ||
me.updateHoverStyle(me.lastActive, hoverOptions.mode, false); | ||
} | ||
|
||
// Built-in hover styling | ||
if (me.active.length && hoverOptions.mode) { | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
me.updateHoverStyle(me.active, hoverOptions.mode, true); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
|
@@ -985,8 +1020,10 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
// Find Active Elements for hover and tooltips | ||
if (e.type === 'mouseout') { | ||
me.active = []; | ||
me._lastEvent = null; | ||
} else { | ||
me.active = me.getElementsAtEventForMode(e, hoverOptions.mode, hoverOptions); | ||
me._lastEvent = e; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot reproduce that issue in the codepen I have provided with the PR code, I think that issue there was with refiring the actual click event. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this issue when clicking the legend in your codepen. |
||
} | ||
|
||
// Invoke onHover hook | ||
|
@@ -1000,16 +1037,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |
} | ||
} | ||
|
||
// Remove styling for last active (even if it may still be active) | ||
if (me.lastActive.length) { | ||
me.updateHoverStyle(me.lastActive, hoverOptions.mode, false); | ||
} | ||
|
||
// Built in hover styling | ||
if (me.active.length && hoverOptions.mode) { | ||
me.updateHoverStyle(me.active, hoverOptions.mode, true); | ||
} | ||
|
||
me._updateHoverStyles(); | ||
changed = !helpers.arrayEquals(me.active, me.lastActive); | ||
|
||
// Remember Last Actives | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ defaults._set('global', { | |
labelColor: function(tooltipItem, chart) { | ||
var meta = chart.getDatasetMeta(tooltipItem.datasetIndex); | ||
var activeElement = meta.data[tooltipItem.index]; | ||
var view = activeElement._view; | ||
var view = activeElement.$previousStyle || activeElement._view; | ||
return { | ||
borderColor: view.borderColor, | ||
backgroundColor: view.backgroundColor | ||
|
@@ -492,8 +492,25 @@ function getBeforeAfterBodyLines(callback) { | |
|
||
var exports = Element.extend({ | ||
initialize: function() { | ||
this._model = getBaseModel(this._options); | ||
this._lastActive = []; | ||
var me = this; | ||
me._model = getBaseModel(me._options); | ||
me._view = {}; | ||
me._lastActive = []; | ||
}, | ||
|
||
transition: function(easingValue) { | ||
var me = this; | ||
var options = me._options; | ||
|
||
if (me._lastEvent && me._chart.animating) { | ||
etimberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Let's react to changes during animation | ||
me._active = me._chart.getElementsAtEventForMode(me._lastEvent, options.mode, options); | ||
me.update(true); | ||
me.pivot(); | ||
me._lastActive = me.active; | ||
} | ||
|
||
Element.prototype.transition.call(me, easingValue); | ||
}, | ||
|
||
// Get the title | ||
|
@@ -967,8 +984,10 @@ var exports = Element.extend({ | |
// Find Active Elements for tooltips | ||
if (e.type === 'mouseout') { | ||
me._active = []; | ||
me._lastEvent = null; | ||
} else { | ||
me._active = me._chart.getElementsAtEventForMode(e, options.mode, options); | ||
me._lastEvent = e; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
} | ||
|
||
// Remember Last Actives | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply calling
eventHandler
but duplicating code inhandleEvent
? I think other logic such as calling theonHover
callback is also needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is probably right, I replaced these lines now with a call to eventHandler.