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 plotlines removes, update bug with cached uHook #125

Merged
merged 1 commit into from
Aug 9, 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
1 change: 1 addition & 0 deletions demo/examples/tooltip-with-datarefs.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ <h1>Tooltip Plugin</h1>
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',
Expand Down
2 changes: 2 additions & 0 deletions src/YagrCore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Yagr<TConfig extends MinimalValidConfig = MinimalValidConfig> {
/** Create uPlot options methods */
protected createUplotOptions!: CreateUplotOptionsMixin<TConfig>['createUplotOptions'];
protected transformSeries!: TransformSeriesMixin<TConfig>['transformSeries'];
protected _uHooks: Record<string, (u: uPlot) => void> = {};

/** Dynamic update methods */
setTheme!: DynamicUpdatesMixin<TConfig>['setTheme'];
Expand Down Expand Up @@ -230,6 +231,7 @@ class Yagr<TConfig extends MinimalValidConfig = MinimalValidConfig> {
this.plugins?.tooltip?.dispose();
this.plugins?.legend?.destroy();
this.uplot.destroy();
this._uHooks = {};
this.execHooks('dispose', {chart: this});
}

Expand Down
18 changes: 8 additions & 10 deletions src/YagrCore/mixins/create-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {configureAxes} from '../utils/axes';
import {getPaddingByAxes} from '../utils/chart';
import {DrawOrderKey} from '../utils/types';

const uHooks: Record<string, (u: uPlot) => void> = {};

function setIfNotSet(hooks: uPlot.Hooks.Arrays[keyof uPlot.Hooks.Arrays], fn: (u: uPlot) => void) {
for (const hook of hooks || []) {
if (hook === fn) {
Expand All @@ -34,7 +32,7 @@ function setIfNotSet(hooks: uPlot.Hooks.Arrays[keyof uPlot.Hooks.Arrays], fn: (u

export class CreateUplotOptionsMixin<T extends MinimalValidConfig> {
initMixin(this: Yagr) {
uHooks.onDraw = () => {
this._uHooks.onDraw = () => {
if (this.state.stage === 'listen') {
return;
}
Expand All @@ -48,7 +46,7 @@ export class CreateUplotOptionsMixin<T extends MinimalValidConfig> {
});
};

uHooks.ready = () => {
this._uHooks.ready = () => {
const initTime = performance.now() - this._startTime;
this._meta.initTime = initTime;
this.execHooks('inited', {
Expand All @@ -58,7 +56,7 @@ export class CreateUplotOptionsMixin<T extends MinimalValidConfig> {
},
});
};
uHooks.drawClear = (u: uPlot) => {
this._uHooks.drawClear = (u: uPlot) => {
const {ctx} = u;
ctx.save();
ctx.fillStyle = this.utils.theme.BACKGROUND;
Expand All @@ -70,7 +68,7 @@ export class CreateUplotOptionsMixin<T extends MinimalValidConfig> {
);
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 || {};
Expand Down Expand Up @@ -212,10 +210,10 @@ export class CreateUplotOptionsMixin<T extends MinimalValidConfig> {
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(
Expand Down
8 changes: 3 additions & 5 deletions src/YagrCore/plugins/plotLines/plotLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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: {
Expand Down