diff --git a/src/chart/custom/CustomSeries.ts b/src/chart/custom/CustomSeries.ts index 29014e1a6a..4c3a9433fb 100644 --- a/src/chart/custom/CustomSeries.ts +++ b/src/chart/custom/CustomSeries.ts @@ -308,6 +308,7 @@ export interface CustomSeriesRenderItemParams { dataIndexInside: number; dataInsideLength: number; + itemPayload: Dictionary; actionType?: string; } @@ -334,6 +335,7 @@ export interface CustomSeriesOption extends coordinateSystem?: string | 'none'; renderItem?: CustomSeriesRenderItem; + itemPayload?: Dictionary; /** * @deprecated diff --git a/src/chart/custom/CustomView.ts b/src/chart/custom/CustomView.ts index ee4e244cf7..afb047da3e 100644 --- a/src/chart/custom/CustomView.ts +++ b/src/chart/custom/CustomView.ts @@ -100,6 +100,7 @@ import { stopPreviousKeyframeAnimationAndRestore } from '../../animation/customGraphicKeyframeAnimation'; import type SeriesModel from '../../model/Series'; +import { getCustomSeries } from './customSeriesRegister'; const EMPHASIS = 'emphasis' as const; const NORMAL = 'normal' as const; @@ -591,7 +592,18 @@ function makeRenderItem( ecModel: GlobalModel, api: ExtensionAPI ) { - const renderItem = customSeries.get('renderItem'); + let renderItem = customSeries.get('renderItem'); + if (typeof renderItem === 'string') { + // Find renderItem in registered custom series + const registeredRenderItem = getCustomSeries(renderItem); + if (registeredRenderItem) { + renderItem = registeredRenderItem; + } + else if (__DEV__) { + console.warn(`Custom series renderItem '${renderItem}' not found. + Call 'echarts.registerCustomSeries' to register it.`); + } + } const coordSys = customSeries.coordinateSystem; let prepareResult = {} as ReturnType; @@ -635,7 +647,8 @@ function makeRenderItem( seriesIndex: customSeries.seriesIndex, coordSys: prepareResult.coordSys, dataInsideLength: data.count(), - encode: wrapEncodeDef(customSeries.getData()) + encode: wrapEncodeDef(customSeries.getData()), + itemPayload: customSeries.get('itemPayload') || {} } as CustomSeriesRenderItemParams; // If someday intending to refactor them to a class, should consider do not diff --git a/src/chart/custom/customSeriesRegister.ts b/src/chart/custom/customSeriesRegister.ts new file mode 100644 index 0000000000..3edfe64f2d --- /dev/null +++ b/src/chart/custom/customSeriesRegister.ts @@ -0,0 +1,11 @@ +import type { CustomSeriesRenderItem } from './CustomSeries'; + +const customRenderers: {[type: string]: CustomSeriesRenderItem} = {}; + +export function registerCustomSeries(type: string, renderItem: CustomSeriesRenderItem): void { + customRenderers[type] = renderItem; +} + +export function getCustomSeries(type: string): CustomSeriesRenderItem { + return customRenderers[type]; +} diff --git a/src/core/echarts.ts b/src/core/echarts.ts index 4ecb2c6534..05e37913bc 100644 --- a/src/core/echarts.ts +++ b/src/core/echarts.ts @@ -51,6 +51,7 @@ import ComponentModel from '../model/Component'; import SeriesModel from '../model/Series'; import ComponentView, {ComponentViewConstructor} from '../view/Component'; import ChartView, {ChartViewConstructor} from '../view/Chart'; +import type {CustomSeriesRenderItem} from '../chart/custom/CustomSeries'; import * as graphic from '../util/graphic'; import {getECData} from '../util/innerStore'; import { @@ -132,6 +133,7 @@ import lifecycle, { import { platformApi, setPlatformAPI } from 'zrender/src/core/platform'; import { getImpl } from './impl'; import type geoSourceManager from '../coord/geo/geoSourceManager'; +import {registerCustomSeries as registerCustom} from '../chart/custom/customSeriesRegister'; declare let global: any; @@ -2893,6 +2895,10 @@ export function getCoordinateSystemDimensions(type: string): DimensionDefinition } } +export function registerCustomSeries(seriesType: string, renderItem: CustomSeriesRenderItem) { + registerCustom(seriesType, renderItem); +} + export {registerLocale} from './locale'; /** diff --git a/src/model/Global.ts b/src/model/Global.ts index b845d7263d..7b2e8a3396 100644 --- a/src/model/Global.ts +++ b/src/model/Global.ts @@ -411,7 +411,8 @@ class GlobalModel extends Model { else { const isSeriesType = mainType === 'series'; const ComponentModelClass = (ComponentModel as ComponentModelConstructor).getClass( - mainType, resultItem.keyInfo.subType, + mainType, + resultItem.keyInfo.subType, !isSeriesType // Give a more detailed warn later if series don't exists ); diff --git a/test/custom-register.html b/test/custom-register.html new file mode 100644 index 0000000000..86925a6c54 --- /dev/null +++ b/test/custom-register.html @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +