diff --git a/demo/examples/dynamic-updates.html b/demo/examples/dynamic-updates.html index b18b513..65194bb 100644 --- a/demo/examples/dynamic-updates.html +++ b/demo/examples/dynamic-updates.html @@ -83,6 +83,14 @@

Dynamic updates

+
+
+
+ + +
+
+ diff --git a/src/YagrCore/mixins/batch-updates.ts b/src/YagrCore/mixins/batch-updates.ts index 349809b..accb332 100644 --- a/src/YagrCore/mixins/batch-updates.ts +++ b/src/YagrCore/mixins/batch-updates.ts @@ -92,7 +92,8 @@ export class BatchMixin { this.transformSeries(); }) .inStage('uplot', () => { - this.uplot.destroy(); + // uplot may be undefined if chart is not rendered yet, but got update + this.uplot?.destroy(); this.uplot = new UPlot(this.options, this.series, this.initRender); this.plugins.legend?.redraw(); }) diff --git a/src/YagrCore/mixins/dynamic-updates.ts b/src/YagrCore/mixins/dynamic-updates.ts index dab5d1f..7a0995d 100644 --- a/src/YagrCore/mixins/dynamic-updates.ts +++ b/src/YagrCore/mixins/dynamic-updates.ts @@ -1,11 +1,11 @@ -import type {MinimalValidConfig, RawSerieData, SupportedLocales, YagrConfig, YagrTheme} from '../types'; +import type {MinimalValidConfig, RawSerieData, Scale, SupportedLocales, YagrConfig, YagrTheme} from '../types'; import Yagr from '..'; import i18n from '../locale'; import {DEFAULT_X_SCALE, DEFAULT_Y_SCALE} from '../defaults'; import {overrideSeriesInUpdate, configureSeries} from '../utils/series'; import {getRedrawOptionsForAxesUpdate, updateAxis} from '../utils/axes'; -import {Paths, containsOnly, deepIsEqual, get} from '../utils/common'; +import {Paths, deepIsEqual, get} from '../utils/common'; import {Batch} from './batch-updates'; interface UpdateOptions { @@ -101,33 +101,34 @@ function setVisibleImpl(yagr: Yagr, lineId: string | null, show: boolean, update function setScalesImpl(yagr: Yagr, scales: YagrConfig['scales'], batch: Batch) { let stackingIsChanged = false; - let typeIsChanged = false; let normalizationIsChanged = false; Object.entries(scales).forEach(([scaleName, scaleConfig]) => { const scale = yagr.config.scales[scaleName]; if (scale) { - const {stacking, type} = scale; - const {stacking: newStacking, type: newType} = scaleConfig; + const {stacking} = scale; + const {stacking: newStacking} = scaleConfig; if (stacking !== newStacking) { stackingIsChanged = true; } - if (type !== newType) { - typeIsChanged = true; - } - if (scaleConfig.normalize !== scale.normalize || scaleConfig.normalizeBase !== scale.normalizeBase) { normalizationIsChanged = true; } } }); - const isChangingOnlyMinMax = Object.values(scales).every((scaleConfig) => - containsOnly(scaleConfig as Record, ['min', 'max']), - ); + const isChangingOnlyMinMax = Object.entries(scales).every(([key, scaleConfig]: [string, Scale]) => { + const cfg = yagr.config.scales[key]; + + const {min: pMin, max: pMax, ...pRest} = cfg; + const {min: nMin, max: nMax, ...nRest} = scaleConfig; + const isChangedSomething = deepIsEqual(nRest, pRest) === false; + + return !isChangedSomething && (pMin !== nMin || pMax !== nMax); + }); const isChangingXAxis = Object.keys(scales).includes(DEFAULT_X_SCALE); @@ -150,13 +151,8 @@ function setScalesImpl(yagr: Yagr, scales: YagrConfig['scales'], batch: Batch) { batch.reinit = true; } - if (typeIsChanged) { - batch.reopt = true; - batch.recalc = true; - } - yagr.config.scales = scales; - batch.redraw = [true, true]; + batch.reinit = true; } function isChanged(oldConfig: YagrConfig, newConfig: Partial) { @@ -223,7 +219,7 @@ function setConfigImpl(yagr: Yagr, batch: Batch, newConfig: Partial, yagr.setScales(newConfig.scales); } - const isChangedSeries = areSeriesChanged(yagr.config.series, newConfig.series); + const isChangedSeries = Boolean(newConfig.series) && areSeriesChanged(yagr.config.series, newConfig.series); if (isChangedSeries) { batch.redrawLegend = true; diff --git a/src/YagrCore/utils/common.ts b/src/YagrCore/utils/common.ts index 6662f85..5a0a4db 100644 --- a/src/YagrCore/utils/common.ts +++ b/src/YagrCore/utils/common.ts @@ -399,10 +399,6 @@ export const html = ( return el; }; -export function containsOnly(obj: Record, keys: string[]) { - return Object.keys(obj).every((key) => keys.includes(key)); -} - type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]; type Join = K extends string | number ? P extends string | number