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 1 commit
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
17 changes: 17 additions & 0 deletions src/chart/custom/CustomSeriesManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CustomSeriesRenderItem } from './CustomSeries';

const customRenderers: {[type: string]: CustomSeriesRenderItem} = {};

class CustomSeriesManager {

static register = function (type: string, creator: CustomSeriesRenderItem): void {
customRenderers[type] = creator;
};

static get = function (type: string): CustomSeriesRenderItem {
return customRenderers[type];
}

Check failure on line 13 in src/chart/custom/CustomSeriesManager.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Missing semicolon

}

export default CustomSeriesManager;
6 changes: 5 additions & 1 deletion src/chart/custom/CustomView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,11 @@ function makeRenderItem(
ecModel: GlobalModel,
api: ExtensionAPI
) {
const renderItem = customSeries.get('renderItem');
const type = customSeries.get('type').split('.');
const customRenderer = type.length > 1
? ecModel.getCustomRenderer(type[1])
: null;
const renderItem = customRenderer || customSeries.get('renderItem');
const coordSys = customSeries.coordinateSystem;
let prepareResult = {} as ReturnType<PrepareCustomInfo>;

Expand Down
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 {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 CustomSeriesManager from '../chart/custom/CustomSeriesManager';

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) {
CustomSeriesManager.register(seriesType, renderItem);
}

export {registerLocale} from './locale';

/**
Expand Down
17 changes: 15 additions & 2 deletions src/model/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { concatInternalOptions } from './internalComponentCreator';
import { LocaleOption } from '../core/locale';
import {PaletteMixin} from './mixin/palette';
import { error, warn } from '../util/log';
import CustomSeriesManager from '../chart/custom/CustomSeriesManager';

export interface GlobalModelSetOptionOpts {
replaceMerge: ComponentMainType | ComponentMainType[];
Expand Down Expand Up @@ -161,6 +162,8 @@ class GlobalModel extends Model<ECUnitOption> {

private _optionManager: OptionManager;

private _customSeriesManager: CustomSeriesManager;
Ovilia marked this conversation as resolved.
Show resolved Hide resolved

private _componentsMap: HashMap<ComponentModel[], ComponentMainType>;

/**
Expand Down Expand Up @@ -410,14 +413,20 @@ class GlobalModel extends Model<ECUnitOption> {
}
else {
const isSeriesType = mainType === 'series';

let subType = resultItem.keyInfo.subType;
if (subType && subType.startsWith('custom.')) {
subType = 'custom';
}

const ComponentModelClass = (ComponentModel as ComponentModelConstructor).getClass(
mainType, resultItem.keyInfo.subType,
mainType,
subType,
!isSeriesType // Give a more detailed warn later if series don't exists
);

if (!ComponentModelClass) {
if (__DEV__) {
const subType = resultItem.keyInfo.subType;
const seriesImportName = BUILTIN_CHARTS_MAP[subType as keyof typeof BUILTIN_CHARTS_MAP];
if (!componetsMissingLogPrinted[subType]) {
componetsMissingLogPrinted[subType] = true;
Expand Down Expand Up @@ -578,6 +587,10 @@ echarts.use([${seriesImportName}]);`);
}
}

getCustomRenderer(type: string) {
return CustomSeriesManager.get(type);
}

Ovilia marked this conversation as resolved.
Show resolved Hide resolved
/**
* @return Never be null/undefined.
*/
Expand Down
92 changes: 92 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