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(event): emit plot events #5034

Merged
merged 1 commit into from
May 16, 2023
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
8 changes: 8 additions & 0 deletions __tests__/integration/api-chart-on-item-element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ describe('chart.on', () => {
await fired;
});

it('chart.on("plot:click", callback) should emit plot events', async () => {
await finished;
const [fired, resolve] = createPromise();
chart.on(`plot:${ChartEvent.CLICK}`, () => resolve());
dispatchFirstElementEvent(canvas, 'click', { detail: 1 });
await fired;
});

afterAll(() => {
canvas?.destroy();
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/plots/api/chart-on-item-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function chartOnItemElement(context) {

chart.on('interval:click', log('interval:click'));
chart.on('element:click', log('element:click'));
chart.on('plot:click', () => console.log('plot:click'));
chart.on('interval:dblclick', log('interval:dblclick'));

chart.on('interval:pointertap', log('interval:pointertap'));
Expand Down
6 changes: 6 additions & 0 deletions site/docs/manual/event.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ chart.on(`interval:${ChartEvent.CLICK}`, (ev) => {
});
```

- 监听 Plot 区域事件

```js
chart.on('plot:click', (event) => console.log(event));
```

### 点击事件

| 事件名 | 说明 | 回调参数 |
Expand Down
14 changes: 11 additions & 3 deletions src/interaction/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
if (!predicate(e)) return;
const { target } = e;
const { className: elementType, markType } = target;

// Emit plot events.
emitter.emit(`plot:${eventType}`, e);

// If target area is plot area, do not emit extra events.
if (elementType === 'plot') return;

// Emit wrapped events.
if (elementType === 'element') {
const e1 = {
...e,
Expand All @@ -27,10 +35,10 @@ function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
};
emitter.emit(`element:${eventType}`, e1);
emitter.emit(`${markType}:${eventType}`, e1);
return;
} else {
// @todo Handle click axis and legend.
emitter.emit(`${elementType}:${eventType}`, e);
}
// @todo Handle click axis and legend.
emitter.emit(`${elementType}:click`);
};
}

Expand Down