From 3a5b26c23b5f120ad676771e88d49a8aa2c6099b Mon Sep 17 00:00:00 2001 From: Trdat Mkrtchyan Date: Wed, 9 Aug 2023 17:11:00 +0400 Subject: [PATCH] fix: fixed plotlines removes, update bug with cached uHook --- demo/examples/tooltip-with-datarefs.html | 1 + src/YagrCore/index.ts | 2 ++ src/YagrCore/mixins/create-options.ts | 18 ++++++++---------- src/YagrCore/plugins/plotLines/plotLines.ts | 8 +++----- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/demo/examples/tooltip-with-datarefs.html b/demo/examples/tooltip-with-datarefs.html index 8ecf3d7..e300a6f 100644 --- a/demo/examples/tooltip-with-datarefs.html +++ b/demo/examples/tooltip-with-datarefs.html @@ -151,6 +151,7 @@

Tooltip Plugin

const yData = y1.uplot.data[0]; plToRemove = [ { + scale: 'x', value: [data.state.range[0].value, data.state.range[1].value], color: 'rgba(0, 0, 0, 0.5)', id: 'pined', diff --git a/src/YagrCore/index.ts b/src/YagrCore/index.ts index c3c3660..786447c 100644 --- a/src/YagrCore/index.ts +++ b/src/YagrCore/index.ts @@ -88,6 +88,7 @@ class Yagr { /** Create uPlot options methods */ protected createUplotOptions!: CreateUplotOptionsMixin['createUplotOptions']; protected transformSeries!: TransformSeriesMixin['transformSeries']; + protected _uHooks: Record void> = {}; /** Dynamic update methods */ setTheme!: DynamicUpdatesMixin['setTheme']; @@ -230,6 +231,7 @@ class Yagr { this.plugins?.tooltip?.dispose(); this.plugins?.legend?.destroy(); this.uplot.destroy(); + this._uHooks = {}; this.execHooks('dispose', {chart: this}); } diff --git a/src/YagrCore/mixins/create-options.ts b/src/YagrCore/mixins/create-options.ts index dc8d661..f7809af 100644 --- a/src/YagrCore/mixins/create-options.ts +++ b/src/YagrCore/mixins/create-options.ts @@ -21,8 +21,6 @@ import {configureAxes} from '../utils/axes'; import {getPaddingByAxes} from '../utils/chart'; import {DrawOrderKey} from '../utils/types'; -const uHooks: Record void> = {}; - function setIfNotSet(hooks: uPlot.Hooks.Arrays[keyof uPlot.Hooks.Arrays], fn: (u: uPlot) => void) { for (const hook of hooks || []) { if (hook === fn) { @@ -34,7 +32,7 @@ function setIfNotSet(hooks: uPlot.Hooks.Arrays[keyof uPlot.Hooks.Arrays], fn: (u export class CreateUplotOptionsMixin { initMixin(this: Yagr) { - uHooks.onDraw = () => { + this._uHooks.onDraw = () => { if (this.state.stage === 'listen') { return; } @@ -48,7 +46,7 @@ export class CreateUplotOptionsMixin { }); }; - uHooks.ready = () => { + this._uHooks.ready = () => { const initTime = performance.now() - this._startTime; this._meta.initTime = initTime; this.execHooks('inited', { @@ -58,7 +56,7 @@ export class CreateUplotOptionsMixin { }, }); }; - uHooks.drawClear = (u: uPlot) => { + this._uHooks.drawClear = (u: uPlot) => { const {ctx} = u; ctx.save(); ctx.fillStyle = this.utils.theme.BACKGROUND; @@ -70,7 +68,7 @@ export class CreateUplotOptionsMixin { ); ctx.restore(); }; - uHooks.setSelect = (u: uPlot) => { + this._uHooks.setSelect = (u: uPlot) => { const {left, width} = u.select; const [_from, _to] = [u.posToVal(left, DEFAULT_X_SCALE), u.posToVal(left + width, DEFAULT_X_SCALE)]; const {timeMultiplier = 1} = this.config.chart || {}; @@ -212,10 +210,10 @@ export class CreateUplotOptionsMixin { options.hooks.drawClear = options.hooks.drawClear || []; options.hooks.setSelect = options.hooks.setSelect || []; - setIfNotSet(options.hooks.draw, uHooks.onDraw); - setIfNotSet(options.hooks.ready, uHooks.ready); - setIfNotSet(options.hooks.drawClear, uHooks.drawClear); - setIfNotSet(options.hooks.setSelect, uHooks.setSelect); + setIfNotSet(options.hooks.draw, this._uHooks.onDraw); + setIfNotSet(options.hooks.ready, this._uHooks.ready); + setIfNotSet(options.hooks.drawClear, this._uHooks.drawClear); + setIfNotSet(options.hooks.setSelect, this._uHooks.setSelect); options.drawOrder = chart.appearance?.drawOrder ? (chart.appearance?.drawOrder.filter( diff --git a/src/YagrCore/plugins/plotLines/plotLines.ts b/src/YagrCore/plugins/plotLines/plotLines.ts index 4a62104..367fe13 100644 --- a/src/YagrCore/plugins/plotLines/plotLines.ts +++ b/src/YagrCore/plugins/plotLines/plotLines.ts @@ -137,11 +137,9 @@ export default function plotLinesPlugin(yagr: Yagr, plotLinesCfg: PlotLineConfig }) : []; }, - remove: (plotLinesToRemove: PlotLineConfig[], scale?: string) => { + remove: (plotLinesToRemove: PlotLineConfig[]) => { plotLines = plotLines.filter((p) => { - return !plotLinesToRemove.some((pl) => { - return pl.id === p.id && (scale ? p.scale === scale : true); - }); + return !hasPlotLine(plotLinesToRemove, p); }); }, add: (additionalPlotLines: PlotLineConfig[], scale?: string) => { @@ -164,7 +162,7 @@ export default function plotLinesPlugin(yagr: Yagr, plotLinesCfg: PlotLineConfig }); additions.length && plugin.add(additions, scale); - removes.length && plugin.remove(removes, scale); + removes.length && plugin.remove(removes); }, uplot: { hooks: {