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

Do not redraw endlessly on mouse move #8898

Merged
merged 2 commits into from Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/plugins/plugin.tooltip.js
@@ -1,7 +1,7 @@
import Animations from '../core/core.animations';
import Element from '../core/core.element';
import {addRoundedRectPath} from '../helpers/helpers.canvas';
import {each, noop, isNullOrUndef, isArray, _elementsEqual} from '../helpers/helpers.core';
import {each, noop, isNullOrUndef, isArray, _elementsEqual, valueOrDefault} from '../helpers/helpers.core';
import {toFont, toPadding, toTRBLCorners} from '../helpers/helpers.options';
import {getRtlAdapter, overrideTextDirection, restoreTextDirection} from '../helpers/helpers.rtl';
import {distanceBetweenPoints, _limitValue} from '../helpers/helpers.math';
Expand All @@ -16,7 +16,7 @@ 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
* @returns {object} tooltip position. false if no position
etimberg marked this conversation as resolved.
Show resolved Hide resolved
*/
average(items) {
if (!items.length) {
Expand Down Expand Up @@ -52,6 +52,10 @@ const positioners = {
* @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;
Expand Down Expand Up @@ -1073,9 +1077,9 @@ export class Tooltip extends Element {
* @returns {boolean} True if the position has changed
kurkle marked this conversation as resolved.
Show resolved Hide resolved
*/
_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 caretX !== valueOrDefault(position.x, caretX) || caretY !== valueOrDefault(position.y, caretY);
etimberg marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
2 changes: 1 addition & 1 deletion types/index.esm.d.ts
Expand Up @@ -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[];
Expand Down