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.
71 lines
1.9 KiB
71 lines
1.9 KiB
import { CoreSerie, yAxisOrientation } from '../types'; |
|
import { palette } from '../colors'; |
|
|
|
import lodashDefaultsDeep from 'lodash/defaultsDeep'; |
|
import lodashMap from 'lodash/map'; |
|
import lodashCloneDeep from 'lodash/cloneDeep'; |
|
import lodashUniq from 'lodash/uniq'; |
|
|
|
|
|
const SERIE_DEFAULTS = { |
|
alias: '', |
|
visible: true, |
|
yOrientation: yAxisOrientation.LEFT, |
|
datapoints: [], |
|
// fields below will be set in "fillDefaults" method |
|
idx: undefined, |
|
color: undefined, |
|
}; |
|
|
|
export class CoreSeries<T extends CoreSerie> { |
|
_series: Array<T> = []; |
|
|
|
constructor(series: T[]) { |
|
this.setSeries(series); |
|
} |
|
|
|
public updateSeries(series: T[]): void { |
|
this.setSeries(series); |
|
} |
|
|
|
protected setSeries(series: T[]): void { |
|
this._series = lodashMap(series, (serie, serieIdx) => this.fillDefaults(lodashCloneDeep(serie), serieIdx)); |
|
this.ensureSeriesValid(this._series); |
|
} |
|
|
|
ensureSeriesValid(series: T[]): void { |
|
const targets = lodashMap(series, serie => serie.target); |
|
const uniqTargets = lodashUniq(targets); |
|
if(uniqTargets.length !== series.length) { |
|
throw new Error(`All serie.target should be uniq`); |
|
} |
|
} |
|
|
|
protected fillDefaults(serie: T, idx: number): T { |
|
let defaults = lodashCloneDeep(SERIE_DEFAULTS); |
|
defaults.color = palette[idx % palette.length]; |
|
defaults.idx = idx; |
|
lodashDefaultsDeep(serie, defaults); |
|
return serie; |
|
} |
|
|
|
get isSeriesAvailable(): boolean { |
|
return this.visibleSeries.length > 0; |
|
} |
|
|
|
get visibleSeries(): Array<T> { |
|
return this._series.filter(serie => serie.visible); |
|
} |
|
|
|
get allSeries(): Array<T> { |
|
return this._series; |
|
} |
|
|
|
get leftYRelatedSeries(): Array<T> { |
|
return this.visibleSeries.filter(serie => serie.yOrientation = yAxisOrientation.LEFT); |
|
} |
|
|
|
get rightYRelatedSeries(): Array<T> { |
|
return this.visibleSeries.filter(serie => serie.yOrientation = yAxisOrientation.RIGHT); |
|
} |
|
}
|
|
|