|
|
|
@ -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'; |
|
|
|
@ -119,7 +113,7 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
protected brush?: d3.BrushBehavior<unknown>; |
|
|
|
|
protected zoom?: any; |
|
|
|
|
protected svg?: d3.Selection<SVGElement, unknown, null, undefined>; |
|
|
|
|
protected state?: PodState; |
|
|
|
|
protected state?: PodState<T, O>; |
|
|
|
|
protected clipPath?: any; |
|
|
|
|
protected isPanning = false; |
|
|
|
|
protected isBrushing = false; |
|
|
|
@ -141,11 +135,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, |
|
|
|
@ -162,13 +151,11 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
this.series = cloneDeep(_series); |
|
|
|
|
this.d3 = _d3; |
|
|
|
|
|
|
|
|
|
// TODO: mb move it to render();
|
|
|
|
|
this.initPodState(); |
|
|
|
|
|
|
|
|
|
this.d3Node = this.d3.select(this.el); |
|
|
|
|
this.addEventListeners(); |
|
|
|
|
|
|
|
|
|
this.createSvg(); |
|
|
|
|
this.initPodState(); |
|
|
|
|
this.initComponents(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -181,8 +168,6 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public render(): void { |
|
|
|
|
this.clearScaleCache(); |
|
|
|
|
|
|
|
|
|
this.renderAxes(); |
|
|
|
|
this.renderGrid(); |
|
|
|
|
|
|
|
|
@ -235,16 +220,21 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
public abstract hideSharedCrosshair(): void; |
|
|
|
|
|
|
|
|
|
protected initPodState(): void { |
|
|
|
|
this.state = new PodState(this.options); |
|
|
|
|
const boxPararms = { |
|
|
|
|
height: this.height, |
|
|
|
|
width: this.width, |
|
|
|
|
} |
|
|
|
|
this.state = new PodState(this.d3, boxPararms, this.series, this.options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected initComponents(): void { |
|
|
|
|
// TODO: make chartContainer a separate class with SvgElParams inside to avoid duplication
|
|
|
|
|
// TODO: bad connection between State and Grid
|
|
|
|
|
const svgElParams = { |
|
|
|
|
height: this.height, |
|
|
|
|
width: this.width, |
|
|
|
|
xScale: this.xScale, |
|
|
|
|
yScale: this.yScale, |
|
|
|
|
xScale: this.state.xScale, |
|
|
|
|
yScale: this.state.yScale, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.grid = new Grid(this.d3, this.chartContainer, svgElParams, this.options.grid); |
|
|
|
@ -605,8 +595,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(); |
|
|
|
|
|
|
|
|
@ -673,9 +662,6 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
const signX = Math.sign(event.transform.x); |
|
|
|
|
const transformX = this.absXScale.invert(Math.abs(transformStep)); |
|
|
|
|
let rangeX = this.state.xValueRange; |
|
|
|
|
if(this.state.xValueRange === undefined) { |
|
|
|
|
rangeX = [this.maxValueX, this.minValueX]; |
|
|
|
|
} |
|
|
|
|
this.state.xValueRange = [rangeX[0] + signX * transformX, rangeX[1] + signX * transformX]; |
|
|
|
|
const translateX = this.state.transform.x + signX * transformStep; |
|
|
|
|
this.state.transform = { x: translateX }; |
|
|
|
@ -687,7 +673,7 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
if(this.options.axis.y.invert === true) { |
|
|
|
|
signY = -signY; |
|
|
|
|
} |
|
|
|
|
let rangeY = this.state.yValueRange || [this.maxValue, this.minValue]; |
|
|
|
|
let rangeY = this.state.yValueRange; |
|
|
|
|
const transformY = this.absYScale.invert(deltaY); |
|
|
|
|
this.deltaYTransform = this.deltaYTransform + deltaY; |
|
|
|
|
// TODO: not hardcoded bounds
|
|
|
|
@ -839,173 +825,31 @@ 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.maxValueX - this.minValueX)]; |
|
|
|
|
const domain = [0, Math.abs(this.state.getMaxValueX() - this.state.getMinValueX())]; |
|
|
|
|
return this.d3.scaleLinear() |
|
|
|
|
.domain(domain) |
|
|
|
|
.range([0, this.width]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get absYScale(): d3.ScaleLinear<number, number> { |
|
|
|
|
const domain = [0, Math.abs(this.maxValue - this.minValue)]; |
|
|
|
|
const domain = [0, Math.abs(this.state.getMaxValueY() - this.state.getMinValueY())]; |
|
|
|
|
return this.d3.scaleLinear() |
|
|
|
|
.domain(domain) |
|
|
|
|
.range([0, this.height]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get xScale(): d3.ScaleLinear<number, number> { |
|
|
|
|
if(this._xScale === null) { |
|
|
|
|
const domain = this.state.xValueRange || [this.minValueX, this.maxValueX]; |
|
|
|
|
this._xScale = this.d3.scaleLinear() |
|
|
|
|
.domain(domain) |
|
|
|
|
.range([0, this.width]); |
|
|
|
|
} |
|
|
|
|
return this._xScale; |
|
|
|
|
return this.state.xScale; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get yScale(): d3.ScaleLinear<number, number> { |
|
|
|
|
if(this._yScale === null) { |
|
|
|
|
let domain = this.state.yValueRange || [this.maxValue, this.minValue]; |
|
|
|
|
domain = sortBy(domain) as [number, number]; |
|
|
|
|
if(this.options.axis.y.invert === true) { |
|
|
|
|
domain = reverse(domain); |
|
|
|
|
} |
|
|
|
|
this._yScale = this.d3.scaleLinear() |
|
|
|
|
.domain(domain) |
|
|
|
|
.range([this.height, 0]); // inversed, because d3 y-axis goes from top to bottom
|
|
|
|
|
} |
|
|
|
|
return this._yScale; |
|
|
|
|
return this.state.yScale; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected get y1Scale(): d3.ScaleLinear<number, number> { |
|
|
|
|
if(this.isSeriesUnavailable || this.options.axis.y1 === undefined || this.options.axis.y1.isActive === false) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
// scale for y1 axis(right y axis)
|
|
|
|
|
if(this._y1Scale === null) { |
|
|
|
|
let domain = this.state.y1ValueRange || [this.y1MaxValue, this.y1MinValue]; |
|
|
|
|
domain = sortBy(domain) as [number, number]; |
|
|
|
|
if(this.options.axis.y1.invert === true) { |
|
|
|
|
domain = reverse(domain); |
|
|
|
|
} |
|
|
|
|
this._y1Scale = this.d3.scaleLinear() |
|
|
|
|
.domain(domain) |
|
|
|
|
.range([this.height, 0]); // inversed, because d3 y-axis goes from top to bottom
|
|
|
|
|
} |
|
|
|
|
return this._y1Scale; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
filterSerieByYAxisOrientation(serie: T, orientation: yAxisOrientation): boolean { |
|
|
|
|
if(serie.yOrientation === undefined || serie.yOrientation === yAxisOrientation.BOTH) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return serie.yOrientation === orientation; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get minValue(): number { |
|
|
|
|
// y min value
|
|
|
|
|
if(this.isSeriesUnavailable) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[0]; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.y !== undefined && this.options.axis.y.range !== undefined) { |
|
|
|
|
return min(this.options.axis.y.range); |
|
|
|
|
} |
|
|
|
|
const minValue = min( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false && this.filterSerieByYAxisOrientation(serie, yAxisOrientation.LEFT)) |
|
|
|
|
.map( |
|
|
|
|
serie => minBy<number[]>(serie.datapoints, dp => dp[1])[1] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return minValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get maxValue(): number { |
|
|
|
|
// y max value
|
|
|
|
|
if(this.isSeriesUnavailable) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[1]; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.y !== undefined && this.options.axis.y.range !== undefined) { |
|
|
|
|
return max(this.options.axis.y.range); |
|
|
|
|
} |
|
|
|
|
const maxValue = max( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false && this.filterSerieByYAxisOrientation(serie, yAxisOrientation.LEFT)) |
|
|
|
|
.map( |
|
|
|
|
serie => maxBy<number[]>(serie.datapoints, dp => dp[1])[1] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return maxValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get y1MinValue(): number { |
|
|
|
|
// TODO: remove duplicates
|
|
|
|
|
if(this.isSeriesUnavailable || this.options.axis.y1 === undefined || this.options.axis.y1.isActive === false) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[0]; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.y1.range !== undefined) { |
|
|
|
|
return min(this.options.axis.y1.range); |
|
|
|
|
} |
|
|
|
|
const minValue = min( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false && this.filterSerieByYAxisOrientation(serie, yAxisOrientation.RIGHT)) |
|
|
|
|
.map( |
|
|
|
|
serie => minBy<number[]>(serie.datapoints, dp => dp[1])[1] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return minValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get y1MaxValue(): number { |
|
|
|
|
if(this.isSeriesUnavailable || this.options.axis.y1 === undefined || this.options.axis.y1.isActive === false) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[1]; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.y1 !== undefined && this.options.axis.y1.range !== undefined) { |
|
|
|
|
return max(this.options.axis.y1.range); |
|
|
|
|
} |
|
|
|
|
const maxValue = max( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false && this.filterSerieByYAxisOrientation(serie, yAxisOrientation.RIGHT)) |
|
|
|
|
.map( |
|
|
|
|
serie => maxBy<number[]>(serie.datapoints, dp => dp[1])[1] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return maxValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get minValueX(): number { |
|
|
|
|
if(this.isSeriesUnavailable) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(this.options.axis.x !== undefined && this.options.axis.x.range !== undefined) { |
|
|
|
|
return min(this.options.axis.x.range) |
|
|
|
|
} |
|
|
|
|
const minValue = min( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false) |
|
|
|
|
.map( |
|
|
|
|
serie => minBy<number[]>(serie.datapoints, dp => dp[0])[0] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return minValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get maxValueX(): number { |
|
|
|
|
if(this.isSeriesUnavailable) { |
|
|
|
|
return DEFAULT_AXIS_RANGE[1]; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.x !== undefined && this.options.axis.x.range !== undefined) { |
|
|
|
|
return max(this.options.axis.x.range) |
|
|
|
|
} |
|
|
|
|
const maxValue = max( |
|
|
|
|
this.series |
|
|
|
|
.filter(serie => serie.visible !== false) |
|
|
|
|
.map( |
|
|
|
|
serie => maxBy<number[]>(serie.datapoints, dp => dp[0])[0] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
return maxValue; |
|
|
|
|
return this.state.y1Scale; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getd3TimeRangeEvery(count: number): d3.TimeInterval { |
|
|
|
@ -1146,27 +990,13 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
return mergeWith({}, DEFAULT_MARGIN, this.extraMargin, add); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get isSeriesUnavailable(): boolean { |
|
|
|
|
// TODO: Use one && throw error
|
|
|
|
|
return this.series === undefined || this.series.length === 0 || |
|
|
|
|
max(this.series.map(serie => serie.datapoints.length)) === 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
formatedBound(alias: string, target: string): string { |
|
|
|
|
formattedBound(alias: string, target: string): string { |
|
|
|
|
const confidenceMetric = replace(alias, '$__metric_name', target); |
|
|
|
|
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 { |
|
|
|
@ -1192,8 +1022,8 @@ abstract class ChartwerkPod<T extends TimeSerie, O extends Options> {
|
|
|
|
|
} |
|
|
|
|
let series = []; |
|
|
|
|
this.series.forEach(serie => { |
|
|
|
|
series.push(this.formatedBound(this.options.bounds.upper, serie.target)); |
|
|
|
|
series.push(this.formatedBound(this.options.bounds.lower, serie.target)); |
|
|
|
|
series.push(this.formattedBound(this.options.bounds.upper, serie.target)); |
|
|
|
|
series.push(this.formattedBound(this.options.bounds.lower, serie.target)); |
|
|
|
|
}); |
|
|
|
|
return series; |
|
|
|
|
} |
|
|
|
|