Skip to content

Commit 8027c06

Browse files
authored
Merge pull request #3607 from VisActor/feat/axis-event-data
feat: add axis component event data
2 parents 7253f92 + 2f7b826 commit 8027c06

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@visactor/vchart",
5+
"comment": "feat: add datum to params on axis-label event ",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@visactor/vchart"
10+
}

packages/vchart/src/component/axis/base-axis.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { GridEnum } from '@visactor/vgrammar-core';
4747
import { registerComponentMark } from '../../mark/component';
4848
import { Factory } from '../../core/factory';
4949
// eslint-disable-next-line no-duplicate-imports
50-
import { GroupTransition } from '@visactor/vrender-components';
50+
import { AXIS_ELEMENT_NAME, GroupTransition } from '@visactor/vrender-components';
5151
// eslint-disable-next-line no-duplicate-imports
5252
import { GroupFadeOut, GroupFadeIn } from '@visactor/vrender-core';
5353
import { scaleParser } from '../../data/parser/scale';
@@ -704,6 +704,16 @@ export abstract class AxisComponent<T extends ICommonAxisSpec & Record<string, a
704704
dataToPosition(values: any[]): number {
705705
return this._scale.scale(values);
706706
}
707+
708+
getDatum(childGraphic?: IGraphic) {
709+
if (childGraphic && childGraphic.name === AXIS_ELEMENT_NAME.label) {
710+
return childGraphic.data;
711+
}
712+
713+
if (this._axisMark) {
714+
return this._axisMark.getProduct()?.getGroupGraphicItem()?.attribute.items;
715+
}
716+
}
707717
}
708718

709719
export const registerAxis = () => {

packages/vchart/src/component/axis/cartesian/axis.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ICartesianHorizontal } from './interface/spec';
22
import { Bounds, last, type IBounds, type IBoundsLike, type Maybe } from '@visactor/vutils';
33
// eslint-disable-next-line no-duplicate-imports
44
import type { IEffect, IModelInitOption, IModelSpecInfo } from '../../../model/interface';
5-
import { SeriesTypeEnum, type ICartesianSeries } from '../../../series/interface';
5+
import type { ICartesianSeries } from '../../../series/interface';
66
import type { IRegion } from '../../../region/interface';
77
import type { ICartesianAxisCommonSpec, IAxisHelper, ICartesianVertical } from './interface';
88
import { mergeSpec } from '@visactor/vutils-extension';
@@ -50,7 +50,6 @@ import type { IGraphic, IText } from '@visactor/vrender-core';
5050
// eslint-disable-next-line no-duplicate-imports
5151
import { createText } from '@visactor/vrender-core';
5252
import type { ICartesianChartSpec } from '../../../chart/cartesian/interface';
53-
import { STACK_FIELD_END, STACK_FIELD_START } from '../../../constant/data';
5453

5554
const CartesianAxisPlugin = [AxisSyncPlugin];
5655

packages/vchart/src/component/base/base-component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CustomEvent, type IGraphic, type IGroup, type INode } from '@visactor/vrender-core';
1+
import { CustomEvent, type IGraphicAttribute, type IGraphic, type IGroup, type INode } from '@visactor/vrender-core';
22
import type { IRegion } from '../../region/interface';
33
import type { IComponent, IComponentOption } from '../interface';
44
import { ComponentPluginService } from '../../plugin/components/plugin-service';
@@ -178,4 +178,9 @@ export class BaseComponent<T extends IComponentSpec = IComponentSpec> extends La
178178
getBoundsInRect(rect: ILayoutRect, fullRect: ILayoutRect): IBoundsLike {
179179
return { x1: 0, x2: 0, y1: 0, y2: 0 };
180180
}
181+
182+
getDatum(graphic?: IGraphic<Partial<IGraphicAttribute>>) {
183+
// override
184+
return;
185+
}
181186
}

packages/vchart/src/component/interface/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export interface IComponent extends ILayoutModel {
4545

4646
// 清空,用于更新等场景
4747
clear: () => void;
48+
49+
// 数据
50+
getDatum: (childGraphic?: IGraphic) => any | undefined;
4851
}
4952

5053
export interface IComponentConstructor extends IModelConstructor {

packages/vchart/src/event/event-dispatcher.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { CompilerListenerParameters } from '../compile/interface';
2020
import type { Compiler } from '../compile/compiler';
2121
import type { StringOrNumber } from '../typings';
2222
import type { IElement } from '@visactor/vgrammar-core';
23+
import type { IComponent } from '../component/interface';
2324
import { Factory as VGrammarFactory } from '@visactor/vgrammar-core';
2425

2526
const componentTypeMap: Record<string, string> = {
@@ -301,11 +302,18 @@ export class EventDispatcher implements IEventDispatcher {
301302
targetMark = targetMark.group;
302303
}
303304

305+
const node = get(listenerParams.event, 'target');
306+
307+
let datum = listenerParams.datum;
308+
if (model && model.modelType === 'component') {
309+
datum = (model as IComponent).getDatum(node) ?? datum;
310+
}
311+
304312
const params: BaseEventParams = {
305313
event: listenerParams.event,
306314
item: listenerParams.item,
307-
datum: listenerParams.datum,
308315
source: listenerParams.source,
316+
datum,
309317
itemMap,
310318
chart,
311319
model,
@@ -326,7 +334,6 @@ export class EventDispatcher implements IEventDispatcher {
326334
if ((event as any).elements) {
327335
items = (event as any).elements;
328336
}
329-
330337
const params: InteractionEventParam = {
331338
event: listenerParams.event,
332339
chart,

0 commit comments

Comments
 (0)