You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by ov...@apache.org on 2020/12/02 06:52:53 UTC

[incubator-echarts] 01/01: fix(axis): when category axis max is greater than data length

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

ovilia pushed a commit to branch feat-ordinal
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git

commit b8c6c28cc5f558e1d1f6f7b85912e7869a6e4bde
Author: Ovilia <zw...@gmail.com>
AuthorDate: Wed Dec 2 14:52:02 2020 +0800

    fix(axis): when category axis max is greater than data length
---
 src/scale/Ordinal.ts | 23 ++++++++++++++++-----
 test/bar-race.html   | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/src/scale/Ordinal.ts b/src/scale/Ordinal.ts
index 98fbf0f..6ab1edc 100644
--- a/src/scale/Ordinal.ts
+++ b/src/scale/Ordinal.ts
@@ -146,9 +146,17 @@ class OrdinalScale extends Scale<OrdinalScaleSetting> {
      */
     getRawIndex(displayIndex: OrdinalNumber): OrdinalNumber {
         if (this._categorySortInfo.length) {
-            return this._categorySortInfo[displayIndex].ordinalNumber;
+            // Sorted
+            if (this._categorySortInfo[displayIndex] == null) {
+                // Out of range, e.g., when axis max is larger than cagetory number
+                return null;
+            }
+            else {
+                return this._categorySortInfo[displayIndex].ordinalNumber;
+            }
         }
         else {
+            // Not sorted
             return displayIndex;
         }
     }
@@ -159,10 +167,15 @@ class OrdinalScale extends Scale<OrdinalScaleSetting> {
     getLabel(tick: ScaleTick): string {
         if (!this.isBlank()) {
             const rawIndex = this.getRawIndex(tick.value);
-            const cateogry = this._ordinalMeta.categories[rawIndex];
-            // Note that if no data, ordinalMeta.categories is an empty array.
-            // Return empty if it's not exist.
-            return cateogry == null ? '' : cateogry + '';
+            if (rawIndex == null) {
+                return '';
+            }
+            else {
+                const cateogry = this._ordinalMeta.categories[rawIndex];
+                // Note that if no data, ordinalMeta.categories is an empty array.
+                // Return empty if it's not exist.
+                return cateogry == null ? '' : cateogry + '';
+            }
         }
     }
 
diff --git a/test/bar-race.html b/test/bar-race.html
index b4ec39a..bbe0a30 100644
--- a/test/bar-race.html
+++ b/test/bar-race.html
@@ -54,6 +54,9 @@ under the License.
         <div id="main2" class="chart"></div>
 
 
+        <div id="main3" class="chart"></div>
+
+
         <script>
 
         require(
@@ -307,5 +310,59 @@ under the License.
 
         );
         </script>
+
+
+
+        <script>
+
+        require(
+            (testHelper.hasURLParam('en')
+                ? [
+                    'echarts',
+                    // 'echarts/lang/en',
+                ]
+                : [
+                    'echarts'
+                ]
+            ).concat(
+                [
+                    // 'echarts/chart/bar',
+                    // 'echarts/chart/line',
+                    // 'echarts/component/legend',
+                    // 'echarts/component/graphic',
+                    // 'echarts/component/grid',
+                    // 'echarts/component/tooltip',
+                    // 'echarts/component/brush',
+                    // 'echarts/component/toolbox',
+                    // 'echarts/component/title',
+                    // 'zrender/vml/vml'
+                ]
+            ),
+            function (echarts) {
+                var chart = echarts.init(document.getElementById('main3'), null, {
+                });
+                var option = {
+                    title: {
+                        text: 'When yAxis max is larger than yAxis data length, it should not get error'
+                    },
+                    xAxis: {
+                    },
+                    yAxis: {
+                        type: 'category',
+                        data: ['A', 'B', 'C'],
+                        max: 5
+                    },
+                    series: [{
+                        realtimeSort: true,
+                        type: 'bar',
+                        data: [10, 8, 2]
+                    }]
+                };
+
+                chart.setOption(option);
+            }
+
+        );
+        </script>
     </body>
 </html>


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