From edd13a2671a0e92389caae34dad19e3bcfc73ebf Mon Sep 17 00:00:00 2001 From: pissang Date: Wed, 26 Feb 2020 22:35:11 +0800 Subject: [PATCH] ts: add types for singleAxis --- src/component/toolbox/feature/DataView.ts | 7 +- src/coord/cartesian/Axis2D.ts | 26 ++-- src/coord/polar/Polar.ts | 8 +- src/coord/single/AxisModel.ts | 140 +++++++++--------- src/coord/single/Single.ts | 167 +++++++++------------- src/coord/single/SingleAxis.ts | 115 ++++++--------- src/coord/single/prepareCustom.ts | 9 +- src/coord/single/singleAxisHelper.ts | 47 +++--- src/coord/single/singleCreator.ts | 24 ++-- 9 files changed, 236 insertions(+), 307 deletions(-) diff --git a/src/component/toolbox/feature/DataView.ts b/src/component/toolbox/feature/DataView.ts index 2e4f4bc5b..a0041d227 100644 --- a/src/component/toolbox/feature/DataView.ts +++ b/src/component/toolbox/feature/DataView.ts @@ -27,6 +27,7 @@ import { ColorString, ECUnitOption, SeriesOption, Payload, Dictionary } from '.. import ExtensionAPI from '../../../ExtensionAPI'; import { addEventListener } from 'zrender/src/core/event'; import Axis from '../../../coord/Axis'; +import Cartesian2D from '../../../coord/cartesian/Cartesian2D'; var dataViewLang = lang.toolbox.dataView; @@ -70,10 +71,9 @@ function groupSeries(ecModel: GlobalModel) { var coordSys = seriesModel.coordinateSystem; if (coordSys && (coordSys.type === 'cartesian2d' || coordSys.type === 'polar')) { - var baseAxis = coordSys.getBaseAxis(); - // @ts-ignore TODO Polar + // TODO: TYPE Consider polar? Include polar may increase unecessary bundle size. + var baseAxis = (coordSys as Cartesian2D).getBaseAxis(); if (baseAxis.type === 'category') { - // @ts-ignore TODO Polar var key = baseAxis.dim + '_' + baseAxis.index; if (!seriesGroupByCategoryAxis[key]) { seriesGroupByCategoryAxis[key] = { @@ -83,7 +83,6 @@ function groupSeries(ecModel: GlobalModel) { }; meta.push({ axisDim: baseAxis.dim, - // @ts-ignore TODO Polar axisIndex: baseAxis.index }); } diff --git a/src/coord/cartesian/Axis2D.ts b/src/coord/cartesian/Axis2D.ts index 8abf3bbcc..9214d7401 100644 --- a/src/coord/cartesian/Axis2D.ts +++ b/src/coord/cartesian/Axis2D.ts @@ -25,6 +25,19 @@ import Grid from './Grid'; import { OptionAxisType } from '../axisCommonTypes'; +interface Axis2D { + /** + * Transform global coord to local coord, + * i.e. var localCoord = axis.toLocalCoord(80); + */ + toLocalCoord(coord: number): number; + + /** + * Transform global coord to local coord, + * i.e. var globalCoord = axis.toLocalCoord(40); + */ + toGlobalCoord(coord: number): number; +} class Axis2D extends Axis { /** @@ -106,19 +119,6 @@ class Axis2D extends Axis { return this.coordToData(this.toLocalCoord(point[this.dim === 'x' ? 0 : 1]), clamp); } - /** - * Transform global coord to local coord, - * i.e. var localCoord = axis.toLocalCoord(80); - * designate by module:echarts/coord/cartesian/Grid. - */ - toLocalCoord: (coord: number) => number; - - /** - * Transform global coord to local coord, - * i.e. var globalCoord = axis.toLocalCoord(40); - * designate by module:echarts/coord/cartesian/Grid. - */ - toGlobalCoord: (coord: number) => number; } diff --git a/src/coord/polar/Polar.ts b/src/coord/polar/Polar.ts index cf1fa87b7..4b7e3af8d 100644 --- a/src/coord/polar/Polar.ts +++ b/src/coord/polar/Polar.ts @@ -17,10 +17,6 @@ * under the License. */ -/** - * @module echarts/coord/polar/Polar - */ - import RadiusAxis from './RadiusAxis'; import AngleAxis from './AngleAxis'; import PolarModel from './PolarModel'; @@ -241,13 +237,13 @@ class Polar implements CoordinateSystem, CoordinateSystemMaster { convertToPixel(ecModel: GlobalModel, finder: ParsedModelFinder, value: ScaleDataValue[]) { const seriesModel = finder.seriesModel; - const coordSys = seriesModel ? seriesModel.coordinateSystem : null; + const coordSys = seriesModel && seriesModel.coordinateSystem; return coordSys === this ? this.dataToPoint(value) : null; } convertFromPixel(ecModel: GlobalModel, finder: ParsedModelFinder, pixel: number[]) { const seriesModel = finder.seriesModel; - const coordSys = seriesModel ? seriesModel.coordinateSystem : null; + const coordSys = seriesModel && seriesModel.coordinateSystem; return coordSys === this ? this.pointToData(pixel) : null; } } diff --git a/src/coord/single/AxisModel.ts b/src/coord/single/AxisModel.ts index 75b235cd1..7f302d0a0 100644 --- a/src/coord/single/AxisModel.ts +++ b/src/coord/single/AxisModel.ts @@ -17,90 +17,92 @@ * under the License. */ -// @ts-nocheck - import * as zrUtil from 'zrender/src/core/util'; import ComponentModel from '../../model/Component'; import axisModelCreator from '../axisModelCreator'; import {AxisModelCommonMixin} from '../axisModelCommonMixin'; +import Single from './Single'; +import SingleAxis from './SingleAxis'; +import { AxisBaseOption } from '../axisCommonTypes'; +import { BoxLayoutOptionMixin, LayoutOrient } from '../../util/types'; + +export type SingleAxisPosition = 'top' | 'bottom' | 'left' | 'right' -var AxisModel = ComponentModel.extend({ +export interface SingleAxisOption extends AxisBaseOption, BoxLayoutOptionMixin { + position?: SingleAxisPosition + orient: LayoutOrient +} - type: 'singleAxis', +class SingleAxisModel extends ComponentModel { + static type = 'singleAxis' + type = SingleAxisModel.type - layoutMode: 'box', + static readonly layoutMode = 'box' - /** - * @type {module:echarts/coord/single/SingleAxis} - */ - axis: null, + axis: SingleAxis - /** - * @type {module:echarts/coord/single/Single} - */ - coordinateSystem: null, + coordinateSystem: Single - /** - * @override - */ - getCoordSysModel: function () { + getCoordSysModel() { return this; } -}); - -var defaultOption = { - - left: '5%', - top: '5%', - right: '5%', - bottom: '5%', - - type: 'value', - - position: 'bottom', - - orient: 'horizontal', - - axisLine: { - show: true, - lineStyle: { - width: 1, - type: 'solid' - } - }, - - // Single coordinate system and single axis is the, - // which is used as the parent tooltip model. - // same model, so we set default tooltip show as true. - tooltip: { - show: true - }, - - axisTick: { - show: true, - length: 6, - lineStyle: { - width: 1 - } - }, - - axisLabel: { - show: true, - interval: 'auto' - }, - - splitLine: { - show: true, - lineStyle: { - type: 'dashed', - opacity: 0.2 + static defaultOption: SingleAxisOption = { + + left: '5%', + top: '5%', + right: '5%', + bottom: '5%', + + type: 'value', + + position: 'bottom', + + orient: 'horizontal', + + axisLine: { + show: true, + lineStyle: { + width: 1, + type: 'solid' + } + }, + + // Single coordinate system and single axis is the, + // which is used as the parent tooltip model. + // same model, so we set default tooltip show as true. + tooltip: { + show: true + }, + + axisTick: { + show: true, + length: 6, + lineStyle: { + width: 1 + } + }, + + axisLabel: { + show: true, + interval: 'auto' + }, + + splitLine: { + show: true, + lineStyle: { + type: 'dashed', + opacity: 0.2 + } } } -}; +} + +ComponentModel.registerClass(SingleAxisModel); -zrUtil.mixin(AxisModel, {AxisModelCommonMixin}); +interface SingleAxisModel extends AxisModelCommonMixin {} +zrUtil.mixin(SingleAxisModel, {AxisModelCommonMixin}); -axisModelCreator('single', AxisModel, defaultOption); +axisModelCreator('single', SingleAxisModel, SingleAxisModel.defaultOption); -export default AxisModel; \ No newline at end of file +export default SingleAxisModel; \ No newline at end of file diff --git a/src/coord/single/Single.ts b/src/coord/single/Single.ts index 4df9773b6..35457be9f 100644 --- a/src/coord/single/Single.ts +++ b/src/coord/single/Single.ts @@ -17,8 +17,6 @@ * under the License. */ -// @ts-nocheck - /** * Single coordinates system. */ @@ -27,67 +25,48 @@ import SingleAxis from './SingleAxis'; import * as axisHelper from '../axisHelper'; import {getLayoutRect} from '../../util/layout'; import {each} from 'zrender/src/core/util'; +import { CoordinateSystem, CoordinateSystemMaster } from '../CoordinateSystem'; +import GlobalModel from '../../model/Global'; +import ExtensionAPI from '../../ExtensionAPI'; +import BoundingRect from 'zrender/src/core/BoundingRect'; +import SingleAxisModel from './AxisModel'; +import { ParsedModelFinder } from '../../util/model'; +import { ScaleDataValue } from '../../util/types'; /** * Create a single coordinates system. - * - * @param {module:echarts/coord/single/AxisModel} axisModel - * @param {module:echarts/model/Global} ecModel - * @param {module:echarts/ExtensionAPI} api */ -function Single(axisModel, ecModel, api) { +class Single implements CoordinateSystem, CoordinateSystemMaster { - /** - * @type {string} - * @readOnly - */ - this.dimension = 'single'; + readonly type = 'single'; + readonly dimension = 'single'; /** * Add it just for draw tooltip. - * - * @type {Array.} - * @readOnly */ - this.dimensions = ['single']; + readonly dimensions = ['single']; - /** - * @private - * @type {module:echarts/coord/single/SingleAxis}. - */ - this._axis = null; + name: string - /** - * @private - * @type {module:zrender/core/BoundingRect} - */ - this._rect; + axisPointerEnabled: boolean = true - this._init(axisModel, ecModel, api); + model: SingleAxisModel - /** - * @type {module:echarts/coord/single/AxisModel} - */ - this.model = axisModel; -} + private _axis: SingleAxis -Single.prototype = { + private _rect: BoundingRect - type: 'singleAxis', + constructor(axisModel: SingleAxisModel, ecModel: GlobalModel, api: ExtensionAPI) { - axisPointerEnabled: true, + this.model = axisModel; - constructor: Single, + this._init(axisModel, ecModel, api); + } /** * Initialize single coordinate system. - * - * @param {module:echarts/coord/single/AxisModel} axisModel - * @param {module:echarts/model/Global} ecModel - * @param {module:echarts/ExtensionAPI} api - * @private */ - _init: function (axisModel, ecModel, api) { + _init(axisModel: SingleAxisModel, ecModel: GlobalModel, api: ExtensionAPI) { var dim = this.dimension; @@ -108,14 +87,12 @@ Single.prototype = { axis.model = axisModel; axis.coordinateSystem = this; this._axis = axis; - }, + } /** * Update axis scale after data processed - * @param {module:echarts/model/Global} ecModel - * @param {module:echarts/ExtensionAPI} api */ - update: function (ecModel, api) { + update(ecModel: GlobalModel, api: ExtensionAPI) { ecModel.eachSeries(function (seriesModel) { if (seriesModel.coordinateSystem === this) { var data = seriesModel.getData(); @@ -125,15 +102,12 @@ Single.prototype = { axisHelper.niceScaleExtent(this._axis.scale, this._axis.model); } }, this); - }, + } /** * Resize the single coordinate system. - * - * @param {module:echarts/coord/single/AxisModel} axisModel - * @param {module:echarts/ExtensionAPI} api */ - resize: function (axisModel, api) { + resize(axisModel: SingleAxisModel, api: ExtensionAPI) { this._rect = getLayoutRect( { left: axisModel.get('left'), @@ -150,19 +124,13 @@ Single.prototype = { ); this._adjustAxis(); - }, + } - /** - * @return {module:zrender/core/BoundingRect} - */ - getRect: function () { + getRect() { return this._rect; - }, + } - /** - * @private - */ - _adjustAxis: function () { + private _adjustAxis() { var rect = this._rect; var axis = this._axis; @@ -175,13 +143,10 @@ Single.prototype = { this._updateAxisTransform(axis, isHorizontal ? rect.x : rect.y); - }, + } + - /** - * @param {module:echarts/coord/single/SingleAxis} axis - * @param {number} coordBase - */ - _updateAxisTransform: function (axis, coordBase) { + private _updateAxisTransform(axis: SingleAxis, coordBase: number) { var axisExtent = axis.getExtent(); var extentSum = axisExtent[0] + axisExtent[1]; @@ -202,47 +167,38 @@ Single.prototype = { : function (coord) { return extentSum - coord + coordBase; }; - }, + } /** * Get axis. - * - * @return {module:echarts/coord/single/SingleAxis} */ - getAxis: function () { + getAxis() { return this._axis; - }, + } /** * Get axis, add it just for draw tooltip. - * - * @return {[type]} [description] */ - getBaseAxis: function () { + getBaseAxis() { return this._axis; - }, + } - /** - * @return {Array.} - */ - getAxes: function () { + getAxes() { return [this._axis]; - }, + } - /** - * @return {Object} {baseAxes: [], otherAxes: []} - */ - getTooltipAxes: function () { - return {baseAxes: [this.getAxis()]}; - }, + getTooltipAxes() { + return { + baseAxes: [this.getAxis()], + // Empty otherAxes + otherAxes: [] as SingleAxis[] + }; + } /** * If contain point. - * - * @param {Array.} point - * @return {boolean} */ - containPoint: function (point) { + containPoint(point: number[]) { var rect = this.getRect(); var axis = this.getAxis(); var orient = axis.orient; @@ -254,26 +210,20 @@ Single.prototype = { return axis.contain(axis.toLocalCoord(point[1])) && (point[0] >= rect.y && point[0] <= (rect.y + rect.height)); } - }, + } - /** - * @param {Array.} point - * @return {Array.} - */ - pointToData: function (point) { + pointToData(point: number[]) { var axis = this.getAxis(); return [axis.coordToData(axis.toLocalCoord( point[axis.orient === 'horizontal' ? 0 : 1] ))]; - }, + } /** * Convert the series data to concrete point. - * - * @param {number|Array.} val - * @return {Array.} + * Can be [val] | val */ - dataToPoint: function (val) { + dataToPoint(val: ScaleDataValue | ScaleDataValue[]) { var axis = this.getAxis(); var rect = this.getRect(); var pt = []; @@ -288,6 +238,17 @@ Single.prototype = { return pt; } -}; + convertToPixel(ecModel: GlobalModel, finder: ParsedModelFinder, value: ScaleDataValue[]) { + const seriesModel = finder.seriesModel; + const coordSys = seriesModel && seriesModel.coordinateSystem; + return coordSys === this ? this.dataToPoint(value) : null; + } + + convertFromPixel(ecModel: GlobalModel, finder: ParsedModelFinder, pixel: number[]) { + const seriesModel = finder.seriesModel; + const coordSys = seriesModel && seriesModel.coordinateSystem; + return coordSys === this ? this.pointToData(pixel) : null; + } +} export default Single; diff --git a/src/coord/single/SingleAxis.ts b/src/coord/single/SingleAxis.ts index 9ea24b2af..f0a6df88d 100644 --- a/src/coord/single/SingleAxis.ts +++ b/src/coord/single/SingleAxis.ts @@ -17,98 +17,63 @@ * under the License. */ -// @ts-nocheck - -import * as zrUtil from 'zrender/src/core/util'; import Axis from '../Axis'; +import Scale from '../../scale/Scale'; +import { OptionAxisType } from '../axisCommonTypes'; +import SingleAxisModel, { SingleAxisPosition } from './AxisModel'; +import { LayoutOrient } from '../../util/types'; +import Single from './Single'; -/** - * @constructor module:echarts/coord/single/SingleAxis - * @extends {module:echarts/coord/Axis} - * @param {string} dim - * @param {*} scale - * @param {Array.} coordExtent - * @param {string} axisType - * @param {string} position - */ -var SingleAxis = function (dim, scale, coordExtent, axisType, position) { - - Axis.call(this, dim, scale, coordExtent); - +interface SingleAxis { /** - * Axis type - * - 'category' - * - 'value' - * - 'time' - * - 'log' - * @type {string} + * Transform global coord to local coord, + * i.e. var localCoord = axis.toLocalCoord(80); */ - this.type = axisType || 'value'; + toLocalCoord(coord: number): number; /** - * Axis position - * - 'top' - * - 'bottom' - * - 'left' - * - 'right' - * @type {string} + * Transform global coord to local coord, + * i.e. var globalCoord = axis.toLocalCoord(40); */ - this.position = position || 'bottom'; + toGlobalCoord(coord: number): number; +} +class SingleAxis extends Axis { - /** - * Axis orient - * - 'horizontal' - * - 'vertical' - * @type {[type]} - */ - this.orient = null; + type: OptionAxisType -}; + position: SingleAxisPosition -SingleAxis.prototype = { + orient: LayoutOrient - constructor: SingleAxis, + reverse: boolean - /** - * Axis model - * @type {module:echarts/coord/single/AxisModel} - */ - model: null, - - /** - * Judge the orient of the axis. - * @return {boolean} - */ - isHorizontal: function () { - var position = this.position; - return position === 'top' || position === 'bottom'; + coordinateSystem: Single - }, + model: SingleAxisModel - /** - * @override - */ - pointToData: function (point, clamp) { - return this.coordinateSystem.pointToData(point, clamp)[0]; - }, + constructor( + dim: string, + scale: Scale, + coordExtent: [number, number], + axisType?: OptionAxisType, + position?: SingleAxisPosition + ) { + super(dim, scale, coordExtent); - /** - * Convert the local coord(processed by dataToCoord()) - * to global coord(concrete pixel coord). - * designated by module:echarts/coord/single/Single. - * @type {Function} - */ - toGlobalCoord: null, + this.type = axisType || 'value'; + this.position = position || 'bottom'; + } /** - * Convert the global coord to local coord. - * designated by module:echarts/coord/single/Single. - * @type {Function} + * Judge the orient of the axis. */ - toLocalCoord: null - -}; - -zrUtil.inherits(SingleAxis, Axis); + isHorizontal() { + var position = this.position; + return position === 'top' || position === 'bottom'; + } + pointToData(point: number[], clamp?: boolean) { // TODO: clamp is not used. + return this.coordinateSystem.pointToData(point)[0]; + } +} export default SingleAxis; \ No newline at end of file diff --git a/src/coord/single/prepareCustom.ts b/src/coord/single/prepareCustom.ts index 602d153ca..6f3ef7a88 100644 --- a/src/coord/single/prepareCustom.ts +++ b/src/coord/single/prepareCustom.ts @@ -17,11 +17,10 @@ * under the License. */ -// @ts-nocheck - import * as zrUtil from 'zrender/src/core/util'; +import Single from './Single'; -function dataToCoordSize(dataSize, dataItem) { +function dataToCoordSize(this: Single, dataSize: number | number[], dataItem: number | number[]) { // dataItem is necessary in log axis. var axis = this.getAxis(); var val = dataItem instanceof Array ? dataItem[0] : dataItem; @@ -31,7 +30,7 @@ function dataToCoordSize(dataSize, dataItem) { : Math.abs(axis.dataToCoord(val - halfSize) - axis.dataToCoord(val + halfSize)); } -export default function (coordSys) { +export default function (coordSys: Single) { var rect = coordSys.getRect(); return { @@ -43,7 +42,7 @@ export default function (coordSys) { height: rect.height }, api: { - coord: function (val) { + coord: function (val: number) { // do not provide "out" param return coordSys.dataToPoint(val); }, diff --git a/src/coord/single/singleAxisHelper.ts b/src/coord/single/singleAxisHelper.ts index af2fba502..a2fb43eb5 100644 --- a/src/coord/single/singleAxisHelper.ts +++ b/src/coord/single/singleAxisHelper.ts @@ -17,22 +17,27 @@ * under the License. */ -// @ts-nocheck - import * as zrUtil from 'zrender/src/core/util'; +import SingleAxisModel from './AxisModel'; + +interface LayoutResult { + position: [number, number], + rotation: number + labelRotation: number + labelDirection: 1 | -1 + tickDirection: 1 | -1 + nameDirection: 1 | -1 + z2: number +} -/** - * @param {Object} opt {labelInside} - * @return {Object} { - * position, rotation, labelDirection, labelOffset, - * tickDirection, labelRotate, z2 - * } - */ -export function layout(axisModel, opt) { +export function layout(axisModel: SingleAxisModel, opt?: { + labelInside?: boolean + rotate?: number +}) { opt = opt || {}; var single = axisModel.coordinateSystem; var axis = axisModel.axis; - var layout = {}; + var layout = {} as LayoutResult; var axisPosition = axis.position; var orient = axis.orient; @@ -43,35 +48,35 @@ export function layout(axisModel, opt) { var positionMap = { horizontal: {top: rectBound[2], bottom: rectBound[3]}, vertical: {left: rectBound[0], right: rectBound[1]} - }; + } as const; layout.position = [ orient === 'vertical' - ? positionMap.vertical[axisPosition] + ? positionMap.vertical[axisPosition as 'left' | 'right'] : rectBound[0], orient === 'horizontal' - ? positionMap.horizontal[axisPosition] + ? positionMap.horizontal[axisPosition as 'top' | 'bottom'] : rectBound[3] - ]; + ] as [number, number]; var r = {horizontal: 0, vertical: 1}; layout.rotation = Math.PI / 2 * r[orient]; - var directionMap = {top: -1, bottom: 1, right: 1, left: -1}; + const directionMap = {top: -1, bottom: 1, right: 1, left: -1} as const; layout.labelDirection = layout.tickDirection = layout.nameDirection = directionMap[axisPosition]; - if (axisModel.get('axisTick.inside')) { - layout.tickDirection = -layout.tickDirection; + if (axisModel.get(['axisTick', 'inside'])) { + layout.tickDirection = -layout.tickDirection as -1 | 1; } - if (zrUtil.retrieve(opt.labelInside, axisModel.get('axisLabel.inside'))) { - layout.labelDirection = -layout.labelDirection; + if (zrUtil.retrieve(opt.labelInside, axisModel.get(['axisLabel', 'inside']))) { + layout.labelDirection = -layout.labelDirection as -1 | 1; } var labelRotation = opt.rotate; - labelRotation == null && (labelRotation = axisModel.get('axisLabel.rotate')); + labelRotation == null && (labelRotation = axisModel.get(['axisLabel', 'rotate'])); layout.labelRotation = axisPosition === 'top' ? -labelRotation : labelRotation; layout.z2 = 1; diff --git a/src/coord/single/singleCreator.ts b/src/coord/single/singleCreator.ts index d7f85460d..3bd2ad64b 100644 --- a/src/coord/single/singleCreator.ts +++ b/src/coord/single/singleCreator.ts @@ -17,26 +17,25 @@ * under the License. */ -// @ts-nocheck - /** * Single coordinate system creator. */ import Single from './Single'; import CoordinateSystem from '../../CoordinateSystem'; +import GlobalModel from '../../model/Global'; +import ExtensionAPI from '../../ExtensionAPI'; +import SingleAxisModel from './AxisModel'; +import SeriesModel from '../../model/Series'; +import { SeriesOption } from '../../util/types'; /** * Create single coordinate system and inject it into seriesModel. - * - * @param {module:echarts/model/Global} ecModel - * @param {module:echarts/ExtensionAPI} api - * @return {Array.} */ -function create(ecModel, api) { - var singles = []; +function create(ecModel: GlobalModel, api: ExtensionAPI) { + var singles: Single[] = []; - ecModel.eachComponent('singleAxis', function (axisModel, idx) { + ecModel.eachComponent('singleAxis', function (axisModel: SingleAxisModel, idx: number) { var single = new Single(axisModel, ecModel, api); single.name = 'single_' + idx; @@ -46,13 +45,16 @@ function create(ecModel, api) { }); - ecModel.eachSeries(function (seriesModel) { + ecModel.eachSeries(function (seriesModel: SeriesModel) { if (seriesModel.get('coordinateSystem') === 'singleAxis') { var singleAxisModel = ecModel.queryComponents({ mainType: 'singleAxis', index: seriesModel.get('singleAxisIndex'), id: seriesModel.get('singleAxisId') - })[0]; + })[0] as SingleAxisModel; seriesModel.coordinateSystem = singleAxisModel && singleAxisModel.coordinateSystem; } }); -- GitLab