From edeff8b1d0668d576ebbb05c387007f36bbb49c5 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt Date: Mon, 23 May 2022 09:18:07 +0300 Subject: [PATCH 1/3] feat: add new stacking strategies --- src/processor/dataStack.ts | 21 ++-- src/util/types.ts | 1 + test/area-stack.html | 191 +++++++++++++++++++++++++++++++++++-- 3 files changed, 196 insertions(+), 17 deletions(-) diff --git a/src/processor/dataStack.ts b/src/processor/dataStack.ts index 42f7779ba6..c98261e0c5 100644 --- a/src/processor/dataStack.ts +++ b/src/processor/dataStack.ts @@ -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 -// (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(); ecModel.eachSeries(function (seriesModel: SeriesModel) { 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(); @@ -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'); // Should not write on raw data, because stack series model list changes // depending on legend selection. @@ -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 && sum >= 0 && val > 0) // Positive stack + || (!stackStrategy && sum <= 0 && val < 0) // 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; diff --git a/src/util/types.ts b/src/util/types.ts index c503f48eec..ce8ff015c3 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -1661,6 +1661,7 @@ export interface SeriesLargeOptionMixin { } export interface SeriesStackOptionMixin { stack?: string + stackStrategy?: 'all' | 'positive' | 'negative'; } type SamplingFunc = (frame: ArrayLike) => number; diff --git a/test/area-stack.html b/test/area-stack.html index 4cc1248738..454a48bdb9 100644 --- a/test/area-stack.html +++ b/test/area-stack.html @@ -40,13 +40,10 @@
- - - - - - - +
+
+
+
+ + + + + + + - \ No newline at end of file + From 6d69ccd040f4492e8568205ccc645608c8d29c08 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt Date: Mon, 23 May 2022 12:14:47 +0300 Subject: [PATCH 2/3] add explicit default --- src/processor/dataStack.ts | 6 +++--- src/util/types.ts | 2 +- test/area-stack.html | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/processor/dataStack.ts b/src/processor/dataStack.ts index c98261e0c5..824e630594 100644 --- a/src/processor/dataStack.ts +++ b/src/processor/dataStack.ts @@ -87,7 +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'); + const stackStrategy = targetStackInfo.seriesModel.get('stackStrategy') || 'samesign'; // Should not write on raw data, because stack series model list changes // depending on legend selection. @@ -131,8 +131,8 @@ function calculateStack(stackInfoList: StackInfo[]) { stackStrategy === 'all' // single stack group || (stackStrategy === 'positive' && val > 0) || (stackStrategy === 'negative' && val < 0) - || (!stackStrategy && sum >= 0 && val > 0) // Positive stack - || (!stackStrategy && sum <= 0 && val < 0) // Negative stack + || (stackStrategy === 'samesign' && sum >= 0 && val > 0) // All positive stack + || (stackStrategy === 'samesign' && sum <= 0 && val < 0) // All negative stack ) { // The sum has to be very small to be affected by the // floating arithmetic problem. An incorrect result will probably diff --git a/src/util/types.ts b/src/util/types.ts index ce8ff015c3..e8d3f1d182 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -1661,7 +1661,7 @@ export interface SeriesLargeOptionMixin { } export interface SeriesStackOptionMixin { stack?: string - stackStrategy?: 'all' | 'positive' | 'negative'; + stackStrategy?: 'samesign' | 'all' | 'positive' | 'negative'; } type SamplingFunc = (frame: ArrayLike) => number; diff --git a/test/area-stack.html b/test/area-stack.html index 454a48bdb9..164bba9d20 100644 --- a/test/area-stack.html +++ b/test/area-stack.html @@ -609,7 +609,7 @@ }; testHelper.create(echarts, 'stack-default', { - title: 'Stacking with positive and negative values (default)', + title: 'Stacking with positive and negative values (default: samesign)', option: option }); }); From 8e3250fefab3d1b658686ead4596b56d9ee2f168 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt Date: Fri, 27 May 2022 14:41:56 +0300 Subject: [PATCH 3/3] remove redundant spaces --- test/area-stack.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/area-stack.html b/test/area-stack.html index 164bba9d20..c60eaa1b64 100644 --- a/test/area-stack.html +++ b/test/area-stack.html @@ -579,7 +579,7 @@ require(['echarts'], function (echarts) { - var option = { + var option = { xAxis: [ { type: 'category', @@ -623,7 +623,7 @@ require(['echarts'], function (echarts) { - var option = { + var option = { xAxis: [ { type: 'category', @@ -669,7 +669,7 @@ require(['echarts'], function (echarts) { - var option = { + var option = { xAxis: [ { type: 'category', @@ -715,7 +715,7 @@ require(['echarts'], function (echarts) { - var option = { + var option = { xAxis: [ { type: 'category',