Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class WaterfallChartSpecTransformer<
series.stackLabel = spec.stackLabel;
series.leaderLine = spec.leaderLine;
series.total = spec.total;
series.calculationMode = spec.calculationMode;

return series;
}
Expand Down
18 changes: 14 additions & 4 deletions packages/vchart/src/data/transforms/waterfall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface IWaterfallOpt {
startAs: string;
endAs: string;
total: IWaterfallSeriesSpec['total'];
calculationMode: 'increase' | 'decrease';

seriesFieldName: {
total: string;
increase: string;
Expand All @@ -42,7 +44,7 @@ export const waterfall = (lastData: Array<Datum>, op: IWaterfallOpt) => {
if (!lastData || lastData.length === 0) {
return lastData;
}
const { indexField, total: totalSpec, groupData } = op;
const { indexField, total: totalSpec, groupData, calculationMode } = op;
const totalData: {
start: number;
end: number;
Expand All @@ -57,7 +59,10 @@ export const waterfall = (lastData: Array<Datum>, op: IWaterfallOpt) => {
dimensionValues: { [key in string]: Set<string> };
dimensionData: { [key in string]: Datum[] };
};
const indexValues = Array.from(dimensionValues[indexField]);
let indexValues = Array.from(dimensionValues[indexField]);
if (calculationMode === 'decrease') {
indexValues = indexValues.reverse();
}
// 上一次的计算结果
let temp: { start: number; end: number; lastIndex: string; positive: number; negative: number } = {
start: 0,
Expand Down Expand Up @@ -320,20 +325,25 @@ export interface IWaterfallFillEndOpt {
valueField: string;
seriesField?: string;
total: IWaterfallSeriesSpec['total'];
calculationMode: IWaterfallSeriesSpec['calculationMode'];
}

export const waterfallFillTotal = (data: Array<Datum>, op: IWaterfallFillEndOpt) => {
if (!data) {
return data;
}
const { indexField, valueField, total, seriesField } = op;
const { indexField, valueField, total, seriesField, calculationMode } = op;
const totalData = {
[indexField]: total?.text || 'total',
[valueField]: data.reduce((pre, cur) => precisionAdd(pre, +cur[valueField]), 0)
};
if (seriesField) {
totalData[seriesField] = 'total';
}
data.push(totalData);
if (calculationMode === 'decrease') {
data.unshift(totalData);
} else {
data.push(totalData);
}
return data;
};
7 changes: 7 additions & 0 deletions packages/vchart/src/series/waterfall/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ export interface IWaterfallSeriesSpec
/** 标签偏移量 */
offset?: number;
};

/**
* 瀑布图类型
* 默认为 increase 增长瀑布图
* @since 2.0.6
*/
calculationMode?: 'increase' | 'decrease';
}

export interface IWaterfallSeriesTheme extends IBarSeriesTheme {
Expand Down
11 changes: 7 additions & 4 deletions packages/vchart/src/series/waterfall/waterfall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class WaterfallSeries<T extends IWaterfallSeriesSpec = IWaterfallSeriesSp
seriesField: this.getSeriesField(),
seriesFieldName: this._theme.seriesFieldName,
total: this._spec.total,
calculationMode: this._spec.calculationMode ?? 'increase',
stackInverse: this.getRegion().getStackInverse()
}
},
Expand All @@ -126,6 +127,7 @@ export class WaterfallSeries<T extends IWaterfallSeriesSpec = IWaterfallSeriesSp
startAs: STACK_FIELD_START,
endAs: STACK_FIELD_END,
total: this._spec.total,
calculationMode: this._spec.calculationMode ?? 'increase',
groupData: () => this.getGroups().groupData,
stackInverse: this.getRegion().getStackInverse()
}
Expand Down Expand Up @@ -301,6 +303,7 @@ export class WaterfallSeries<T extends IWaterfallSeriesSpec = IWaterfallSeriesSp

initMarkStyle(): void {
super.initMarkStyle();
const isDecrease = this._spec.calculationMode === 'decrease';
if (this._leaderLineMark) {
if (this._direction === Direction.horizontal) {
this.setMarkStyle(
Expand All @@ -313,9 +316,9 @@ export class WaterfallSeries<T extends IWaterfallSeriesSpec = IWaterfallSeriesSp
if (!datum.lastIndex) {
return 0;
}
return this.totalPositionY(datum, 'lastIndex', 1);
return this.totalPositionY(datum, 'lastIndex', isDecrease ? 0 : 1);
},
y1: (datum: Datum) => this.totalPositionY(datum, 'index', 0)
y1: (datum: Datum) => this.totalPositionY(datum, 'index', isDecrease ? 1 : 0)
},
'normal',
AttributeLevel.Series
Expand All @@ -329,9 +332,9 @@ export class WaterfallSeries<T extends IWaterfallSeriesSpec = IWaterfallSeriesSp
if (!datum.lastIndex) {
return 0;
}
return this.totalPositionX(datum, 'lastIndex', 1);
return this.totalPositionX(datum, 'lastIndex', isDecrease ? 0 : 1);
},
x1: (datum: Datum) => this.totalPositionX(datum, 'index', 0),
x1: (datum: Datum) => this.totalPositionX(datum, 'index', isDecrease ? 1 : 0),
y: (datum: Datum) => this.totalPositionY(datum, 'lastEnd', 0),
y1: (datum: Datum) => this.totalPositionY(datum, datum.isTotal ? 'end' : 'start', 0)
},
Expand Down
Loading