Copilot commented on code in PR #21604:
URL: https://github.com/apache/echarts/pull/21604#discussion_r3182513506
##########
src/processor/dataStack.ts:
##########
@@ -170,3 +198,108 @@ function calculateStack(stackInfoList: StackInfo[]) {
});
});
}
+
+function getStackTotalMap(
+ stackInfoList: StackInfo[],
+ stackTotalMaps: StackTotalMaps,
+ isStackedByIndex: boolean
+) {
+ const totalMapKey = isStackedByIndex ? 'byIndex' : 'byDimension';
+ return stackTotalMaps[totalMapKey]
+ || (stackTotalMaps[totalMapKey] =
calculateStackTotalMap(stackInfoList, isStackedByIndex));
+}
+
+function calculateStackTotalMap(stackInfoList: StackInfo[], isStackedByIndex:
boolean) {
+ const stackTotalMap = createHashMap<StackTotal, string | number>();
+
+ for (let i = 0; i < stackInfoList.length; i++) {
+ const stackInfo = stackInfoList[i];
+ const data = stackInfo.data;
+
+ for (let dataIndex = 0, len = data.count(); dataIndex < len;
dataIndex++) {
+ const value = data.get(stackInfo.stackedDimension, dataIndex) as
number;
+
+ if (isNaN(value)) {
+ continue;
+ }
+
+ addStackTotal(
+ stackTotalMap,
+ isStackedByIndex
+ ? data.getRawIndex(dataIndex)
+ : data.get(stackInfo.stackedByDimension, dataIndex) as
number,
+ value
+ );
+ }
+ }
+
+ return stackTotalMap;
+}
+
+function addStackTotal(stackTotalMap: StackTotalMap, key: string | number,
value: number) {
+ const total = stackTotalMap.get(key) || stackTotalMap.set(key, {
+ all: 0,
+ positive: 0,
+ negative: 0
+ });
+
+ total.all = addSafe(total.all, value);
+ if (value > 0) {
+ total.positive = addSafe(total.positive, value);
+ }
+ else if (value < 0) {
+ total.negative = addSafe(total.negative, value);
+ }
+}
+
+function normalizeStackValue(
+ stackInfoList: StackInfo[],
+ stackTotalMaps: StackTotalMaps,
+ targetStackInfo: StackInfo,
+ dataIndex: number,
+ stackStrategy: StackSeriesOption['stackStrategy']
+) {
+ const rawValue =
targetStackInfo.data.get(targetStackInfo.stackedDimension, dataIndex) as number;
+
+ if (isNaN(rawValue)) {
+ return NaN;
+ }
+
+ const stackTotalMap = getStackTotalMap(stackInfoList, stackTotalMaps,
targetStackInfo.isStackedByIndex);
+ const key = targetStackInfo.isStackedByIndex
+ ? targetStackInfo.data.getRawIndex(dataIndex)
+ : targetStackInfo.data.get(targetStackInfo.stackedByDimension,
dataIndex) as number;
+ const totalInfo = stackTotalMap.get(key);
+ const total = totalInfo && getStackTotal(totalInfo, rawValue,
stackStrategy);
+ return total ? rawValue / Math.abs(total) : 0;
+}
+
+function getStackTotal(
+ totalInfo: StackTotal,
+ targetValue: number,
+ stackStrategy: StackSeriesOption['stackStrategy']
+) {
+ if (stackStrategy === 'all') {
+ return totalInfo.all;
+ }
+ else if (stackStrategy === 'positive') {
+ return totalInfo.positive;
+ }
+ else if (stackStrategy === 'negative') {
+ return totalInfo.negative;
+ }
+
+ return targetValue >= 0 ? totalInfo.positive : totalInfo.negative;
+}
Review Comment:
`normalizeStackValue`/`getStackTotal` adds normalized handling for
`stackStrategy: 'positive'` and `'negative'`, but the new unit tests only cover
`'samesign'` (default) and `'all'`. Please add UT cases that enable
`stackNormalize` together with `stackStrategy: 'positive'` and `'negative'`
(including mixed-sign inputs) so these branches and their expected behavior are
validated.
##########
src/processor/dataStack.ts:
##########
@@ -170,3 +198,108 @@ function calculateStack(stackInfoList: StackInfo[]) {
});
});
}
+
+function getStackTotalMap(
+ stackInfoList: StackInfo[],
+ stackTotalMaps: StackTotalMaps,
+ isStackedByIndex: boolean
+) {
+ const totalMapKey = isStackedByIndex ? 'byIndex' : 'byDimension';
+ return stackTotalMaps[totalMapKey]
+ || (stackTotalMaps[totalMapKey] =
calculateStackTotalMap(stackInfoList, isStackedByIndex));
+}
+
+function calculateStackTotalMap(stackInfoList: StackInfo[], isStackedByIndex:
boolean) {
+ const stackTotalMap = createHashMap<StackTotal, string | number>();
+
+ for (let i = 0; i < stackInfoList.length; i++) {
+ const stackInfo = stackInfoList[i];
+ const data = stackInfo.data;
+
+ for (let dataIndex = 0, len = data.count(); dataIndex < len;
dataIndex++) {
+ const value = data.get(stackInfo.stackedDimension, dataIndex) as
number;
+
+ if (isNaN(value)) {
+ continue;
+ }
+
+ addStackTotal(
+ stackTotalMap,
+ isStackedByIndex
+ ? data.getRawIndex(dataIndex)
+ : data.get(stackInfo.stackedByDimension, dataIndex) as
number,
Review Comment:
In the normalization total map, the key for `stackedByDimension` is asserted
as `number` (`data.get(... ) as number`), but category axis values can be
strings. Using `string | number` (or the appropriate raw value type) for the
key here (and in `normalizeStackValue`) would better reflect actual data and
avoid accidental numeric-only assumptions later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]