From 2e0398752565771f79007e5313d3a67898933fdd Mon Sep 17 00:00:00 2001 From: vargburz Date: Mon, 1 Nov 2021 18:59:11 +0300 Subject: [PATCH] some updates --- src/index.ts | 29 +++++------------------------ src/state.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index affe6bb..cf11136 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,14 +32,8 @@ import includes from 'lodash/includes'; import first from 'lodash/first'; import last from 'lodash/last'; import mergeWith from 'lodash/mergeWith'; -import min from 'lodash/min'; -import minBy from 'lodash/minBy'; -import max from 'lodash/max'; -import maxBy from 'lodash/maxBy'; import add from 'lodash/add'; import replace from 'lodash/replace'; -import reverse from 'lodash/reverse'; -import sortBy from 'lodash/sortBy'; import cloneDeep from 'lodash/cloneDeep'; import debounce from 'lodash/debounce'; import has from 'lodash/has'; @@ -142,11 +136,6 @@ abstract class ChartwerkPod { // components protected grid: Grid; - // TODO: test variables instead of functions with cache - private _xScale: d3.ScaleLinear | null = null; - private _yScale: d3.ScaleLinear | null = null; - private _y1Scale: d3.ScaleLinear | null = null; - constructor( // maybe it's not the best idea _d3: typeof d3, @@ -180,7 +169,6 @@ abstract class ChartwerkPod { } public render(): void { - console.log('render'); this.renderAxes(); this.renderGrid(); @@ -608,8 +596,7 @@ abstract class ChartwerkPod { this.onPanningRescale(event); - const shouldClearState = false; - this.clearScaleCache(shouldClearState); + // TODO: check clear state for necessity this.renderYAxis(); this.renderXAxis(); @@ -839,6 +826,7 @@ abstract class ChartwerkPod { } } + // TODO: move to State get absXScale(): d3.ScaleLinear { const domain = [0, Math.abs(this.state.maxValueX - this.state.minValueX)]; return this.d3.scaleLinear() @@ -853,6 +841,7 @@ abstract class ChartwerkPod { .range([0, this.height]); } + // TODO: these getters can be removed, but it will break all Pods. get xScale(): d3.ScaleLinear { return this.state.xScale; } @@ -1008,16 +997,8 @@ abstract class ChartwerkPod { return confidenceMetric; } - protected clearScaleCache(shouldClearState = true): void { - this._xScale = null; - this._yScale = null; - this._y1Scale = null; - if(shouldClearState) { - this.state.xValueRange = undefined; - this.state.yValueRange = undefined; - this.state.y1ValueRange = undefined; - this.state.transform = { x: 0, y: 0, k: 1 }; - } + protected clearState(): void { + this.state.clearState(); } protected getSerieColor(idx: number): string { diff --git a/src/state.ts b/src/state.ts index c777193..2c7c447 100755 --- a/src/state.ts +++ b/src/state.ts @@ -21,7 +21,6 @@ const DEFAULT_TRANSFORM = { // TODO: replace all getters with fields. Because getters will be recalculated on each call. Use scales as example. // TODO: remove duplicates in max/min values. -// TODO: add scales // TODO: PodState can be divided in two classes, but it is hard now. export class PodState { private _xValueRange: [number, number]; @@ -38,11 +37,11 @@ export class PodState { protected series: T[], protected options: O, ) { - this.initRanges(); + this.setInitialRanges(); this.initScales(); } - protected initRanges(): void { + protected setInitialRanges(): void { this._xValueRange = [this.minValueX, this.maxValueX]; this._yValueRange = [this.minValueY, this.maxValueY]; this._y1ValueRange = [this.minValueY1, this.maxValueY1]; @@ -83,6 +82,12 @@ export class PodState { .range([this.boxParams.height, 0]); // inversed, because d3 y-axis goes from top to bottom } + public clearState(): void { + this.setInitialRanges(); + this.initScales(); + this._transform = { x: 0, y: 0, k: 1 }; + } + get yScale(): d3.ScaleLinear { return this._yScale; }