You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
6.2 KiB
220 lines
6.2 KiB
3 years ago
|
import { CoreSerie, Options, yAxisOrientation } from '../types';
|
||
|
import { CoreSeries } from './series';
|
||
|
import { CoreOptions } from './options';
|
||
4 years ago
|
|
||
4 years ago
|
import * as d3 from 'd3';
|
||
|
|
||
4 years ago
|
import cloneDeep from 'lodash/cloneDeep';
|
||
4 years ago
|
import min from 'lodash/min';
|
||
|
import minBy from 'lodash/minBy';
|
||
|
import max from 'lodash/max';
|
||
|
import maxBy from 'lodash/maxBy';
|
||
4 years ago
|
import sortBy from 'lodash/sortBy';
|
||
|
import reverse from 'lodash/reverse';
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
const DEFAULT_AXIS_RANGE = [0, 1];
|
||
4 years ago
|
const DEFAULT_TRANSFORM = {
|
||
|
x: 0,
|
||
|
y: 0,
|
||
|
k: 1
|
||
|
}
|
||
|
|
||
4 years ago
|
// TODO: replace all getters with fields. Because getters will be recalculated on each call. Use scales as example.
|
||
4 years ago
|
// TODO: remove duplicates in max/min values.
|
||
4 years ago
|
// TODO: PodState.transform has conflicts with d3.zoom.event.transform. It should be synchronized.
|
||
3 years ago
|
export class PodState<T extends CoreSerie, O extends Options> {
|
||
4 years ago
|
private _xValueRange: [number, number];
|
||
|
private _yValueRange: [number, number];
|
||
4 years ago
|
private _y1ValueRange: [number, number];
|
||
3 years ago
|
private _transform: { x: number, y: number, k: number | string } = cloneDeep(DEFAULT_TRANSFORM);
|
||
4 years ago
|
private _xScale: d3.ScaleLinear<number, number>;
|
||
|
private _yScale: d3.ScaleLinear<number, number>;
|
||
|
private _y1Scale: d3.ScaleLinear<number, number>;
|
||
4 years ago
|
|
||
|
constructor(
|
||
4 years ago
|
protected boxParams: { height: number, width: number },
|
||
3 years ago
|
protected coreSeries: CoreSeries<T>,
|
||
|
protected coreOptions: CoreOptions<O>,
|
||
4 years ago
|
) {
|
||
4 years ago
|
this.setInitialRanges();
|
||
4 years ago
|
this.initScales();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
protected setInitialRanges(): void {
|
||
4 years ago
|
this._xValueRange = [this.getMinValueX(), this.getMaxValueX()];
|
||
|
this._yValueRange = [this.getMinValueY(), this.getMaxValueY()];
|
||
|
this._y1ValueRange = [this.getMinValueY1(), this.getMaxValueY1()];
|
||
4 years ago
|
}
|
||
|
|
||
|
protected initScales(): void {
|
||
|
this.setXScale();
|
||
|
this.setYScale();
|
||
|
this.setY1Scale();
|
||
|
}
|
||
|
|
||
|
protected setYScale(): void {
|
||
|
let domain = this._yValueRange;
|
||
|
domain = sortBy(domain) as [number, number];
|
||
3 years ago
|
if(this.coreOptions.axis.y.invert === true) {
|
||
4 years ago
|
domain = reverse(domain);
|
||
|
}
|
||
3 years ago
|
this._yScale = d3.scaleLinear()
|
||
4 years ago
|
.domain(domain)
|
||
|
.range([this.boxParams.height, 0]); // inversed, because d3 y-axis goes from top to bottom;
|
||
|
}
|
||
|
|
||
|
protected setXScale(): void {
|
||
3 years ago
|
let domain = this._xValueRange;
|
||
|
if(this.coreOptions.axis.x.invert === true) {
|
||
|
domain = reverse(domain);
|
||
|
}
|
||
3 years ago
|
this._xScale = d3.scaleLinear()
|
||
4 years ago
|
.domain(domain)
|
||
|
.range([0, this.boxParams.width]);
|
||
|
}
|
||
|
|
||
|
protected setY1Scale(): void {
|
||
|
let domain = this._y1ValueRange;
|
||
|
domain = sortBy(domain) as [number, number];
|
||
3 years ago
|
if(this.coreOptions.axis.y1.invert === true) {
|
||
4 years ago
|
domain = reverse(domain);
|
||
4 years ago
|
}
|
||
3 years ago
|
this._y1Scale = d3.scaleLinear()
|
||
4 years ago
|
.domain(domain)
|
||
|
.range([this.boxParams.height, 0]); // inversed, because d3 y-axis goes from top to bottom
|
||
|
}
|
||
|
|
||
4 years ago
|
public clearState(): void {
|
||
|
this.setInitialRanges();
|
||
|
this.initScales();
|
||
|
this._transform = { x: 0, y: 0, k: 1 };
|
||
|
}
|
||
|
|
||
4 years ago
|
get yScale(): d3.ScaleLinear<number, number> {
|
||
|
return this._yScale;
|
||
|
}
|
||
|
|
||
|
get xScale(): d3.ScaleLinear<number, number> {
|
||
|
return this._xScale;
|
||
|
}
|
||
|
|
||
|
get y1Scale(): d3.ScaleLinear<number, number> {
|
||
|
return this._y1Scale;
|
||
4 years ago
|
}
|
||
|
|
||
|
get xValueRange(): [number, number] | undefined {
|
||
|
return this._xValueRange;
|
||
|
}
|
||
|
|
||
|
get yValueRange(): [number, number] | undefined {
|
||
|
return this._yValueRange;
|
||
|
}
|
||
|
|
||
|
get y1ValueRange(): [number, number] | undefined {
|
||
|
return this._y1ValueRange;
|
||
|
}
|
||
|
|
||
3 years ago
|
get transform(): { x?: number, y?: number, k?: number | string } {
|
||
4 years ago
|
return this._transform;
|
||
|
}
|
||
|
|
||
|
set xValueRange(range: [number, number]) {
|
||
|
this._xValueRange = range;
|
||
4 years ago
|
this.setXScale();
|
||
4 years ago
|
}
|
||
|
|
||
|
set yValueRange(range: [number, number]) {
|
||
|
this._yValueRange = range;
|
||
4 years ago
|
this.setYScale();
|
||
4 years ago
|
}
|
||
|
|
||
|
set y1ValueRange(range: [number, number]) {
|
||
|
this._y1ValueRange = range;
|
||
4 years ago
|
this.setY1Scale();
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
set transform(transform: { x?: number, y?: number, k?: number | string }) {
|
||
4 years ago
|
this._transform.x = transform.x !== undefined ? transform.x : this._transform.x;
|
||
|
this._transform.y = transform.y !== undefined ? transform.y : this._transform.y;
|
||
|
this._transform.k = transform.k !== undefined ? transform.k : this._transform.k;
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
public getMinValueY(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[0];
|
||
|
}
|
||
3 years ago
|
if(this.coreOptions.axis.y.range !== undefined) {
|
||
|
return min(this.coreOptions.axis.y.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.minValueY;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
public getMaxValueY(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[1];
|
||
|
}
|
||
3 years ago
|
if(this.coreOptions.axis.y.range !== undefined) {
|
||
|
return max(this.coreOptions.axis.y.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.maxValueY;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
public getMinValueX(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[0];
|
||
|
}
|
||
|
|
||
3 years ago
|
if(this.coreOptions.axis.x.range !== undefined) {
|
||
|
return min(this.coreOptions.axis.x.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.minValueX;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
public getMaxValueX(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[1];
|
||
|
}
|
||
3 years ago
|
|
||
|
if(this.coreOptions.axis.x.range !== undefined) {
|
||
|
return max(this.coreOptions.axis.x.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.maxValueX;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
public getMinValueY1(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[0];
|
||
|
}
|
||
3 years ago
|
if(this.coreOptions.axis.y1.range !== undefined) {
|
||
|
return min(this.coreOptions.axis.y1.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.minValueY;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
public getMaxValueY1(): number {
|
||
3 years ago
|
if(!this.coreSeries.isSeriesAvailable) {
|
||
4 years ago
|
return DEFAULT_AXIS_RANGE[1];
|
||
|
}
|
||
3 years ago
|
if(this.coreOptions.axis.y1.range !== undefined) {
|
||
|
return max(this.coreOptions.axis.y1.range);
|
||
4 years ago
|
}
|
||
3 years ago
|
return this.coreSeries.maxValueY;
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
// getters for correct transform
|
||
|
get absXScale(): d3.ScaleLinear<number, number> {
|
||
|
const domain = [0, Math.abs(this.getMaxValueX() - this.getMinValueX())];
|
||
|
return d3.scaleLinear()
|
||
|
.domain(domain)
|
||
|
.range([0, this.boxParams.width]);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get absYScale(): d3.ScaleLinear<number, number> {
|
||
|
const domain = [0, Math.abs(this.getMaxValueY() - this.getMinValueY())];
|
||
|
return d3.scaleLinear()
|
||
|
.domain(domain)
|
||
|
.range([0, this.boxParams.height]);
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|