8000 Fix/waterfall stack navigate by xuefei1313 · Pull Request #2819 · VisActor/VChart · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix/waterfall stack navigate #2819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2024
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
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix the issue issue with stacked waterfall charts where positive and negative values were not stacked separately when there were both positive and negative values in the same stack\n\n",
"type": "none",
"packageName": "@visactor/vchart"
}
],
"packageName": "@visactor/vchart",
"email": "lixuef1313@163.com"
}
57 changes: 43 additions & 14 deletions packages/vchart/src/data/transforms/waterfall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type TotalInfo = {
lastEnd: number;
index: string;
isTotal: boolean;
positive: number;
negative: number;
};

export interface IWaterfallOpt {
Expand Down Expand Up @@ -47,22 +49,32 @@ export const waterfall = (lastData: Array<Datum>, op: IWaterfallOpt) => {
lastEnd: number;
index: string;
isTotal: boolean;
positive: number;
negative: number;
}[] = [];
const { dimensionValues, dimensionData } = groupData().latestData as {
dimensionValues: { [key in string]: Set<string> };
dimensionData: { [key in string]: Datum[] };
};
const indexValues = Array.from(dimensionValues[indexField]);
// 上一次的计算结果
let temp: { start: number; end: number; lastIndex: string } = { start: 0, end: 0, lastIndex: null };
let temp: { start: number; end: number; lastIndex: string; positive: number; negative: number } = {
start: 0,
end: 0,
positive: 0,
negative: 0,
lastIndex: null
};
indexValues.forEach((key, index) => {
const total = {
start: temp.end,
end: temp.end,
lastIndex: temp.lastIndex,
lastEnd: temp.end,
index: key,
isTotal: false
isTotal: false,
positive: temp.end,
negative: temp.end
};

const indexData = dimensionData[key];
Expand Down Expand Up @@ -115,7 +127,7 @@ function computeTotalWithMultipleData(
key: string,
total: TotalInfo,
totalData: TotalInfo[],
temp: { start: number; end: number; lastIndex: string },
temp: { start: number; end: number; lastIndex: string; positive: number; negative: number },
indexValues: string[],
index: number,
op: IWaterfallOpt,
Expand Down Expand Up @@ -153,16 +165,25 @@ function computeTotalWithMultipleData(
let { start, end } = getTotalStartEnd(totalConfigData, total, totalData, temp, totalSpec);
total.start = start;
total.end = end;
let positive = start;
let navigate = start;
// 当前剩余的总计值
let valueTemp = end - start;
// 将非总计数据进行堆叠
_normalTemp.forEach(d => {
d[startAs] = +start;
d[endAs] = precisionAdd(d[startAs], +d[valueField]);
start = d[endAs];
valueTemp = precisionSub(valueTemp, +d[valueField]);
const value = +d[valueField];
if (value >= 0) {
d[startAs] = +positive;
positive = precisionAdd(positive, value);
} else {
d[startAs] = +navigate;
navigate = precisionAdd(navigate, value);
}
d[endAs] = precisionAdd(d[startAs], value);
start = precisionAdd(start, value);
valueTemp = precisionSub(valueTemp, value);
});
// 先在的start end 就是 total 的
// 现在的start end 就是 total 的
_totalTemp.forEach(d => {
d[startAs] = +start;
d[endAs] = precisionAdd(d[startAs], valueTemp);
Expand All @@ -176,7 +197,7 @@ function computeNormalData(
key: string,
total: TotalInfo,
totalData: TotalInfo[],
temp: { start: number; end: number; lastIndex: string },
temp: { start: number; end: number; lastIndex: string; positive: number; negative: number },
indexValues: string[],
index: number,
op: IWaterfallOpt
Expand Down Expand Up @@ -204,9 +225,17 @@ function computeNormalData(
}
}
if (!isTotalTag) {
d[startAs] = +total.end;
d[endAs] = precisionAdd(d[startAs], +d[valueField]);
total.end = d[endAs];
const value = +d[valueField];
// 区分正负值
if (value >= 0) {
d[startAs] = +total.positive;
total.positive = precisionAdd(total.positive, value);
} else {
d[startAs] = +total.negative;
total.negative = precisionAdd(total.negative, value);
}
d[endAs] = precisionAdd(d[startAs], value);
total.end = precisionAdd(total.end, value);
}
total.isTotal = isTotalTag;

Expand All @@ -225,7 +254,7 @@ function getTotalStartEnd(
d: Datum,
total: TotalInfo,
totalData: TotalInfo[],
temp: { start: number; end: number; lastIndex: string },
temp: { start: number; end: number; lastIndex: string; positive: number; negative: number },
totalSpec: IWaterfallOpt['total']
) {
if (!totalSpec || totalSpec.type === 'end') {
Expand All @@ -249,7 +278,7 @@ function getTotalInEndType(total: TotalInfo) {

function getTotalInCustomType(
d: Datum,
temp: { start: number; end: number; lastIndex: string },
temp: { start: number; end: number; lastIndex: string; positive: number; negative: number },
totalSpec: IWaterfallOpt['total']
) {
return (<IWaterfallTotalCustom>totalSpec).product(d, temp);
Expand Down
Loading
0