Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(custom): reusable custom series #20226

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/chart/custom/CustomSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export interface CustomSeriesRenderItemParams {

dataIndexInside: number;
dataInsideLength: number;
itemPayload: Dictionary<unknown>;

actionType?: string;
}
Expand All @@ -334,6 +335,7 @@ export interface CustomSeriesOption extends
coordinateSystem?: string | 'none';

renderItem?: CustomSeriesRenderItem;
itemPayload?: Dictionary<unknown>;

/**
* @deprecated
Expand Down
17 changes: 15 additions & 2 deletions src/chart/custom/CustomView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PrepareCustomInfo>;

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/chart/custom/customSeriesRegister.ts
Original file line number Diff line number Diff line change
@@ -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];
}
6 changes: 6 additions & 0 deletions src/core/echarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2893,6 +2895,10 @@ export function getCoordinateSystemDimensions(type: string): DimensionDefinition
}
}

export function registerCustomSeries(seriesType: string, renderItem: CustomSeriesRenderItem) {
registerCustom(seriesType, renderItem);
}

export {registerLocale} from './locale';

/**
Expand Down
3 changes: 2 additions & 1 deletion src/model/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ class GlobalModel extends Model<ECUnitOption> {
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
);

Expand Down
98 changes: 98 additions & 0 deletions test/custom-register.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading