]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add "beforeTooltipDraw" and "afterTooltipDraw" plugin hooks (#4793)
authorJewelsJLF <julie.l.farkas@gmail.com>
Sat, 14 Oct 2017 21:29:35 +0000 (15:29 -0600)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 14 Oct 2017 21:29:35 +0000 (23:29 +0200)
src/core/core.controller.js
src/core/core.plugin.js
test/specs/core.controller.tests.js

index 03dfedb7196fcd5eef7b5d60607aac4b071f5c81..241448dadd7366686e5c56dbb01a9f45115a3e46 100644 (file)
@@ -528,9 +528,7 @@ module.exports = function(Chart) {
                        }
 
                        me.drawDatasets(easingValue);
-
-                       // Finally draw the tooltip
-                       me.tooltip.draw();
+                       me._drawTooltip(easingValue);
 
                        plugins.notify(me, 'afterDraw', [easingValue]);
                },
@@ -595,6 +593,28 @@ module.exports = function(Chart) {
                        plugins.notify(me, 'afterDatasetDraw', [args]);
                },
 
+               /**
+                * Draws tooltip unless a plugin returns `false` to the `beforeTooltipDraw`
+                * hook, in which case, plugins will not be called on `afterTooltipDraw`.
+                * @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]);
+               },
+
                // Get the single element that was clicked on
                // @return : An object containing the dataset index and element index of the matching element. Also contains the rectangle that was draw
                getElementAtEvent: function(e) {
index 399075b812cf532c922b741d2eff9f4221f766b0..0b7423a690428cdda80a8dbb0e4165e9e56aef83 100644 (file)
@@ -323,6 +323,27 @@ module.exports = function(Chart) {
         * @param {Number} args.easingValue - The current animation value, between 0.0 and 1.0.
         * @param {Object} options - The plugin options.
         */
+       /**
+        * @method IPlugin#beforeTooltipDraw
+        * @desc Called before drawing the `tooltip`. If any plugin returns `false`,
+        * the tooltip drawing is cancelled until another `render` is triggered.
+        * @param {Chart} chart - The chart instance.
+        * @param {Object} args - The call arguments.
+        * @param {Object} args.tooltip - The tooltip.
+        * @param {Number} args.easingValue - The current animation value, between 0.0 and 1.0.
+        * @param {Object} options - The plugin options.
+        * @returns {Boolean} `false` to cancel the chart tooltip drawing.
+        */
+       /**
+        * @method IPlugin#afterTooltipDraw
+        * @desc Called after drawing the `tooltip`. Note that this hook will not
+        * be called if the tooltip drawing has been previously cancelled.
+        * @param {Chart} chart - The chart instance.
+        * @param {Object} args - The call arguments.
+        * @param {Object} args.tooltip - The tooltip.
+        * @param {Number} args.easingValue - The current animation value, between 0.0 and 1.0.
+        * @param {Object} options - The plugin options.
+        */
        /**
         * @method IPlugin#beforeEvent
         * @desc Called before processing the specified `event`. If any plugin returns `false`,
index 50a206bcfd2ed5ccd6468bd3f94ab7b959141c79..fe73d02a0c23b806e91f7acb0d3aafbac13361c8 100644 (file)
@@ -884,6 +884,8 @@ describe('Chart', function() {
                                        'beforeDatasetDraw',
                                        'afterDatasetDraw',
                                        'afterDatasetsDraw',
+                                       'beforeTooltipDraw',
+                                       'afterTooltipDraw',
                                        'afterDraw',
                                        'afterRender',
                                ],