提交 6b2ec74a 编写于 作者: 1 100pah

add axis model creator type

上级 786ae682
...@@ -24,6 +24,7 @@ import * as zrUtil from 'zrender/src/core/util'; ...@@ -24,6 +24,7 @@ import * as zrUtil from 'zrender/src/core/util';
import * as graphic from '../util/graphic'; import * as graphic from '../util/graphic';
import '../coord/cartesian/Grid'; import '../coord/cartesian/Grid';
import '../coord/cartesian/GridModel';
import './axis'; import './axis';
// Grid view // Grid view
......
...@@ -26,12 +26,12 @@ import './legend/legendAction'; ...@@ -26,12 +26,12 @@ import './legend/legendAction';
import './legend/LegendView'; import './legend/LegendView';
import legendFilter from './legend/legendFilter'; import legendFilter from './legend/legendFilter';
import Component, { ComponentModelConstructor } from '../model/Component'; import Component from '../model/Component';
// Series Filter // Series Filter
echarts.registerProcessor(echarts.PRIORITY.PROCESSOR.SERIES_FILTER, legendFilter); echarts.registerProcessor(echarts.PRIORITY.PROCESSOR.SERIES_FILTER, legendFilter);
(Component as ComponentModelConstructor).registerSubTypeDefaulter('legend', function () { Component.registerSubTypeDefaulter('legend', function () {
// Default 'plain' when no type specified. // Default 'plain' when no type specified.
return 'plain'; return 'plain';
}); });
...@@ -28,6 +28,8 @@ import { ZRenderType } from 'zrender/src/zrender'; ...@@ -28,6 +28,8 @@ import { ZRenderType } from 'zrender/src/zrender';
import { TooltipOption } from './TooltipModel'; import { TooltipOption } from './TooltipModel';
import Model from '../../model/Model'; import Model from '../../model/Model';
import { ZRRawEvent } from 'zrender/src/core/types'; 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 each = zrUtil.each;
var toCamelCase = formatUtil.toCamelCase; var toCamelCase = formatUtil.toCamelCase;
...@@ -134,7 +136,7 @@ function makeStyleCoord(out: number[], zr: ZRenderType, appendToBody: boolean, z ...@@ -134,7 +136,7 @@ function makeStyleCoord(out: number[], zr: ZRenderType, appendToBody: boolean, z
// xy should be based on canvas root. But tooltipContent is // xy should be based on canvas root. But tooltipContent is
// the sibling of canvas root. So padding of ec container // the sibling of canvas root. So padding of ec container
// should be considered here. // should be considered here.
var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset(); var viewportRootOffset = zrPainter && (zrPainter as CanvasPainter | SVGPainter).getViewportRootOffset();
if (viewportRootOffset) { if (viewportRootOffset) {
out[0] += viewportRootOffset.offsetLeft; out[0] += viewportRootOffset.offsetLeft;
out[1] += viewportRootOffset.offsetTop; out[1] += viewportRootOffset.offsetTop;
......
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
* under the License. * under the License.
*/ */
import Component, { ComponentModelConstructor } from '../../model/Component'; import Component from '../../model/Component';
import {VisualMapOption} from './VisualMapModel'; import {VisualMapOption} from './VisualMapModel';
import {PiecewiseVisualMapOption} from './PiecewiseModel'; import {PiecewiseVisualMapOption} from './PiecewiseModel';
import {ContinousVisualMapOption} from './ContinuousModel'; import {ContinousVisualMapOption} from './ContinuousModel';
(Component as ComponentModelConstructor).registerSubTypeDefaulter( Component.registerSubTypeDefaulter(
'visualMap', function (option: VisualMapOption) { 'visualMap', function (option: VisualMapOption) {
// Compatible with ec2, when splitNumber === 0, continuous visualMap will be used. // Compatible with ec2, when splitNumber === 0, continuous visualMap will be used.
return ( return (
......
...@@ -52,7 +52,7 @@ export interface CoordinateSystemMaster { ...@@ -52,7 +52,7 @@ export interface CoordinateSystemMaster {
model?: Model; model?: Model;
update(ecModel: GlobalModel, api: ExtensionAPI): void; update?: (ecModel: GlobalModel, api: ExtensionAPI) => void;
// This methods is also responsible for determine whether this // This methods is also responsible for determine whether this
// coodinate system is applicable to the given `finder`. // coodinate system is applicable to the given `finder`.
......
...@@ -24,7 +24,9 @@ import { ...@@ -24,7 +24,9 @@ import {
import { Dictionary } from 'zrender/src/core/types'; 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 { export interface AxisBaseOption extends ComponentOption {
type?: OptionAxisType; type?: OptionAxisType;
......
...@@ -174,8 +174,8 @@ var logAxis: AxisBaseOption = zrUtil.defaults({ ...@@ -174,8 +174,8 @@ var logAxis: AxisBaseOption = zrUtil.defaults({
export default { export default {
categoryAxis: categoryAxis, category: categoryAxis,
valueAxis: valueAxis, value: valueAxis,
timeAxis: timeAxis, time: timeAxis,
logAxis: logAxis log: logAxis
}; };
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
* under the License. * under the License.
*/ */
// @ts-nocheck
import * as zrUtil from 'zrender/src/core/util'; import * as zrUtil from 'zrender/src/core/util';
import axisDefault from './axisDefault'; import axisDefault from './axisDefault';
import ComponentModel from '../model/Component'; import ComponentModel from '../model/Component';
...@@ -28,95 +26,100 @@ import { ...@@ -28,95 +26,100 @@ import {
fetchLayoutMode fetchLayoutMode
} from '../util/layout'; } from '../util/layout';
import OrdinalMeta from '../data/OrdinalMeta'; import OrdinalMeta from '../data/OrdinalMeta';
import { DimensionName, ComponentOption } from '../util/types'; import { DimensionName, BoxLayoutOptionMixin, OrdinalRawValue } from '../util/types';
import { OptionAxisType } from './axisCommonTypes'; import { AxisBaseOption, AXIS_TYPES } from './axisCommonTypes';
import GlobalModel from '../model/Global';
// FIXME axisType is fixed ? type Constructor<T> = new (...args: any[]) => T;
var AXIS_TYPES = ['value', 'category', 'time', 'log'];
/** /**
* Generate sub axis model class * Generate sub axis model class
* @param axisName 'x' 'y' 'radius' 'angle' 'parallel' * @param axisName 'x' 'y' 'radius' 'angle' 'parallel' ...
* @param {module:echarts/model/Component} BaseAxisModelClass
* @param {Function} axisTypeDefaulter
* @param {Object} [extraDefaultOption]
*/ */
export default function ( export default function <
AxisOptionT extends AxisBaseOption,
AxisModelCtor extends Constructor<ComponentModel<AxisOptionT>>
>(
axisName: DimensionName, axisName: DimensionName,
BaseAxisModelClass, BaseAxisModelClass: AxisModelCtor,
axisTypeDefaulter: (axisDim: DimensionName, option: ComponentOption) => OptionAxisType, extraDefaultOption?: AxisOptionT
extraDefaultOption?: ComponentOption
) { ) {
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
);
/** class AxisModel extends BaseAxisModelClass {
* @readOnly
*/ static type = axisName + 'Axis.' + axisType;
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 layoutMode = fetchLayoutMode(this);
var inputPositionParams = layoutMode var inputPositionParams = layoutMode
? getLayoutParams(option) : {}; ? getLayoutParams(option as BoxLayoutOptionMixin) : {};
var themeModel = ecModel.getTheme(); var themeModel = ecModel.getTheme();
zrUtil.merge(option, themeModel.get(axisType + 'Axis')); zrUtil.merge(option, themeModel.get(axisType + 'Axis'));
zrUtil.merge(option, this.getDefaultOption()); zrUtil.merge(option, this.getDefaultOption());
option.type = axisTypeDefaulter(axisName, option); option.type = getAxisType(option);
if (layoutMode) { if (layoutMode) {
mergeLayoutParam(option, inputPositionParams, layoutMode); mergeLayoutParam(option as BoxLayoutOptionMixin, inputPositionParams, layoutMode);
} }
}, }
/** optionUpdated(): void {
* @override
*/
optionUpdated: function () {
var thisOption = this.option; var thisOption = this.option;
if (thisOption.type === 'category') { if (thisOption.type === 'category') {
this.__ordinalMeta = OrdinalMeta.createByAxisModel(this); this.__ordinalMeta = OrdinalMeta.createByAxisModel(this);
} }
}, }
/** /**
* Should not be called before all of 'getInitailData' finished. * Should not be called before all of 'getInitailData' finished.
* Because categories are collected during initializing data. * Because categories are collected during initializing data.
*/ */
getCategories: function (rawData) { getCategories(rawData: boolean): OrdinalRawValue[] | AxisBaseOption['data'] {
var option = this.option; var option = this.option;
// FIXME // FIXME
// warning if called before all of 'getInitailData' finished. // warning if called before all of 'getInitailData' finished.
if (option.type === 'category') { if (option.type === 'category') {
if (rawData) { if (rawData) {
return option.data; return option.data as AxisBaseOption['data'];
} }
return this.__ordinalMeta.categories; return this.__ordinalMeta.categories;
} }
}, }
getOrdinalMeta: function () { getOrdinalMeta(): OrdinalMeta {
return this.__ordinalMeta; return this.__ordinalMeta;
}, }
}
defaultOption: zrUtil.mergeAll(
[ ComponentModel.registerClass(AxisModel);
{},
axisDefault[axisType + 'Axis'],
extraDefaultOption
],
true
)
});
}); });
ComponentModel.registerSubTypeDefaulter( ComponentModel.registerSubTypeDefaulter(
axisName + 'Axis', 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');
}
...@@ -22,7 +22,6 @@ import ComponentModel from '../../model/Component'; ...@@ -22,7 +22,6 @@ import ComponentModel from '../../model/Component';
import axisModelCreator from '../axisModelCreator'; import axisModelCreator from '../axisModelCreator';
import {AxisModelCommonMixin} from '../axisModelCommonMixin'; import {AxisModelCommonMixin} from '../axisModelCommonMixin';
import Axis2D from './Axis2D'; import Axis2D from './Axis2D';
import { DimensionName } from '../../util/types';
import { AxisBaseOption } from '../axisCommonTypes'; import { AxisBaseOption } from '../axisCommonTypes';
import GridModel from './GridModel'; import GridModel from './GridModel';
...@@ -69,11 +68,6 @@ class AxisModel extends ComponentModel<CartesianAxisOption> { ...@@ -69,11 +68,6 @@ class AxisModel extends ComponentModel<CartesianAxisOption> {
ComponentModel.registerClass(AxisModel); 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> {} interface AxisModel extends AxisModelCommonMixin<CartesianAxisOption> {}
zrUtil.mixin(AxisModel, AxisModelCommonMixin); zrUtil.mixin(AxisModel, AxisModelCommonMixin);
...@@ -83,7 +77,7 @@ var extraOption: CartesianAxisOption = { ...@@ -83,7 +77,7 @@ var extraOption: CartesianAxisOption = {
offset: 0 offset: 0
}; };
axisModelCreator('x', AxisModel, getAxisType, extraOption); axisModelCreator<CartesianAxisOption, typeof AxisModel>('x', AxisModel, extraOption);
axisModelCreator('y', AxisModel, getAxisType, extraOption); axisModelCreator<CartesianAxisOption, typeof AxisModel>('y', AxisModel, extraOption);
export default AxisModel; export default AxisModel;
...@@ -37,7 +37,7 @@ interface GridOption extends ComponentOption, BoxLayoutOptionMixin, ShadowOption ...@@ -37,7 +37,7 @@ interface GridOption extends ComponentOption, BoxLayoutOptionMixin, ShadowOption
tooltop?: any; // FIXME:TS add this tooltip type 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'; static type = 'grid';
...@@ -63,5 +63,8 @@ export default class extends ComponentModel<GridOption> implements CoordinateSys ...@@ -63,5 +63,8 @@ export default class extends ComponentModel<GridOption> implements CoordinateSys
borderWidth: 1, borderWidth: 1,
borderColor: '#ccc' borderColor: '#ccc'
} }
}
ComponentModel.registerClass(GridModel);
} export default GridModel;
\ No newline at end of file
...@@ -142,10 +142,6 @@ var defaultOption = { ...@@ -142,10 +142,6 @@ var defaultOption = {
zrUtil.mixin(AxisModel, AxisModelCommonMixin); zrUtil.mixin(AxisModel, AxisModelCommonMixin);
function getAxisType(axisName, option) { axisModelCreator('parallel', AxisModel, defaultOption);
return option.type || (option.data ? 'category' : 'value');
}
axisModelCreator('parallel', AxisModel, getAxisType, defaultOption);
export default AxisModel; export default AxisModel;
\ No newline at end of file
...@@ -71,10 +71,5 @@ var polarAxisDefaultExtendedOption = { ...@@ -71,10 +71,5 @@ var polarAxisDefaultExtendedOption = {
} }
}; };
function getAxisType(axisDim, option) { axisModelCreator('angle', PolarAxisModel, polarAxisDefaultExtendedOption.angle);
// Default axis with data is category axis axisModelCreator('radius', PolarAxisModel, polarAxisDefaultExtendedOption.radius);
return option.type || (option.data ? 'category' : 'value');
}
axisModelCreator('angle', PolarAxisModel, getAxisType, polarAxisDefaultExtendedOption.angle);
axisModelCreator('radius', PolarAxisModel, getAxisType, polarAxisDefaultExtendedOption.radius);
...@@ -25,7 +25,7 @@ import axisDefault from '../axisDefault'; ...@@ -25,7 +25,7 @@ import axisDefault from '../axisDefault';
import Model from '../../model/Model'; import Model from '../../model/Model';
import {AxisModelCommonMixin} from '../axisModelCommonMixin'; import {AxisModelCommonMixin} from '../axisModelCommonMixin';
var valueAxisDefault = axisDefault.valueAxis; var valueAxisDefault = axisDefault.value;
function defaultsShow(opt, show) { function defaultsShow(opt, show) {
return zrUtil.defaults({ return zrUtil.defaults({
......
...@@ -99,12 +99,8 @@ var defaultOption = { ...@@ -99,12 +99,8 @@ var defaultOption = {
} }
}; };
function getAxisType(axisName, option) {
return option.type || (option.data ? 'category' : 'value');
}
zrUtil.mixin(AxisModel, {AxisModelCommonMixin}); zrUtil.mixin(AxisModel, {AxisModelCommonMixin});
axisModelCreator('single', AxisModel, getAxisType, defaultOption); axisModelCreator('single', AxisModel, defaultOption);
export default AxisModel; export default AxisModel;
\ No newline at end of file
...@@ -76,13 +76,21 @@ export {createSymbol} from './util/symbol'; ...@@ -76,13 +76,21 @@ export {createSymbol} from './util/symbol';
/** /**
* Create scale * Create scale
* @param {Array.<number>} dataExtent * @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) { export function createScale(dataExtent, option) {
var axisModel = option; var axisModel = option;
if (!Model.isInstance(option)) { if (!Model.isInstance(option)) {
axisModel = new Model(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); var scale = axisHelper.createScaleByModel(axisModel);
......
...@@ -286,6 +286,8 @@ class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Mode ...@@ -286,6 +286,8 @@ class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Mode
} }
static registerClass: ClassManager['registerClass']; static registerClass: ClassManager['registerClass'];
static registerSubTypeDefaulter: componentUtil.SubTypeDefaulterManager['registerSubTypeDefaulter'];
} }
// Reset ComponentModel.extend, add preConstruct. // Reset ComponentModel.extend, add preConstruct.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册