From deacee7bc6433b8c85c3a337778bfab6adf1eb83 Mon Sep 17 00:00:00 2001 From: Trdat Mkrtchyan Date: Mon, 25 Dec 2023 16:13:58 +0400 Subject: [PATCH] fix: fixed tooltip positioning when synced with different scale --- src/YagrCore/plugins/tooltip/tooltip.ts | 8 ++++++-- src/YagrCore/utils/common.ts | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/YagrCore/plugins/tooltip/tooltip.ts b/src/YagrCore/plugins/tooltip/tooltip.ts index 3d7a0f7..0f0e2e3 100644 --- a/src/YagrCore/plugins/tooltip/tooltip.ts +++ b/src/YagrCore/plugins/tooltip/tooltip.ts @@ -10,7 +10,7 @@ import {DataSeries, ProcessingInterpolation, YagrPlugin} from '../../types'; import {TOOLTIP_Y_OFFSET, TOOLTIP_X_OFFSET, TOOLTIP_DEFAULT_MAX_LINES, DEFAULT_Y_SCALE} from '../../defaults'; -import {findInRange, findDataIdx, findSticky, px, isNil} from '../../utils/common'; +import {findInRange, findDataIdx, findSticky, px, isNil, inBetween} from '../../utils/common'; import { TooltipOptions, TooltipRow, @@ -289,7 +289,8 @@ class YagrTooltip { render = (props: {left: number; top: number; idx: number}) => { const u = this.yagr.uplot; - const {left, top, idx} = props; + let {left, top} = props; + const {idx} = props; const {opts, state} = this; if (opts.show && typeof opts.show === 'function' && opts.show(this.yagr) === false) { @@ -301,6 +302,9 @@ class YagrTooltip { this.hide(); } + top = inBetween(top, 0, u.bbox.top + u.bbox.height); + left = inBetween(left, 0, u.bbox.left + u.bbox.width); + const {data} = u; if (data === null || isNil(idx) || top === undefined) { diff --git a/src/YagrCore/utils/common.ts b/src/YagrCore/utils/common.ts index ee7da3e..04f2986 100644 --- a/src/YagrCore/utils/common.ts +++ b/src/YagrCore/utils/common.ts @@ -464,3 +464,7 @@ export function asPlain(t: T): T extends (...args: any[]) => any ? ReturnType export function isNil(v: unknown): v is null | undefined { return v === null || v === undefined; } + +export function inBetween(value: number, start: number, end: number) { + return value >= start && value <= end ? value : value < start ? start : end; +}