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 2020/10/14 08:10:14 UTC

[incubator-echarts] 04/08: ts: fix type of converters.

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

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

commit 8b34dfbe1a5e91ee9af06d9b26331aadff409cc7
Author: 100pah <su...@gmail.com>
AuthorDate: Tue Oct 13 11:40:58 2020 +0800

    ts: fix type of converters.
---
 src/coord/CoordinateSystem.ts         |  2 +-
 src/echarts.ts                        | 24 ++++++---
 src/util/types.ts                     | 25 ++++++++--
 test/ut/core/utHelper.ts              |  5 +-
 test/ut/spec/api/containPixel.test.ts | 94 +++++++++++++++++------------------
 test/ut/spec/api/converter.test.ts    | 65 ++++++++++++------------
 6 files changed, 121 insertions(+), 94 deletions(-)

diff --git a/src/coord/CoordinateSystem.ts b/src/coord/CoordinateSystem.ts
index 07ab843..91f7a82 100644
--- a/src/coord/CoordinateSystem.ts
+++ b/src/coord/CoordinateSystem.ts
@@ -71,7 +71,7 @@ export interface CoordinateSystemMaster {
     // null/undefined value.
     convertFromPixel?(
         ecModel: GlobalModel, finder: ParsedModelFinder, pixelValue: number | number[]
-    ): ScaleDataValue | ScaleDataValue[];
+    ): number | number[];
 
     // @param point Point in global pixel coordinate system.
     // The signature of this method should be the same as `CoordinateSystemExecutive`
diff --git a/src/echarts.ts b/src/echarts.ts
index dfbe5a6..8c1bb7e 100644
--- a/src/echarts.ts
+++ b/src/echarts.ts
@@ -88,7 +88,8 @@ import {
     ComponentMainType,
     ComponentSubType,
     ColorString,
-    SelectChangedPayload
+    SelectChangedPayload,
+    ScaleDataValue
 } from './util/types';
 import Displayable from 'zrender/src/graphic/Displayable';
 import IncrementalDisplayable from 'zrender/src/graphic/IncrementalDisplayable';
@@ -237,7 +238,12 @@ let updateMethods: {
     updateVisual: UpdateMethod,
     updateLayout: UpdateMethod
 };
-let doConvertPixel: (ecIns: ECharts, methodName: string, finder: ModelFinder, value: any) => any;
+let doConvertPixel: (
+    ecIns: ECharts,
+    methodName: string,
+    finder: ModelFinder,
+    value: (number | number[]) | (ScaleDataValue | ScaleDataValue[])
+) => (number | number[]);
 let updateStreamModes: (ecIns: ECharts, ecModel: GlobalModel) => void;
 let doDispatchAction: (this: ECharts, payload: Payload, silent: boolean) => void;
 let flushPendingActions: (this: ECharts, silent: boolean) => void;
@@ -779,7 +785,9 @@ class ECharts extends Eventful {
      * Convert from logical coordinate system to pixel coordinate system.
      * See CoordinateSystem#convertToPixel.
      */
-    convertToPixel(finder: ModelFinder, value: any): number[] {
+    convertToPixel(finder: ModelFinder, value: ScaleDataValue): number;
+    convertToPixel(finder: ModelFinder, value: ScaleDataValue[]): number[];
+    convertToPixel(finder: ModelFinder, value: ScaleDataValue | ScaleDataValue[]): number | number[] {
         return doConvertPixel(this, 'convertToPixel', finder, value);
     }
 
@@ -787,7 +795,9 @@ class ECharts extends Eventful {
      * Convert from pixel coordinate system to logical coordinate system.
      * See CoordinateSystem#convertFromPixel.
      */
-    convertFromPixel(finder: ModelFinder, value: number[]): any {
+    convertFromPixel(finder: ModelFinder, value: number): number;
+    convertFromPixel(finder: ModelFinder, value: number[]): number[];
+    convertFromPixel(finder: ModelFinder, value: number | number[]): number | number[] {
         return doConvertPixel(this, 'convertFromPixel', finder, value);
     }
 
@@ -1614,8 +1624,8 @@ class ECharts extends Eventful {
             ecIns: ECharts,
             methodName: 'convertFromPixel' | 'convertToPixel',
             finder: ModelFinder,
-            value: any
-        ): any {
+            value: (number | number[]) | (ScaleDataValue | ScaleDataValue[])
+        ): (number | number[]) {
             if (ecIns._disposed) {
                 disposedWarning(ecIns.id);
                 return;
@@ -1629,7 +1639,7 @@ class ECharts extends Eventful {
             for (let i = 0; i < coordSysList.length; i++) {
                 const coordSys = coordSysList[i];
                 if (coordSys[methodName]
-                    && (result = coordSys[methodName](ecModel, parsedFinder, value)) != null
+                    && (result = coordSys[methodName](ecModel, parsedFinder, value as any)) != null
                 ) {
                     return result;
                 }
diff --git a/src/util/types.ts b/src/util/types.ts
index cc869e6..7ce3e5f 100644
--- a/src/util/types.ts
+++ b/src/util/types.ts
@@ -302,12 +302,27 @@ export type OrdinalSortInfo = {
     ordinalNumber: OrdinalNumber,
     beforeSortIndex: number
 };
-export type ParsedValueNumeric = number | OrdinalNumber;
+
+/**
+ * `OptionDataValue` is the primitive value in `series.data` or `dataset.source`.
+ * `OptionDataValue` are parsed (see `src/data/helper/dataValueHelper.parseDataValue`)
+ * into `ParsedValue` and stored into `data/List` storage.
+ * Note:
+ * (1) The term "parse" does not mean `src/scale/Scale['parse']`.
+ * (2) If a category dimension is not mapped to any axis, its raw value will NOT be
+ * parsed to `OrdinalNumber` but keep the original `OrdinalRawValue` in `src/data/List` storage.
+ */
 export type ParsedValue = ParsedValueNumeric | OrdinalRawValue;
-// FIXME:TS better name?
-// This is not `OptionDataPrimitive` because the "dataProvider parse"
-// will not be performed. But "scale parse" will be performed.
-export type ScaleDataValue = ParsedValue | Date;
+export type ParsedValueNumeric = number | OrdinalNumber;
+/**
+ * `ScaleDataValue` means that the user input primitive value to `src/scale/Scale`.
+ * (For example, used in `axis.min`, `axis.max`, `convertToPixel`).
+ * Note:
+ * `ScaleDataValue` is a little different from `OptionDataValue`, because it will not go through
+ * `src/data/helper/dataValueHelper.parseDataValue`, but go through `src/scale/Scale['parse']`.
+ */
+export type ScaleDataValue = ParsedValueNumeric | OrdinalRawValue | Date;
+
 export interface ScaleTick {
     value: number
 };
diff --git a/test/ut/core/utHelper.ts b/test/ut/core/utHelper.ts
index e485471..509438c 100755
--- a/test/ut/core/utHelper.ts
+++ b/test/ut/core/utHelper.ts
@@ -17,7 +17,10 @@
 * under the License.
 */
 
-import { init, EChartsType } from '../../../src/echarts';
+// PENDING: should resolve that no `.ts` suffix.
+// @ts-ignore
+import { init, EChartsType } from '../../../echarts.all.ts';
+
 import {
     curry as zrUtilCurry,
     bind as zrUtilBind,
diff --git a/test/ut/spec/api/containPixel.test.ts b/test/ut/spec/api/containPixel.test.ts
index c0c03ad..ec99628 100755
--- a/test/ut/spec/api/containPixel.test.ts
+++ b/test/ut/spec/api/containPixel.test.ts
@@ -18,16 +18,18 @@
 * under the License.
 */
 
-/* jshint maxlen:200 */
+import { createChart, removeChart } from '../../core/utHelper';
+import { EChartsType, registerMap } from '../../../../src/echarts';
+import { GeoJSON } from '../../../../src/coord/geo/geoTypes';
+
 
-const echarts = require('../../../../lib/echarts');
-const utHelper = require('../../core/utHelper');
 
 describe('api/containPixel', function () {
-    var testGeoJson1 = {
+    const testGeoJson1: GeoJSON = {
         'type': 'FeatureCollection',
         'features': [
             {
+                'type': 'Feature',
                 'geometry': {
                     'type': 'Polygon',
                     'coordinates': [
@@ -59,10 +61,11 @@ describe('api/containPixel', function () {
         ]
     };
 
-    var testGeoJson2 = {
+    const testGeoJson2: GeoJSON = {
         'type': 'FeatureCollection',
         'features': [
             {
+                'type': 'Feature',
                 'geometry': {
                     'type': 'Polygon',
                     'coordinates': [
@@ -94,26 +97,23 @@ describe('api/containPixel', function () {
         ]
     };
 
-    var chart = '';
-    var createResult = '';
+    let chart: EChartsType;
+
     beforeEach(function () {
-        createResult = utHelper.createChart(context, echarts);
-        chart = createResult.charts[0];
+        chart = createChart({
+            width: 200,
+            height: 150
+        });
     });
 
     afterEach(function () {
-        utHelper.removeChart(createResult);
+        removeChart(chart);
     });
 
 
     it('geo', function () {
-        context.width = 200;
-        context.height = 150;
-        createResult = utHelper.createChart(context, echarts);
-        chart = createResult.charts[0];
-
-        echarts.registerMap('test1', testGeoJson1);
-        echarts.registerMap('test2', testGeoJson2);
+        registerMap('test1', testGeoJson1);
+        registerMap('test2', testGeoJson2);
 
         chart.setOption({
             geo: [
@@ -149,8 +149,8 @@ describe('api/containPixel', function () {
             ]
         });
 
-        var width = chart.getWidth();
-        var height = chart.getWidth();
+        const width = chart.getWidth();
+        const height = chart.getWidth();
 
         expect(chart.containPixel('geo', [15, 30])).toEqual(true);
         expect(chart.containPixel('geo', [9.5, 30])).toEqual(false);
@@ -161,12 +161,8 @@ describe('api/containPixel', function () {
 
 
     it('map', function () {
-        context.width = 200;
-        context.height = 150;
-        createResult = utHelper.createChart(context, echarts);
-        chart = createResult.charts[0];
-        echarts.registerMap('test1', testGeoJson1);
-        echarts.registerMap('test2', testGeoJson2);
+        registerMap('test1', testGeoJson1);
+        registerMap('test2', testGeoJson2);
 
         chart.setOption({
             series: [
@@ -190,7 +186,7 @@ describe('api/containPixel', function () {
             ]
         });
 
-        var width = chart.getWidth();
+        const width = chart.getWidth();
 
         expect(chart.containPixel('series', [15, 30])).toEqual(true);
         expect(chart.containPixel('series', [9.5, 30])).toEqual(false);
@@ -200,7 +196,7 @@ describe('api/containPixel', function () {
 
 
     it('cartesian', function () {
-        echarts.registerMap('test1', testGeoJson1);
+        registerMap('test1', testGeoJson1);
 
         chart.setOption({
             geo: [ // Should not affect grid converter.
@@ -268,38 +264,38 @@ describe('api/containPixel', function () {
                 {
                     id: 'k1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[1000, 700]]
                 },
                 {
                     id: 'k2',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[100, 800]]
                 },
                 {
                     id: 'j1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[100, 800]],
                     xAxisIndex: 1
                 },
                 {
                     id: 'i1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [],
                     xAxisId: 'x2',
                     yAxisId: 'y1'
@@ -307,7 +303,7 @@ describe('api/containPixel', function () {
             ]
         });
 
-        var width = chart.getWidth();
+        const width = chart.getWidth();
 
         expect(chart.containPixel('grid', [15, 30])).toEqual(true);
         expect(chart.containPixel('grid', [9.5, 30])).toEqual(false);
@@ -336,7 +332,7 @@ describe('api/containPixel', function () {
     //         ]
     //     });
 
-    //     var height = chart.getHeight();
+    //     const height = chart.getHeight();
 
     //     expect(chart.containPixel('series', [40, height / 2])).toEqual(false);
     //     expect(chart.containPixel('series', [40, height / 2 + 10])).toEqual(true);
@@ -345,8 +341,8 @@ describe('api/containPixel', function () {
 
 
     // it('pieAndGeo', function () {
-    //     echarts.registerMap('test1', testGeoJson1);
-    //     echarts.registerMap('test2', testGeoJson2);
+    //     registerMap('test1', testGeoJson1);
+    //     registerMap('test2', testGeoJson2);
 
     //     chart.setOption({
     //         geo: [
@@ -394,7 +390,7 @@ describe('api/containPixel', function () {
 
 
     it('graph', function () {
-        echarts.registerMap('test1', testGeoJson1);
+        registerMap('test1', testGeoJson1);
 
         chart.setOption({
             geo: [ // Should not affect graph converter.
@@ -426,4 +422,4 @@ describe('api/containPixel', function () {
     });
 
 
-});
\ No newline at end of file
+});
diff --git a/test/ut/spec/api/converter.test.ts b/test/ut/spec/api/converter.test.ts
index 946dd61..4183ab3 100755
--- a/test/ut/spec/api/converter.test.ts
+++ b/test/ut/spec/api/converter.test.ts
@@ -18,13 +18,14 @@
 * under the License.
 */
 
-/* jshint maxlen:200 */
-var utHelper = require('../../core/utHelper');
-var echarts = require('../../../../index');
+
+import { EChartsType, registerMap } from '../../../../src/echarts';
+import { GeoJSON } from '../../../../src/coord/geo/geoTypes';
+
 
 describe('api/converter', function () {
 
-    var DELTA = 1E-3;
+    const DELTA = 1E-3;
 
     function pointEquals(p1, p2) {
         if (p1 instanceof Array && p2 instanceof Array) {
@@ -35,10 +36,11 @@ describe('api/converter', function () {
         }
     }
 
-    var testGeoJson1 = {
+    const testGeoJson1: GeoJSON = {
         'type': 'FeatureCollection',
         'features': [
             {
+                'type': 'Feature',
                 'geometry': {
                     'type': 'Polygon',
                     'coordinates': [
@@ -70,10 +72,11 @@ describe('api/converter', function () {
         ]
     };
 
-    var testGeoJson2 = {
+    const testGeoJson2: GeoJSON = {
         'type': 'FeatureCollection',
         'features': [
             {
+                'type': 'Feature',
                 'geometry': {
                     'type': 'Polygon',
                     'coordinates': [
@@ -104,10 +107,10 @@ describe('api/converter', function () {
             }
         ]
     };
-    echarts.registerMap('converter_test_geo_1', testGeoJson1);
-    echarts.registerMap('converter_test_geo_2', testGeoJson2);
+    registerMap('converter_test_geo_1', testGeoJson1);
+    registerMap('converter_test_geo_2', testGeoJson2);
 
-    var chart;
+    let chart: EChartsType;
     beforeEach(function () {
         chart = utHelper.createChart();
     });
@@ -153,8 +156,8 @@ describe('api/converter', function () {
             ]
         });
 
-        var width = chart.getWidth();
-        var height = chart.getHeight();
+        const width = chart.getWidth();
+        const height = chart.getHeight();
 
         expect(pointEquals(chart.convertToPixel('geo', [5000, 3000]), [width - 20, height - 40])).toEqual(true);
         expect(pointEquals(chart.convertFromPixel('geo', [width - 20, height - 40]), [5000, 3000])).toEqual(true);
@@ -283,38 +286,38 @@ describe('api/converter', function () {
                 {
                     id: 'k1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[1000, 700]]
                 },
                 {
                     id: 'k2',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[100, 800]]
                 },
                 {
                     id: 'j1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [[100, 800]],
                     xAxisIndex: 1
                 },
                 {
                     id: 'i1',
                     type: 'scatter',
-                    left: 0,
-                    right: 0,
-                    top: 0,
-                    bottom: 0,
+                    // left: 0,
+                    // right: 0,
+                    // top: 0,
+                    // bottom: 0,
                     data: [],
                     xAxisId: 'x2',
                     yAxisId: 'y1'
@@ -322,8 +325,8 @@ describe('api/converter', function () {
             ]
         });
 
-        var width = chart.getWidth();
-        var height = chart.getHeight();
+        const width = chart.getWidth();
+        const height = chart.getHeight();
 
         expect(pointEquals(chart.convertToPixel({seriesIndex: 1}, [-500, 6000]), [10, height - 40])).toEqual(true);
         expect(pointEquals(chart.convertFromPixel({seriesIndex: 1}, [10, height - 40]), [-500, 6000])).toEqual(true);
@@ -386,8 +389,8 @@ describe('api/converter', function () {
             ]
         });
 
-        var width = chart.getWidth();
-        var height = chart.getHeight();
+        const width = chart.getWidth();
+        const height = chart.getHeight();
 
         expect(pointEquals(chart.convertToPixel({seriesIndex: 0}, [2000, 3500]), [10 + (width - 30) / 2, 30 + (height - 70) / 2])).toEqual(true);
         expect(pointEquals(chart.convertFromPixel({seriesIndex: 0}, [10 + (width - 30) / 2, 30 + (height - 70) / 2]), [2000, 3500])).toEqual(true);


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