-
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
Adding hooks "beforeTooltipDraw" and "afterTooltipDraw" to plugins #4793
Conversation
src/core/core.controller.js
Outdated
@@ -529,6 +529,9 @@ module.exports = function(Chart) { | |||
|
|||
me.drawDatasets(easingValue); | |||
|
|||
// notify before drawing tooltips | |||
plugins.notify(me, 'beforeTooltips', [easingValue]); |
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.
Wouldn't beforeTooltipDraw
be more explicit and thus durable (e.g. if we decide to add beforeTooltipUpdate
or whatever related to the tooltip)? Also, since there is only one managed tooltip, not sure it should be plural.
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.
Good point. I'd agree with this change
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.
Sounds good, I'll update it.
…o be more explicit and thus durable (e.g. if we decide to add beforeTooltipUpdate or whatever related to the tooltip)? Also, since there is only one managed tooltip, not sure it should be plural."
src/core/core.controller.js
Outdated
@@ -529,6 +529,9 @@ module.exports = function(Chart) { | |||
|
|||
me.drawDatasets(easingValue); | |||
|
|||
// notify before drawing tooltips | |||
plugins.notify(me, 'beforeTooltipDraw', [easingValue]); |
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.
What about making this new hook consistent with *DatasetDraw
:
- make it cancelable: if the hook return
false
, the tooltip should not be drawn - use the new hook signature that accepts only one argument:
hook(chart, args, options)
We might want to move that code in a new function, similar to drawDataset
:
/**
* @private
*/
_drawTooltip: function(easingValue) {
var me = this;
var tooltip = me.tooltip;
var args = {
tooltip: tooltip ,
easingValue: easingValue
};
if (plugins.notify(me, 'beforeTooltipDraw', [args]) === false) {
return;
}
tooltip .draw();
plugins.notify(me, 'afterTooltipDraw', [args]);
},
Then in draw()
:
// ...
me.drawDatasets(easingValue);
me._drawTooltip(easingValue);
plugins.notify(me, 'afterDraw', [easingValue]);
// ...
Thoughts?
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.
easingValue doesn't seem to be used when drawing the tooltip... any reason to pass it as an arument to the drawTooltip function anyway? Also, any particular reason to call it _drawTooltip instead of just drawTooltip?
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.
easingValue
to be consistent with other *Draw
hooks (it's more or less the animation progress). We can still add it later on demand if you think it's useless right now. _drawTooltip
because it's a private method (there are many other private methods not prefixed because we switched to that convention too late).
…tip" that handles the plugin notifications (beforeTooltipDraw and afterTooltipDraw) and tooltip drawing.
Could you also update the unit tests with these new hooks (I think it's simply add them before |
src/core/core.plugin.js
Outdated
* @param {Chart} chart - The chart instance. | ||
* @param {Object} args - The call arguments. | ||
* @param {Object} args.tooltip - The tooltip. | ||
* @param {Object} options - The plugin options. |
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.
and move options
after args.easingValue
:)
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.
Thanks @JewelsJLF
@etimberg I think this PR is safe enough to be included in 2.7.1, what do you think?
@simonbrunel I agree. Let's merge this :) |
Add a call to plugins.notify before tooltip is drawn. This will allow tooltip to show up on top of a plugin when using "beforeTooltipDraw" inside the plugin.
E.g, Use "beforeTooltipDraw" instead of "afterDraw" in this watermark plugin: https://github.com/JewelsJLF/chartjs-plugin-watermark