You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by su...@apache.org on 2019/09/23 14:23:51 UTC

[incubator-echarts] 01/01: fix: dataZoom toolbox can not return to the original state.

This is an automated email from the ASF dual-hosted git repository.

sushuang pushed a commit to branch fix/dataZoom-option
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git

commit d80dc0b9ed1642232c03486c42b397ac0ca8e81e
Author: SHUANG SU <su...@gmail.com>
AuthorDate: Mon Sep 23 22:22:59 2019 +0800

    fix: dataZoom toolbox can not return to the original state.
---
 src/component/dataZoom/AxisProxy.js         |   2 +-
 src/component/dataZoom/DataZoomModel.js     | 101 +++++++++++++++++++++-------
 src/component/dataZoom/dataZoomProcessor.js |   4 +-
 test/dataZoom-toolbox.html                  |  51 ++++++++++++++
 test/runTest/actions/__meta__.json          |   2 +-
 test/runTest/actions/dataZoom-toolbox.json  |   2 +-
 6 files changed, 132 insertions(+), 30 deletions(-)

diff --git a/src/component/dataZoom/AxisProxy.js b/src/component/dataZoom/AxisProxy.js
index 3b00305..0a2256d 100644
--- a/src/component/dataZoom/AxisProxy.js
+++ b/src/component/dataZoom/AxisProxy.js
@@ -298,7 +298,7 @@ AxisProxy.prototype = {
         // `calculateDataWindow` uses min/maxSpan.
         setMinMaxSpan(this);
 
-        var dataWindow = this.calculateDataWindow(dataZoomModel.option);
+        var dataWindow = this.calculateDataWindow(dataZoomModel.settledOption);
 
         this._valueWindow = dataWindow.valueWindow;
         this._percentWindow = dataWindow.percentWindow;
diff --git a/src/component/dataZoom/DataZoomModel.js b/src/component/dataZoom/DataZoomModel.js
index 99f5b1c..4388d60 100644
--- a/src/component/dataZoom/DataZoomModel.js
+++ b/src/component/dataZoom/DataZoomModel.js
@@ -116,29 +116,55 @@ var DataZoomModel = echarts.extendComponentModel({
          */
         this._rangePropMode = ['percent', 'percent'];
 
-        var rawOption = retrieveRaw(option);
+        var inputRawOption = retrieveRawOption(option);
+
+        /**
+         * Suppose a "main process" start at the point that model prepared (that is,
+         * model initialized or merged or method called in `action`).
+         * We should keep the `main process` idempotent, that is, given a set of values
+         * on `option`, we get the same result.
+         *
+         * But sometimes, values on `option` will be updated for providing users
+         * a "final calculated value" (`dataZoomProcessor` will do that). Those value
+         * should not be the base/input of the `main process`.
+         *
+         * So in that case we should save and keep the input of the `main process`
+         * separately, called `settledOption`.
+         *
+         * For example, consider the case:
+         * (Step_1) brush zoom the grid by `toolbox.dataZoom`,
+         *     where the original input `option.startValue`, `option.endValue` are earsed by
+         *     calculated value.
+         * (Step)2) click the legend to hide and show a series,
+         *     where the new range is calculated by the earsed `startValue` and `endValue`,
+         *     which brings incorrect result.
+         *
+         * @readOnly
+         */
+        this.settledOption = inputRawOption;
 
         this.mergeDefaultAndTheme(option, ecModel);
 
-        this.doInit(rawOption);
+        this.doInit(inputRawOption);
     },
 
     /**
      * @override
      */
     mergeOption: function (newOption) {
-        var rawOption = retrieveRaw(newOption);
+        var inputRawOption = retrieveRawOption(newOption);
 
         //FIX #2591
         zrUtil.merge(this.option, newOption, true);
+        zrUtil.merge(this.settledOption, inputRawOption, true);
 
-        this.doInit(rawOption);
+        this.doInit(inputRawOption);
     },
 
     /**
      * @protected
      */
-    doInit: function (rawOption) {
+    doInit: function (inputRawOption) {
         var thisOption = this.option;
 
         // Disable realtime view update if canvas is not supported.
@@ -146,16 +172,17 @@ var DataZoomModel = echarts.extendComponentModel({
             thisOption.realtime = false;
         }
 
-        this._setDefaultThrottle(rawOption);
+        this._setDefaultThrottle(inputRawOption);
 
-        updateRangeUse(this, rawOption);
+        updateRangeUse(this, inputRawOption);
 
+        var settledOption = this.settledOption;
         each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
             // start/end has higher priority over startValue/endValue if they
             // both set, but we should make chart.setOption({endValue: 1000})
             // effective, rather than chart.setOption({endValue: 1000, end: null}).
             if (this._rangePropMode[index] === 'value') {
-                thisOption[names[0]] = null;
+                thisOption[names[0]] = settledOption[names[0]] = null;
             }
             // Otherwise do nothing and use the merge result.
         }, this);
@@ -375,9 +402,9 @@ var DataZoomModel = echarts.extendComponentModel({
     /**
      * @private
      */
-    _setDefaultThrottle: function (rawOption) {
+    _setDefaultThrottle: function (inputRawOption) {
         // When first time user set throttle, auto throttle ends.
-        if (rawOption.hasOwnProperty('throttle')) {
+        if (inputRawOption.hasOwnProperty('throttle')) {
             this._autoThrottle = false;
         }
         if (this._autoThrottle) {
@@ -450,23 +477,42 @@ var DataZoomModel = echarts.extendComponentModel({
      * @param {number} [opt.end]
      * @param {number} [opt.startValue]
      * @param {number} [opt.endValue]
-     * @param {boolean} [ignoreUpdateRangeUsg=false]
      */
-    setRawRange: function (opt, ignoreUpdateRangeUsg) {
-        var option = this.option;
+    setRawRange: function (opt) {
+        var thisOption = this.option;
+        var settledOption = this.settledOption;
         each([['start', 'startValue'], ['end', 'endValue']], function (names) {
-            // If only one of 'start' and 'startValue' is not null/undefined, the other
-            // should be cleared, which enable clear the option.
-            // If both of them are not set, keep option with the original value, which
-            // enable use only set start but not set end when calling `dispatchAction`.
-            // The same as 'end' and 'endValue'.
+            // Consider the pair <start, startValue>:
+            // If one has value and the other one is `null/undefined`, we both set them
+            // to `settledOption`. This strategy enables the feature to clear the original
+            // value in `settledOption` to `null/undefined`.
+            // But if both of them are `null/undefined`, we do not set them to `settledOption`
+            // and keep `settledOption` with the original value. This strategy enables users to
+            // only set <end or endValue> but not set <start or startValue> when calling
+            // `dispatchAction`.
+            // The pair <end, endValue> is treated in the same way.
             if (opt[names[0]] != null || opt[names[1]] != null) {
-                option[names[0]] = opt[names[0]];
-                option[names[1]] = opt[names[1]];
+                thisOption[names[0]] = settledOption[names[0]] = opt[names[0]];
+                thisOption[names[1]] = settledOption[names[1]] = opt[names[1]];
             }
         }, this);
 
-        !ignoreUpdateRangeUsg && updateRangeUse(this, opt);
+        updateRangeUse(this, opt);
+    },
+
+    /**
+     * @public
+     * @param {Object} opt
+     * @param {number} [opt.start]
+     * @param {number} [opt.end]
+     * @param {number} [opt.startValue]
+     * @param {number} [opt.endValue]
+     */
+    setCalculatedRange: function (opt) {
+        var option = this.option;
+        each(['start', 'startValue', 'end', 'endValue'], function (name) {
+            option[name] = opt[name];
+        });
     },
 
     /**
@@ -540,7 +586,12 @@ var DataZoomModel = echarts.extendComponentModel({
 
 });
 
-function retrieveRaw(option) {
+/**
+ * Retrieve the those raw params from option, which will be cached separately.
+ * becasue they will be overwritten by normalized/calculated values in the main
+ * process.
+ */
+function retrieveRawOption(option) {
     var ret = {};
     each(
         ['start', 'end', 'startValue', 'endValue', 'throttle'],
@@ -551,13 +602,13 @@ function retrieveRaw(option) {
     return ret;
 }
 
-function updateRangeUse(dataZoomModel, rawOption) {
+function updateRangeUse(dataZoomModel, inputRawOption) {
     var rangePropMode = dataZoomModel._rangePropMode;
     var rangeModeInOption = dataZoomModel.get('rangeMode');
 
     each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
-        var percentSpecified = rawOption[names[0]] != null;
-        var valueSpecified = rawOption[names[1]] != null;
+        var percentSpecified = inputRawOption[names[0]] != null;
+        var valueSpecified = inputRawOption[names[1]] != null;
         if (percentSpecified && !valueSpecified) {
             rangePropMode[index] = 'percent';
         }
diff --git a/src/component/dataZoom/dataZoomProcessor.js b/src/component/dataZoom/dataZoomProcessor.js
index c006b03..cfd1406 100644
--- a/src/component/dataZoom/dataZoomProcessor.js
+++ b/src/component/dataZoom/dataZoomProcessor.js
@@ -81,12 +81,12 @@ echarts.registerProcessor({
             var percentRange = axisProxy.getDataPercentWindow();
             var valueRange = axisProxy.getDataValueWindow();
 
-            dataZoomModel.setRawRange({
+            dataZoomModel.setCalculatedRange({
                 start: percentRange[0],
                 end: percentRange[1],
                 startValue: valueRange[0],
                 endValue: valueRange[1]
-            }, true);
+            });
         });
     }
 });
diff --git a/test/dataZoom-toolbox.html b/test/dataZoom-toolbox.html
index 6bd96ce..992ab4c 100644
--- a/test/dataZoom-toolbox.html
+++ b/test/dataZoom-toolbox.html
@@ -26,6 +26,7 @@ under the License.
         <script src="lib/config.js"></script>
         <script src="lib/jquery.min.js"></script>
         <script src="lib/facePrint.js"></script>
+        <script src="lib/testHelper.js"></script>
         <meta name="viewport" content="width=device-width, initial-scale=1" />
         <link rel="stylesheet" href="lib/reset.css">
     </head>
@@ -58,6 +59,8 @@ under the License.
         <h2>Specify Y axis (yAxisIndex: false, should be 'lineX' brush)</h2>
         <div class="chart" id="main-specify-x-axis"></div>
 
+        <div id="main0"></div>
+
         <script>
 
             var lastChart;
@@ -684,5 +687,53 @@ under the License.
 
 
 
+
+        <script>
+        require(['echarts'/*, 'map/js/china' */], function (echarts) {
+            var option;
+
+            option = {
+                toolbox: {
+                    feature: {
+                        dataZoom: {
+                            // yAxisIndex: false
+                        }
+                    }
+                },
+                legend: {},
+                xAxis: {
+                    data: ['I', 'II', 'III']
+                },
+                yAxis: {},
+                series: [{
+                    name: 'x',
+                    type: 'bar',
+                    stack: 'a',
+                    data: [11, 22, 33]
+                }, {
+                    name: 'y',
+                    type: 'bar',
+                    stack: 'a',
+                    data: [44, 33, 22]
+                }]
+            };
+
+            var chart = testHelper.create(echarts, 'main0', {
+                title: [
+                    '(1) Use toolbox.dataZoom to brush a small rect in grid.',
+                    '(2) Click a legend item to disappear a series and click again to show the series.',
+                    '[Check]: the extent of yAxis: should be able to return to **the previous state after (1) did**.',
+                    '(3) Click "back" in toolbox.dataZoom',
+                    '[Check]: Back normally.'
+                ],
+                option: option
+                // height: 300,
+                // buttons: [{text: 'btn-txt', onclick: function () {}}],
+                // recordCanvas: true,
+            });
+        });
+        </script>
+
+
     </body>
 </html>
\ No newline at end of file
diff --git a/test/runTest/actions/__meta__.json b/test/runTest/actions/__meta__.json
index 8879082..723bed1 100644
--- a/test/runTest/actions/__meta__.json
+++ b/test/runTest/actions/__meta__.json
@@ -61,7 +61,7 @@
   "dataZoom-scatter-hv-polar": 3,
   "dataZoom-scatter-hv": 1,
   "dataZoom-sync": 1,
-  "dataZoom-toolbox": 5,
+  "dataZoom-toolbox": 6,
   "dataZoomHighPrecision": 3,
   "dynamic-splitNumber": 1,
   "ec-event": 4,
diff --git a/test/runTest/actions/dataZoom-toolbox.json b/test/runTest/actions/dataZoom-toolbox.json
index 3d8e570..ae79937 100644
--- a/test/runTest/actions/dataZoom-toolbox.json
+++ b/test/runTest/actions/dataZoom-toolbox.json
@@ -1 +1 @@
-[{"name":"Action 1","ops":[{"type":"mousedown","time":409,"x":770,"y":435},{"type":"mousemove","time":509,"x":770,"y":434},{"type":"mousemove","time":712,"x":770,"y":252},{"type":"mousemove","time":913,"x":768,"y":150},{"type":"mousemove","time":1115,"x":767,"y":139},{"type":"mouseup","time":1480,"x":767,"y":139},{"time":1481,"delay":1000,"type":"screenshot-auto"},{"type":"mousemove","time":2065,"x":766,"y":139},{"type":"mousemove","time":2265,"x":727,"y":130},{"type":"mousemove","time": [...]
\ No newline at end of file
+[{"name":"Action 1","ops":[{"type":"mousedown","time":409,"x":770,"y":435},{"type":"mousemove","time":509,"x":770,"y":434},{"type":"mousemove","time":712,"x":770,"y":252},{"type":"mousemove","time":913,"x":768,"y":150},{"type":"mousemove","time":1115,"x":767,"y":139},{"type":"mouseup","time":1480,"x":767,"y":139},{"time":1481,"delay":1000,"type":"screenshot-auto"},{"type":"mousemove","time":2065,"x":766,"y":139},{"type":"mousemove","time":2265,"x":727,"y":130},{"type":"mousemove","time": [...]
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org