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.
92 lines
2.9 KiB
92 lines
2.9 KiB
import { BarSerie, BarOptions, Datapoint, Color, ValueX, OpacityFormatter, BarPodType } from '../types'; |
|
|
|
import * as _ from 'lodash'; |
|
|
|
export type DataRow = { |
|
key: number; // x |
|
data: { |
|
[serieTarget: string]: { |
|
values: number[]; // y list |
|
colors: string[]; |
|
opacity: number[]; |
|
} |
|
}; |
|
maxSumm: number; |
|
minSubtraction: number; |
|
} |
|
|
|
export class DataProcessor { |
|
_formattedDataRows: DataRow[]; |
|
|
|
constructor(protected series: BarSerie[], protected options: BarOptions) { |
|
this.setDataRowsForNonDiscreteType(); |
|
} |
|
|
|
public get dataRows(): DataRow[] { |
|
return this._formattedDataRows; |
|
} |
|
|
|
protected setDataRowsForDiscreteType(): void { |
|
const groupByList = ( |
|
this.options.type[BarPodType.DISCRETE].groupBy.x || |
|
_.map(_.first(this.series).datapoints, datapoint => datapoint[0]) |
|
); |
|
let rows = []; |
|
// TODO: not effective |
|
for(let groupItem of groupByList) { |
|
for(let serie of this.series) { |
|
const datapoint = _.find(serie.datapoints, ) |
|
} |
|
} |
|
} |
|
|
|
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 |
|
data: { |
|
[serie.target]: { |
|
values, // y list |
|
colors: this.getColorsForEachValue(values, serie.color, datapoint[0], serie.target), |
|
opacity: this.getOpacityForEachValue(values, serie.opacityFormatter, datapoint[0], serie.target), |
|
} |
|
}, |
|
maxSumm: this.getMaxSumm(values), // max y axis scale |
|
minSubtraction: this.getMinSubtraction(values), // min y axis scale |
|
} |
|
}); |
|
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 })); |
|
} |
|
|
|
getMaxSumm(values: number[]): number { |
|
return values.reduce((prev, curr) => curr > 0 ? prev + curr : prev, 0); |
|
} |
|
|
|
getMinSubtraction(values: number[]): number { |
|
return values.reduce((prev, curr) => curr < 0 ? prev + curr : prev, 0); |
|
} |
|
}
|
|
|