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

Adding hooks "beforeTooltipDraw" and "afterTooltipDraw" to plugins #4793

Merged
merged 9 commits into from Oct 14, 2017
3 changes: 3 additions & 0 deletions src/core/core.controller.js
Expand Up @@ -529,6 +529,9 @@ module.exports = function(Chart) {

me.drawDatasets(easingValue);

// notify before drawing tooltips
plugins.notify(me, 'beforeTooltipDraw', [easingValue]);
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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).


// Finally draw the tooltip
me.tooltip.draw();

Expand Down
10 changes: 10 additions & 0 deletions src/core/core.plugin.js
Expand Up @@ -273,6 +273,16 @@ module.exports = function(Chart) {
* @param {Object} options - The plugin options.
* @returns {Boolean} `false` to cancel the chart drawing.
*/
/**
* @method IPlugin#beforeTooltipDraw
* @desc Called before drawing `tooltips` at every animation frame specified by the given
* easing value. If any plugin returns `false`, the frame drawing is cancelled until
* another `render` is triggered.
* @param {Chart.Controller} chart - The chart instance.
* @param {Number} easingValue - The current animation value, between 0.0 and 1.0.
* @param {Object} options - The plugin options.
* @returns {Boolean} `false` to cancel the chart drawing.
*/
/**
* @method IPlugin#afterDraw
* @desc Called after the `chart` has been drawn for the specific easing value. Note
Expand Down