|
| 1 | +/* |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +* or more contributor license agreements. See the NOTICE file |
| 4 | +* distributed with this work for additional information |
| 5 | +* regarding copyright ownership. The ASF licenses this file |
| 6 | +* to you under the Apache License, Version 2.0 (the |
| 7 | +* "License"); you may not use this file except in compliance |
| 8 | +* with the License. You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, |
| 13 | +* software distributed under the License is distributed on an |
| 14 | +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +* KIND, either express or implied. See the License for the |
| 16 | +* specific language governing permissions and limitations |
| 17 | +* under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +import { each } from 'zrender/src/core/util'; |
| 21 | +import { addSafe } from './number'; |
| 22 | +import { StackInfo } from './types'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Percent stackStrategy logic to normalize each value as a percentage of the total per index. |
| 26 | + */ |
| 27 | +export function calculatePercentStack(stackInfoList: StackInfo[]) { |
| 28 | + const dataLength = stackInfoList[0].data.count(); |
| 29 | + if (dataLength === 0) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Calculate totals per data index across all series in the stack group. |
| 34 | + const totals = calculateStackTotals(stackInfoList, dataLength); |
| 35 | + |
| 36 | + // Used to track running total of percent values at each index. |
| 37 | + const cumulativePercents = new Float64Array(dataLength); |
| 38 | + |
| 39 | + const resultNaN = [NaN, NaN]; |
| 40 | + |
| 41 | + each(stackInfoList, function (targetStackInfo) { |
| 42 | + const resultVal: number[] = []; |
| 43 | + const dims: [string, string] = [targetStackInfo.stackResultDimension, targetStackInfo.stackedOverDimension]; |
| 44 | + const targetData = targetStackInfo.data; |
| 45 | + const stackedDim = targetStackInfo.stackedDimension; |
| 46 | + |
| 47 | + // Should not write on raw data, because stack series model list changes |
| 48 | + // depending on legend selection. |
| 49 | + targetData.modify(dims, function (v0, v1, dataIndex) { |
| 50 | + const rawValue = targetData.get(stackedDim, dataIndex) as number; |
| 51 | + |
| 52 | + // Consider `connectNulls` of line area, if value is NaN, stackedOver |
| 53 | + // should also be NaN, to draw a appropriate belt area. |
| 54 | + if (isNaN(rawValue)) { |
| 55 | + return resultNaN; |
| 56 | + } |
| 57 | + |
| 58 | + // Pre-calculated total for this specific data index. |
| 59 | + const total = totals[dataIndex]; |
| 60 | + |
| 61 | + // Percentage contribution of this segment. |
| 62 | + const percent = total === 0 ? 0 : (rawValue / total) * 100; |
| 63 | + |
| 64 | + // Bottom edge of this segment (cumulative % before this series). |
| 65 | + const stackedOver = cumulativePercents[dataIndex]; |
| 66 | + |
| 67 | + // Update the cumulative percentage for the next series at this index to use. |
| 68 | + cumulativePercents[dataIndex] = addSafe(stackedOver, percent); |
| 69 | + |
| 70 | + // Result: [Top edge %, Bottom edge %] |
| 71 | + resultVal[0] = cumulativePercents[dataIndex]; |
| 72 | + resultVal[1] = stackedOver; |
| 73 | + return resultVal; |
| 74 | + }); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | +* Helper to calculate the total value across all series for each data index. |
| 80 | +*/ |
| 81 | +function calculateStackTotals(stackInfoList: StackInfo[], dataLength: number): number[] { |
| 82 | + const totals = Array(dataLength).fill(0); |
| 83 | + each(stackInfoList, (stackInfo) => { |
| 84 | + const data = stackInfo.data; |
| 85 | + const dim = stackInfo.stackedDimension; |
| 86 | + for (let i = 0; i < dataLength; i++) { |
| 87 | + const val = data.get(dim, i) as number; |
| 88 | + if (!isNaN(val)) { |
| 89 | + totals[i] = addSafe(totals[i], val); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + return totals; |
| 94 | +} |
0 commit comments