Skip to content

Commit

Permalink
Merge pull request #3607 from VisActor/feat/axis-event-data
Browse files Browse the repository at this point in the history
feat: add axis component event data
  • Loading branch information
xile611 authored Dec 25, 2024
2 parents 7253f92 + 2f7b826 commit 8027c06
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vchart",
"comment": "feat: add datum to params on axis-label event ",
"type": "none"
}
],
"packageName": "@visactor/vchart"
}
12 changes: 11 additions & 1 deletion packages/vchart/src/component/axis/base-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { GridEnum } from '@visactor/vgrammar-core';
import { registerComponentMark } from '../../mark/component';
import { Factory } from '../../core/factory';
// eslint-disable-next-line no-duplicate-imports
import { GroupTransition } from '@visactor/vrender-components';
import { AXIS_ELEMENT_NAME, GroupTransition } from '@visactor/vrender-components';
// eslint-disable-next-line no-duplicate-imports
import { GroupFadeOut, GroupFadeIn } from '@visactor/vrender-core';
import { scaleParser } from '../../data/parser/scale';
Expand Down Expand Up @@ -704,6 +704,16 @@ export abstract class AxisComponent<T extends ICommonAxisSpec & Record<string, a
dataToPosition(values: any[]): number {
return this._scale.scale(values);
}

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

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

export const registerAxis = () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/vchart/src/component/axis/cartesian/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ICartesianHorizontal } from './interface/spec';
import { Bounds, last, type IBounds, type IBoundsLike, type Maybe } from '@visactor/vutils';
// eslint-disable-next-line no-duplicate-imports
import type { IEffect, IModelInitOption, IModelSpecInfo } from '../../../model/interface';
import { SeriesTypeEnum, type ICartesianSeries } from '../../../series/interface';
import type { ICartesianSeries } from '../../../series/interface';
import type { IRegion } from '../../../region/interface';
import type { ICartesianAxisCommonSpec, IAxisHelper, ICartesianVertical } from './interface';
import { mergeSpec } from '@visactor/vutils-extension';
Expand Down Expand Up @@ -50,7 +50,6 @@ import type { 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
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 8027c06

Please sign in to comment.