From 3777ba470c5a4e19121c208fe483f4524a4dc36f Mon Sep 17 00:00:00 2001 From: vargburz Date: Sat, 17 Sep 2022 01:44:19 +0300 Subject: [PATCH] try to adddiscrete type --- src/index.ts | 7 +++-- src/models/data_processor.ts | 54 ++++++++++++++++++++++++++++-------- src/types.ts | 3 +- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7a75018..4926ab5 100755 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,9 @@ import { ChartwerkPod, VueChartwerkPodMixin, TimeFormat, AxisFormat } from '@cha import { BarConfig } from './models/bar_options'; import { BarSeries } from './models/bar_series'; import { BarAnnotation } from './models/bar_annotation'; +import { DataProcessor } from './models/data_processor'; -import { BarSerie, BarOptions, RowValues } from './types'; +import { BarSerie, BarOptions } from './types'; import { findClosest } from './utils'; import * as d3 from 'd3'; @@ -13,14 +14,16 @@ import * as _ from 'lodash'; export class ChartwerkBarPod extends ChartwerkPod { barYScale: null | d3.ScaleLinear = null; - _seriesDataForRendring = []; + dataProcessor: DataProcessor; series: BarSeries; options: BarConfig; + constructor(el: HTMLElement, _series: BarSerie[] = [], _options: BarOptions = {}) { super(el, _series, _options); this.series = new BarSeries(_series); this.options = new BarConfig(_options); + this.dataProcessor = new DataProcessor(this.series.visibleSeries, this.options); } protected renderMetrics(): void { diff --git a/src/models/data_processor.ts b/src/models/data_processor.ts index 6cedea2..9651930 100644 --- a/src/models/data_processor.ts +++ b/src/models/data_processor.ts @@ -1,36 +1,60 @@ -import { BarSerie, BarOptions, Datapoint, Color, ValueX, OpacityFormatter } from '../types'; +import { BarSerie, BarOptions, Datapoint, Color, ValueX, OpacityFormatter, BarPodType } from '../types'; import * as _ from 'lodash'; export type DataRow = { - key: number, // x - values: number[], // y list - colors: string[], - opacity: number[], - serieTarget: string, + 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 - 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, + 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); @@ -57,4 +81,12 @@ export class DataProcessor { } 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); + } } diff --git a/src/types.ts b/src/types.ts index 1bf9934..9da78b8 100755 --- a/src/types.ts +++ b/src/types.ts @@ -25,7 +25,8 @@ export type BarSerie = Serie & BarSerieAdditionalParams; export type BarAdditionalOptions = { type: { // BarPodType.DISCRETE or BarPodType.NON_DISCRETE. Cant be both [BarPodType.DISCRETE]: { - groupBy: { x: ValueX[] }; // union bars as one group, see examples/demo-group-by.html + // union bars as one group, see examples/demo-group-by.html + groupBy: { x: ValueX[] }; // use list of X values from first Serie by default innerSize: { value: number, type?: SizeType.PERCENT | SizeType.PX }; // size of bars inside one group groupSize: { value: number, type?: SizeType.PERCENT | SizeType.PX }; };