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.
60 lines
1.9 KiB
60 lines
1.9 KiB
import { BarSerie, BarOptions, Datapoint, Color, ValueX, OpacityFormatter } from '../types'; |
|
|
|
import * as _ from 'lodash'; |
|
|
|
export type DataRow = { |
|
key: number, // x |
|
values: number[], // y list |
|
colors: string[], |
|
opacity: number[], |
|
serieTarget: string, |
|
} |
|
|
|
export class DataProcessor { |
|
_formattedDataRows: DataRow[]; |
|
|
|
constructor(protected series: BarSerie[], protected options: BarOptions) { |
|
|
|
} |
|
|
|
public get dataRows(): DataRow[] { |
|
return this._formattedDataRows; |
|
} |
|
|
|
protected setDataRowsForNonDiscreteType(): void { |
|
for(let serie of this.series) { |
|
const rows = _.map(serie.datapoints, (datapoint: Datapoint) => { |
|
const values = _.tail(datapoint); |
|
return { |
|
key: datapoint[0], // x |
|
values, // y list |
|
colors: this.getColorsForEachValue(values, serie.color, datapoint[0], serie.target), |
|
opacity: this.getOpacityForEachValue(values, serie.opacityFormatter, datapoint[0], serie.target), |
|
target: serie.target, |
|
} |
|
}); |
|
this._formattedDataRows = _.concat(this._formattedDataRows, rows); |
|
} |
|
this._formattedDataRows = _.sortBy(this._formattedDataRows, 'key'); |
|
} |
|
|
|
getColorsForEachValue(values: number[], color: Color, key: ValueX, target: string): string[] { |
|
if(_.isString(color)) { |
|
return _.map(values, value => color); |
|
} |
|
if(_.isArray(color)) { |
|
return _.map(values, (value, i) => color[i] || 'red'); |
|
} |
|
if(_.isFunction(color)) { |
|
return _.map(values, (value, i) => (color as Function)({ value, barIndex: i, key, target })); |
|
} |
|
throw new Error(`Unknown type of serie color: ${target} ${color}`); |
|
} |
|
|
|
getOpacityForEachValue(values: number[], opacity: OpacityFormatter | undefined, key: ValueX, target: string): number[] { |
|
if(opacity === undefined) { |
|
return _.map(values, value => 1); |
|
} |
|
return _.map(values, (value, i) => opacity({ value, barIndex: i, key, target })); |
|
} |
|
}
|
|
|