Skip to content
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

feat: add new stacking strategies #17086

Merged
merged 3 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 13 additions & 8 deletions src/processor/dataStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ type StackInfo = Pick<
// (1) [Caution]: the logic is correct based on the premises:
// data processing stage is blocked in stream.
// See <module:echarts/stream/Scheduler#performDataProcessorTasks>
// (2) Only register once when import repeatly.
// Should be executed after series filtered and before stack calculation.
// (2) Only register once when import repeatedly.
// Should be executed after series is filtered and before stack calculation.
export default function dataStack(ecModel: GlobalModel) {
const stackInfoMap = createHashMap<StackInfo[]>();
ecModel.eachSeries(function (seriesModel: SeriesModel<SeriesOption & SeriesStackOptionMixin>) {
const stack = seriesModel.get('stack');
// Compatibal: when `stack` is set as '', do not stack.
// Compatible: when `stack` is set as '', do not stack.
if (stack) {
const stackInfoList = stackInfoMap.get(stack) || stackInfoMap.set(stack, []);
const data = seriesModel.getData();
Expand Down Expand Up @@ -87,6 +87,7 @@ function calculateStack(stackInfoList: StackInfo[]) {
const dims: [string, string] = [targetStackInfo.stackResultDimension, targetStackInfo.stackedOverDimension];
const targetData = targetStackInfo.data;
const isStackedByIndex = targetStackInfo.isStackedByIndex;
const stackStrategy = targetStackInfo.seriesModel.get('stackStrategy') || 'samesign';

// Should not write on raw data, because stack series model list changes
// depending on legend selection.
Expand Down Expand Up @@ -126,12 +127,16 @@ function calculateStack(stackInfoList: StackInfo[]) {
) as number;

// Considering positive stack, negative stack and empty data
if ((sum >= 0 && val > 0) // Positive stack
|| (sum <= 0 && val < 0) // Negative stack
if (
stackStrategy === 'all' // single stack group
|| (stackStrategy === 'positive' && val > 0)
|| (stackStrategy === 'negative' && val < 0)
|| (stackStrategy === 'samesign' && sum >= 0 && val > 0) // All positive stack
|| (stackStrategy === 'samesign' && sum <= 0 && val < 0) // All negative stack
) {
// The sum should be as less as possible to be effected
// by floating arithmetic problem. A wrong result probably
// filtered incorrectly by axis min/max.
// The sum has to be very small to be affected by the
// floating arithmetic problem. An incorrect result will probably
// cause axis min/max to be filtered incorrectly.
sum = addSafe(sum, val);
stackedOver = val;
break;
Expand Down
1 change: 1 addition & 0 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,7 @@ export interface SeriesLargeOptionMixin {
}
export interface SeriesStackOptionMixin {
stack?: string
stackStrategy?: 'samesign' | 'all' | 'positive' | 'negative';
}

type SamplingFunc = (frame: ArrayLike<number>) => number;
Expand Down
191 changes: 182 additions & 9 deletions test/area-stack.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.