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/02/25 21:54:24 UTC

[incubator-echarts] branch typescript updated: add axis model creator type

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

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


The following commit(s) were added to refs/heads/typescript by this push:
     new 6b2ec74  add axis model creator type
6b2ec74 is described below

commit 6b2ec74a9879617ef6198d79cf0145e9345af740
Author: 100pah <su...@gmail.com>
AuthorDate: Wed Feb 26 05:50:44 2020 +0800

    add axis model creator type
---
 src/component/gridSimple.ts                 |  1 +
 src/component/legend.ts                     |  4 +-
 src/component/tooltip/TooltipHTMLContent.ts |  4 +-
 src/component/visualMap/typeDefaulter.ts    |  4 +-
 src/coord/CoordinateSystem.ts               |  2 +-
 src/coord/axisCommonTypes.ts                |  4 +-
 src/coord/axisDefault.ts                    |  8 +--
 src/coord/axisModelCreator.ts               | 95 +++++++++++++++--------------
 src/coord/cartesian/AxisModel.ts            | 10 +--
 src/coord/cartesian/GridModel.ts            |  7 ++-
 src/coord/parallel/AxisModel.ts             |  6 +-
 src/coord/polar/AxisModel.ts                |  9 +--
 src/coord/radar/RadarModel.ts               |  2 +-
 src/coord/single/AxisModel.ts               |  6 +-
 src/helper.ts                               | 12 +++-
 src/model/Component.ts                      |  2 +
 16 files changed, 89 insertions(+), 87 deletions(-)

diff --git a/src/component/gridSimple.ts b/src/component/gridSimple.ts
index a19392f..3e25269 100644
--- a/src/component/gridSimple.ts
+++ b/src/component/gridSimple.ts
@@ -24,6 +24,7 @@ import * as zrUtil from 'zrender/src/core/util';
 import * as graphic from '../util/graphic';
 
 import '../coord/cartesian/Grid';
+import '../coord/cartesian/GridModel';
 import './axis';
 
 // Grid view
diff --git a/src/component/legend.ts b/src/component/legend.ts
index 28d35be..b5d10c9 100644
--- a/src/component/legend.ts
+++ b/src/component/legend.ts
@@ -26,12 +26,12 @@ import './legend/legendAction';
 import './legend/LegendView';
 
 import legendFilter from './legend/legendFilter';
-import Component, { ComponentModelConstructor } from '../model/Component';
+import Component from '../model/Component';
 
 // Series Filter
 echarts.registerProcessor(echarts.PRIORITY.PROCESSOR.SERIES_FILTER, legendFilter);
 
-(Component as ComponentModelConstructor).registerSubTypeDefaulter('legend', function () {
+Component.registerSubTypeDefaulter('legend', function () {
     // Default 'plain' when no type specified.
     return 'plain';
 });
diff --git a/src/component/tooltip/TooltipHTMLContent.ts b/src/component/tooltip/TooltipHTMLContent.ts
index 879fdcc..e88f773 100644
--- a/src/component/tooltip/TooltipHTMLContent.ts
+++ b/src/component/tooltip/TooltipHTMLContent.ts
@@ -28,6 +28,8 @@ import { ZRenderType } from 'zrender/src/zrender';
 import { TooltipOption } from './TooltipModel';
 import Model from '../../model/Model';
 import { ZRRawEvent } from 'zrender/src/core/types';
+import CanvasPainter from 'zrender/src/canvas/Painter';
+import SVGPainter from 'zrender/src/svg/Painter';
 
 var each = zrUtil.each;
 var toCamelCase = formatUtil.toCamelCase;
@@ -134,7 +136,7 @@ function makeStyleCoord(out: number[], zr: ZRenderType, appendToBody: boolean, z
         // xy should be based on canvas root. But tooltipContent is
         // the sibling of canvas root. So padding of ec container
         // should be considered here.
-        var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset();
+        var viewportRootOffset = zrPainter && (zrPainter as CanvasPainter | SVGPainter).getViewportRootOffset();
         if (viewportRootOffset) {
             out[0] += viewportRootOffset.offsetLeft;
             out[1] += viewportRootOffset.offsetTop;
diff --git a/src/component/visualMap/typeDefaulter.ts b/src/component/visualMap/typeDefaulter.ts
index 0576b92..6331c55 100644
--- a/src/component/visualMap/typeDefaulter.ts
+++ b/src/component/visualMap/typeDefaulter.ts
@@ -17,12 +17,12 @@
 * under the License.
 */
 
-import Component, { ComponentModelConstructor } from '../../model/Component';
+import Component from '../../model/Component';
 import {VisualMapOption} from './VisualMapModel';
 import {PiecewiseVisualMapOption} from './PiecewiseModel';
 import {ContinousVisualMapOption} from './ContinuousModel';
 
-(Component as ComponentModelConstructor).registerSubTypeDefaulter(
+Component.registerSubTypeDefaulter(
     'visualMap', function (option: VisualMapOption) {
     // Compatible with ec2, when splitNumber === 0, continuous visualMap will be used.
     return (
diff --git a/src/coord/CoordinateSystem.ts b/src/coord/CoordinateSystem.ts
index fbb62d8..18daac8 100644
--- a/src/coord/CoordinateSystem.ts
+++ b/src/coord/CoordinateSystem.ts
@@ -52,7 +52,7 @@ export interface CoordinateSystemMaster {
 
     model?: Model;
 
-    update(ecModel: GlobalModel, api: ExtensionAPI): void;
+    update?: (ecModel: GlobalModel, api: ExtensionAPI) => void;
 
     // This methods is also responsible for determine whether this
     // coodinate system is applicable to the given `finder`.
diff --git a/src/coord/axisCommonTypes.ts b/src/coord/axisCommonTypes.ts
index 6d91988..b20355c 100644
--- a/src/coord/axisCommonTypes.ts
+++ b/src/coord/axisCommonTypes.ts
@@ -24,7 +24,9 @@ import {
 import { Dictionary } from 'zrender/src/core/types';
 
 
-export type OptionAxisType = 'category' | 'value' | 'time' | 'log';
+export var AXIS_TYPES = {value: 1, category: 1, time: 1, log: 1} as const;
+export type OptionAxisType = keyof typeof AXIS_TYPES;
+
 
 export interface AxisBaseOption extends ComponentOption {
     type?: OptionAxisType;
diff --git a/src/coord/axisDefault.ts b/src/coord/axisDefault.ts
index 7e64418..f477ee3 100644
--- a/src/coord/axisDefault.ts
+++ b/src/coord/axisDefault.ts
@@ -174,8 +174,8 @@ var logAxis: AxisBaseOption = zrUtil.defaults({
 
 
 export default {
-    categoryAxis: categoryAxis,
-    valueAxis: valueAxis,
-    timeAxis: timeAxis,
-    logAxis: logAxis
+    category: categoryAxis,
+    value: valueAxis,
+    time: timeAxis,
+    log: logAxis
 };
diff --git a/src/coord/axisModelCreator.ts b/src/coord/axisModelCreator.ts
index 978394c..fa62412 100644
--- a/src/coord/axisModelCreator.ts
+++ b/src/coord/axisModelCreator.ts
@@ -17,8 +17,6 @@
 * under the License.
 */
 
-// @ts-nocheck
-
 import * as zrUtil from 'zrender/src/core/util';
 import axisDefault from './axisDefault';
 import ComponentModel from '../model/Component';
@@ -28,95 +26,100 @@ import {
     fetchLayoutMode
 } from '../util/layout';
 import OrdinalMeta from '../data/OrdinalMeta';
-import { DimensionName, ComponentOption } from '../util/types';
-import { OptionAxisType } from './axisCommonTypes';
+import { DimensionName, BoxLayoutOptionMixin, OrdinalRawValue } from '../util/types';
+import { AxisBaseOption, AXIS_TYPES } from './axisCommonTypes';
+import GlobalModel from '../model/Global';
 
 
-// FIXME axisType is fixed ?
-var AXIS_TYPES = ['value', 'category', 'time', 'log'];
+type Constructor<T> = new (...args: any[]) => T;
 
 /**
  * Generate sub axis model class
- * @param axisName 'x' 'y' 'radius' 'angle' 'parallel'
- * @param {module:echarts/model/Component} BaseAxisModelClass
- * @param {Function} axisTypeDefaulter
- * @param {Object} [extraDefaultOption]
+ * @param axisName 'x' 'y' 'radius' 'angle' 'parallel' ...
  */
-export default function (
+export default function <
+    AxisOptionT extends AxisBaseOption,
+    AxisModelCtor extends Constructor<ComponentModel<AxisOptionT>>
+>(
     axisName: DimensionName,
-    BaseAxisModelClass,
-    axisTypeDefaulter: (axisDim: DimensionName, option: ComponentOption) => OptionAxisType,
-    extraDefaultOption?: ComponentOption
+    BaseAxisModelClass: AxisModelCtor,
+    extraDefaultOption?: AxisOptionT
 ) {
 
-    zrUtil.each(AXIS_TYPES, function (axisType) {
+    zrUtil.each(AXIS_TYPES, function (v, axisType) {
 
-        BaseAxisModelClass.extend({
+        var defaultOption = zrUtil.merge(
+            zrUtil.merge({}, axisDefault[axisType], true),
+            extraDefaultOption, true
+        );
 
-            /**
-             * @readOnly
-             */
-            type: axisName + 'Axis.' + axisType,
+        class AxisModel extends BaseAxisModelClass {
+
+            static type = axisName + 'Axis.' + axisType;
+            type = axisName + 'Axis.' + axisType;
+
+            static defaultOption = defaultOption;
+
+            private __ordinalMeta: OrdinalMeta;
 
-            mergeDefaultAndTheme: function (option, ecModel) {
+            constructor(...args: any[]) {
+                super(...args);
+            }
+
+            mergeDefaultAndTheme(option: AxisOptionT, ecModel: GlobalModel): void {
                 var layoutMode = fetchLayoutMode(this);
                 var inputPositionParams = layoutMode
-                    ? getLayoutParams(option) : {};
+                    ? getLayoutParams(option as BoxLayoutOptionMixin) : {};
 
                 var themeModel = ecModel.getTheme();
                 zrUtil.merge(option, themeModel.get(axisType + 'Axis'));
                 zrUtil.merge(option, this.getDefaultOption());
 
-                option.type = axisTypeDefaulter(axisName, option);
+                option.type = getAxisType(option);
 
                 if (layoutMode) {
-                    mergeLayoutParam(option, inputPositionParams, layoutMode);
+                    mergeLayoutParam(option as BoxLayoutOptionMixin, inputPositionParams, layoutMode);
                 }
-            },
+            }
 
-            /**
-             * @override
-             */
-            optionUpdated: function () {
+            optionUpdated(): void {
                 var thisOption = this.option;
                 if (thisOption.type === 'category') {
                     this.__ordinalMeta = OrdinalMeta.createByAxisModel(this);
                 }
-            },
+            }
 
             /**
              * Should not be called before all of 'getInitailData' finished.
              * Because categories are collected during initializing data.
              */
-            getCategories: function (rawData) {
+            getCategories(rawData: boolean): OrdinalRawValue[] | AxisBaseOption['data'] {
                 var option = this.option;
                 // FIXME
                 // warning if called before all of 'getInitailData' finished.
                 if (option.type === 'category') {
                     if (rawData) {
-                        return option.data;
+                        return option.data as AxisBaseOption['data'];
                     }
                     return this.__ordinalMeta.categories;
                 }
-            },
+            }
 
-            getOrdinalMeta: function () {
+            getOrdinalMeta(): OrdinalMeta {
                 return this.__ordinalMeta;
-            },
-
-            defaultOption: zrUtil.mergeAll(
-                [
-                    {},
-                    axisDefault[axisType + 'Axis'],
-                    extraDefaultOption
-                ],
-                true
-            )
-        });
+            }
+        }
+
+        ComponentModel.registerClass(AxisModel);
     });
 
     ComponentModel.registerSubTypeDefaulter(
         axisName + 'Axis',
-        zrUtil.curry(axisTypeDefaulter, axisName)
+        getAxisType
     );
 }
+
+function getAxisType(option: AxisBaseOption) {
+    // Default axis with data is category axis
+    return option.type || (option.data ? 'category' : 'value');
+}
diff --git a/src/coord/cartesian/AxisModel.ts b/src/coord/cartesian/AxisModel.ts
index d038337..890a51c 100644
--- a/src/coord/cartesian/AxisModel.ts
+++ b/src/coord/cartesian/AxisModel.ts
@@ -22,7 +22,6 @@ import ComponentModel from '../../model/Component';
 import axisModelCreator from '../axisModelCreator';
 import {AxisModelCommonMixin} from '../axisModelCommonMixin';
 import Axis2D from './Axis2D';
-import { DimensionName } from '../../util/types';
 import { AxisBaseOption } from '../axisCommonTypes';
 import GridModel from './GridModel';
 
@@ -69,11 +68,6 @@ class AxisModel extends ComponentModel<CartesianAxisOption> {
 
 ComponentModel.registerClass(AxisModel);
 
-function getAxisType(axisDim: DimensionName, option: CartesianAxisOption) {
-    // Default axis with data is category axis
-    return option.type || (option.data ? 'category' : 'value');
-}
-
 interface AxisModel extends AxisModelCommonMixin<CartesianAxisOption> {}
 zrUtil.mixin(AxisModel, AxisModelCommonMixin);
 
@@ -83,7 +77,7 @@ var extraOption: CartesianAxisOption = {
     offset: 0
 };
 
-axisModelCreator('x', AxisModel, getAxisType, extraOption);
-axisModelCreator('y', AxisModel, getAxisType, extraOption);
+axisModelCreator<CartesianAxisOption, typeof AxisModel>('x', AxisModel, extraOption);
+axisModelCreator<CartesianAxisOption, typeof AxisModel>('y', AxisModel, extraOption);
 
 export default AxisModel;
diff --git a/src/coord/cartesian/GridModel.ts b/src/coord/cartesian/GridModel.ts
index 1a31062..e57ce5d 100644
--- a/src/coord/cartesian/GridModel.ts
+++ b/src/coord/cartesian/GridModel.ts
@@ -37,7 +37,7 @@ interface GridOption extends ComponentOption, BoxLayoutOptionMixin, ShadowOption
     tooltop?: any; // FIXME:TS add this tooltip type
 }
 
-export default class extends ComponentModel<GridOption> implements CoordinateSystemHostModel {
+class GridModel extends ComponentModel<GridOption> implements CoordinateSystemHostModel {
 
     static type = 'grid';
 
@@ -63,5 +63,8 @@ export default class extends ComponentModel<GridOption> implements CoordinateSys
         borderWidth: 1,
         borderColor: '#ccc'
     }
+}
+
+ComponentModel.registerClass(GridModel);
 
-}
\ No newline at end of file
+export default GridModel;
diff --git a/src/coord/parallel/AxisModel.ts b/src/coord/parallel/AxisModel.ts
index 1cfe639..da707a9 100644
--- a/src/coord/parallel/AxisModel.ts
+++ b/src/coord/parallel/AxisModel.ts
@@ -142,10 +142,6 @@ var defaultOption = {
 
 zrUtil.mixin(AxisModel, AxisModelCommonMixin);
 
-function getAxisType(axisName, option) {
-    return option.type || (option.data ? 'category' : 'value');
-}
-
-axisModelCreator('parallel', AxisModel, getAxisType, defaultOption);
+axisModelCreator('parallel', AxisModel, defaultOption);
 
 export default AxisModel;
\ No newline at end of file
diff --git a/src/coord/polar/AxisModel.ts b/src/coord/polar/AxisModel.ts
index a6f744d..7169112 100644
--- a/src/coord/polar/AxisModel.ts
+++ b/src/coord/polar/AxisModel.ts
@@ -71,10 +71,5 @@ var polarAxisDefaultExtendedOption = {
     }
 };
 
-function getAxisType(axisDim, option) {
-    // Default axis with data is category axis
-    return option.type || (option.data ? 'category' : 'value');
-}
-
-axisModelCreator('angle', PolarAxisModel, getAxisType, polarAxisDefaultExtendedOption.angle);
-axisModelCreator('radius', PolarAxisModel, getAxisType, polarAxisDefaultExtendedOption.radius);
+axisModelCreator('angle', PolarAxisModel, polarAxisDefaultExtendedOption.angle);
+axisModelCreator('radius', PolarAxisModel, polarAxisDefaultExtendedOption.radius);
diff --git a/src/coord/radar/RadarModel.ts b/src/coord/radar/RadarModel.ts
index 9b0fe35..de7b215 100644
--- a/src/coord/radar/RadarModel.ts
+++ b/src/coord/radar/RadarModel.ts
@@ -25,7 +25,7 @@ import axisDefault from '../axisDefault';
 import Model from '../../model/Model';
 import {AxisModelCommonMixin} from '../axisModelCommonMixin';
 
-var valueAxisDefault = axisDefault.valueAxis;
+var valueAxisDefault = axisDefault.value;
 
 function defaultsShow(opt, show) {
     return zrUtil.defaults({
diff --git a/src/coord/single/AxisModel.ts b/src/coord/single/AxisModel.ts
index b8b36b5..75b235c 100644
--- a/src/coord/single/AxisModel.ts
+++ b/src/coord/single/AxisModel.ts
@@ -99,12 +99,8 @@ var defaultOption = {
     }
 };
 
-function getAxisType(axisName, option) {
-    return option.type || (option.data ? 'category' : 'value');
-}
-
 zrUtil.mixin(AxisModel, {AxisModelCommonMixin});
 
-axisModelCreator('single', AxisModel, getAxisType, defaultOption);
+axisModelCreator('single', AxisModel, defaultOption);
 
 export default AxisModel;
\ No newline at end of file
diff --git a/src/helper.ts b/src/helper.ts
index d1d2766..1d03dfc 100644
--- a/src/helper.ts
+++ b/src/helper.ts
@@ -76,13 +76,21 @@ export {createSymbol} from './util/symbol';
 /**
  * Create scale
  * @param {Array.<number>} dataExtent
- * @param {Object|module:echarts/Model} option
+ * @param {Object|module:echarts/Model} option If `optoin.type`
+ *        is secified, it can only be `'value'` currently.
  */
 export function createScale(dataExtent, option) {
     var axisModel = option;
     if (!Model.isInstance(option)) {
         axisModel = new Model(option);
-        zrUtil.mixin(axisModel, AxisModelCommonMixin);
+        // FIXME
+        // Currently AxisModelCommonMixin has nothing to do with the
+        // the requirements of `axisHelper.createScaleByModel`. For
+        // example the method `getCategories` and `getOrdinalMeta`
+        // are required for `'category'` axis, and ecModel are required
+        // for `'time'` axis. But occationally echarts-gl happened
+        // to only use `'value'` axis.
+        // zrUtil.mixin(axisModel, AxisModelCommonMixin);
     }
 
     var scale = axisHelper.createScaleByModel(axisModel);
diff --git a/src/model/Component.ts b/src/model/Component.ts
index 73c2ed7..9c1ca02 100644
--- a/src/model/Component.ts
+++ b/src/model/Component.ts
@@ -286,6 +286,8 @@ class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Mode
     }
 
     static registerClass: ClassManager['registerClass'];
+
+    static registerSubTypeDefaulter: componentUtil.SubTypeDefaulterManager['registerSubTypeDefaulter'];
 }
 
 // Reset ComponentModel.extend, add preConstruct.


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