]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Do not redraw endlessly on mouse move (#8898)
authorEvert Timberg <evert.timberg+github@gmail.com>
Thu, 15 Apr 2021 20:36:03 +0000 (16:36 -0400)
committerGitHub <noreply@github.com>
Thu, 15 Apr 2021 20:36:03 +0000 (16:36 -0400)
* Do not redraw endlessly on mouse move

The tooltip incorrectly determined that the position changed leading to many redraws

* Code review feedback

src/plugins/plugin.tooltip.js
types/index.esm.d.ts

index d77c387372a6138349bcac9af5087ca5414ac4c7..b852e7ee47ac68d563e085f64882231eded45418 100644 (file)
@@ -14,9 +14,6 @@ import {drawPoint} from '../helpers';
 const positioners = {
   /**
         * Average mode places the tooltip at the average position of the elements shown
-        * @function Chart.Tooltip.positioners.average
-        * @param items {object[]} the items being displayed in the tooltip
-        * @returns {object} tooltip position
         */
   average(items) {
     if (!items.length) {
@@ -46,12 +43,12 @@ const positioners = {
 
   /**
         * Gets the tooltip position nearest of the item nearest to the event position
-        * @function Chart.Tooltip.positioners.nearest
-        * @param items {object[]} the tooltip items
-        * @param eventPosition {object} the position of the event in canvas coordinates
-        * @returns {object} the tooltip position
         */
   nearest(items, eventPosition) {
+    if (!items.length) {
+      return false;
+    }
+
     let x = eventPosition.x;
     let y = eventPosition.y;
     let minDistance = Number.POSITIVE_INFINITY;
@@ -1073,9 +1070,9 @@ export class Tooltip extends Element {
         * @returns {boolean} True if the position has changed
         */
   _positionChanged(active, e) {
-    const me = this;
-    const position = positioners[me.options.position].call(me, active, e);
-    return me.caretX !== position.x || me.caretY !== position.y;
+    const {caretX, caretY, options} = this;
+    const position = positioners[options.position].call(this, active, e);
+    return position !== false && (caretX !== position.x || caretY !== position.y);
   }
 }
 
index 6d6fa09bf79941b78e974f052b82831b258f642d..b1e347215d45abf87ffd3b134ad9688a60b542ca 100644 (file)
@@ -2331,7 +2331,7 @@ export interface TooltipModel<TType extends ChartType> {
 
 export const Tooltip: Plugin & {
   readonly positioners: {
-    [key: string]: (items: readonly Element[], eventPosition: { x: number; y: number }) => { x: number; y: number };
+    [key: string]: (items: readonly Element[], eventPosition: { x: number; y: number }) => { x: number; y: number } | false;
   };
 
   getActiveElements(): ActiveElement[];