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

fix: fixed setScales method #119

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions demo/examples/dynamic-updates.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ <h1>Dynamic updates</h1>
</div>
</div>

<div class="grid">
<div class="container">
<div id="chart11"></div>
<button id="changeScales">Change scales</button>
<button id="changeScalesMinMax">Change scales min/max</button>
</div>
</div>

<script>
function randomColor(a = 1) {
const r = Math.floor(Math.random() * 255);
Expand Down Expand Up @@ -491,6 +499,37 @@ <h1>Dynamic updates</h1>
],
});
};


const yagr11 = new Yagr(chart11, {
title: {text: 'Change scales'},
timeline: [1, 2, 3, 4],
series: [
{
data: [1, 2, 3, 4],
color: 'green',
},
],
scales: {
y: {min: 0, max: 10}
}
});

window.changeScales.onclick = () => {
yagr11.setConfig({
scales: {
y: {min: 0, max: 5, range: () => ([0, 5])}
}
});
};

window.changeScalesMinMax.onclick = () => {
yagr11.setConfig({
scales: {
y: {min: -10, max: 20, range: () => ([0, 5])}
}
});
};
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion src/YagrCore/mixins/batch-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export class BatchMixin<T extends MinimalValidConfig> {
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();
})
Expand Down
34 changes: 15 additions & 19 deletions src/YagrCore/mixins/dynamic-updates.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<string, unknown>, ['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);

Expand All @@ -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<YagrConfig>) {
Expand Down Expand Up @@ -223,7 +219,7 @@ function setConfigImpl(yagr: Yagr, batch: Batch, newConfig: Partial<YagrConfig>,
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;
Expand Down
4 changes: 0 additions & 4 deletions src/YagrCore/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,6 @@ export const html = (
return el;
};

export function containsOnly(obj: Record<string, unknown>, 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, P> = K extends string | number
? P extends string | number
Expand Down