Browse Source

some updates

merge-requests/6/head
vargburz 3 years ago
parent
commit
2e03987525
  1. 29
      src/index.ts
  2. 11
      src/state.ts

29
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<T extends TimeSerie, O extends Options> {
// components
protected grid: Grid;
// TODO: test variables instead of functions with cache
private _xScale: d3.ScaleLinear<number, number> | null = null;
private _yScale: d3.ScaleLinear<number, number> | null = null;
private _y1Scale: d3.ScaleLinear<number, number> | null = null;
constructor(
// maybe it's not the best idea
_d3: typeof d3,
@ -180,7 +169,6 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
}
public render(): void {
console.log('render');
this.renderAxes();
this.renderGrid();
@ -608,8 +596,7 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
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<T extends TimeSerie, O extends Options> {
}
}
// TODO: move to State
get absXScale(): d3.ScaleLinear<number, number> {
const domain = [0, Math.abs(this.state.maxValueX - this.state.minValueX)];
return this.d3.scaleLinear()
@ -853,6 +841,7 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
.range([0, this.height]);
}
// TODO: these getters can be removed, but it will break all Pods.
get xScale(): d3.ScaleLinear<number, number> {
return this.state.xScale;
}
@ -1008,16 +997,8 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
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 {

11
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<T extends TimeSerie, O extends Options> {
private _xValueRange: [number, number];
@ -38,11 +37,11 @@ export class PodState<T extends TimeSerie, O extends Options> {
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<T extends TimeSerie, O extends Options> {
.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<number, number> {
return this._yScale;
}

Loading…
Cancel
Save