Skip to content

Commit

Permalink
feat: add axis component event data
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoHe committed Dec 25, 2024
1 parent 7253f92 commit 4455dc5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
15 changes: 12 additions & 3 deletions packages/vchart/src/component/axis/cartesian/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type { IComponentOption } from '../../interface';
// eslint-disable-next-line no-duplicate-imports
import { ComponentTypeEnum } from '../../interface/type';
import { HOOK_EVENT } from '@visactor/vgrammar-core';
import type { AxisItem, LineAxisAttributes } from '@visactor/vrender-components';
import { AXIS_ELEMENT_NAME, type AxisItem, type LineAxisAttributes } from '@visactor/vrender-components';
// eslint-disable-next-line no-duplicate-imports
import { getAxisItem, isValidCartesianAxis, shouldUpdateAxis } from '../util';
import type { IAxis, ITick } from '../interface';
Expand All @@ -46,11 +46,10 @@ import type { ICartesianTickDataOpt } from '@visactor/vrender-components';
import type { DataSet } from '@visactor/vdataset';
// eslint-disable-next-line no-duplicate-imports
import { AxisComponent } from '../base-axis';
import type { IGraphic, IText } from '@visactor/vrender-core';
import type { FederatedEvent, IGraphic, IText } from '@visactor/vrender-core';
// eslint-disable-next-line no-duplicate-imports
import { createText } from '@visactor/vrender-core';
import type { ICartesianChartSpec } from '../../../chart/cartesian/interface';
import { STACK_FIELD_END, STACK_FIELD_START } from '../../../constant/data';

const CartesianAxisPlugin = [AxisSyncPlugin];

Expand Down Expand Up @@ -975,6 +974,16 @@ export abstract class CartesianAxis<T extends ICartesianAxisCommonSpec = ICartes
this._clearLayoutCache();
}

getDatum(childGraphic?: IGraphic) {
if (childGraphic && childGraphic.name === AXIS_ELEMENT_NAME.label) {
return childGraphic.data;
}

if (this._axisMark) {
return this._axisMark.getProduct()?.getGroupGraphicItem()?.attribute.items;
}
}

private _appendAxisUnit(bounds: IBounds, isX: boolean) {
if (this._spec.unit && this._spec.unit.visible) {
const { text, style } = this._spec.unit;
Expand Down
7 changes: 6 additions & 1 deletion packages/vchart/src/component/base/base-component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomEvent, type IGraphic, type IGroup, type INode } from '@visactor/vrender-core';
import { CustomEvent, type IGraphicAttribute, type IGraphic, type IGroup, type INode } from '@visactor/vrender-core';
import type { IRegion } from '../../region/interface';
import type { IComponent, IComponentOption } from '../interface';
import { ComponentPluginService } from '../../plugin/components/plugin-service';
Expand Down Expand Up @@ -178,4 +178,9 @@ export class BaseComponent<T extends IComponentSpec = IComponentSpec> extends La
getBoundsInRect(rect: ILayoutRect, fullRect: ILayoutRect): IBoundsLike {
return { x1: 0, x2: 0, y1: 0, y2: 0 };
}

getDatum(graphic?: IGraphic<Partial<IGraphicAttribute>>) {
// override
return;
}
}
3 changes: 3 additions & 0 deletions packages/vchart/src/component/interface/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export interface IComponent extends ILayoutModel {

// 清空,用于更新等场景
clear: () => void;

// 数据
getDatum: (childGraphic?: IGraphic) => any | undefined;
}

export interface IComponentConstructor extends IModelConstructor {
Expand Down
11 changes: 9 additions & 2 deletions packages/vchart/src/event/event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { CompilerListenerParameters } from '../compile/interface';
import type { Compiler } from '../compile/compiler';
import type { StringOrNumber } from '../typings';
import type { IElement } from '@visactor/vgrammar-core';
import type { IComponent } from '../component/interface';
import { Factory as VGrammarFactory } from '@visactor/vgrammar-core';

const componentTypeMap: Record<string, string> = {
Expand Down Expand Up @@ -301,11 +302,18 @@ export class EventDispatcher implements IEventDispatcher {
targetMark = targetMark.group;
}

const node = get(listenerParams.event, 'target');

let datum = listenerParams.datum;
if (model && model.modelType === 'component') {
datum = (model as IComponent).getDatum(node) ?? datum;
}

const params: BaseEventParams = {
event: listenerParams.event,
item: listenerParams.item,
datum: listenerParams.datum,
source: listenerParams.source,
datum,
itemMap,
chart,
model,
Expand All @@ -326,7 +334,6 @@ export class EventDispatcher implements IEventDispatcher {
if ((event as any).elements) {
items = (event as any).elements;
}

const params: InteractionEventParam = {
event: listenerParams.event,
chart,
Expand Down

0 comments on commit 4455dc5

Please sign in to comment.