Skip to content

Commit

Permalink
feat(event): label click event (#6012)
Browse files Browse the repository at this point in the history
Co-authored-by: shield2018 <>
  • Loading branch information
shield2018 authored Jan 5, 2024
1 parent ed5e780 commit fb0911c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
27 changes: 27 additions & 0 deletions __tests__/integration/api-chart-on-label-click.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { chartOnLabelClick as render } from '../plots/api/chart-on-label-click';
import { ChartEvent } from '../../src';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import { dispatchFirstShapeEvent, createPromise } from './utils/event';
import './utils/useSnapshotMatchers';

describe('chart.on', () => {
const canvas = createNodeGCanvas(640, 480);
const { finished, chart } = render({ canvas });

chart.off();

it('chart.on("label:click", callback) should provide datum for item element', async () => {
await finished;
const [fired, resolve] = createPromise();
chart.on(`label:${ChartEvent.CLICK}`, (event) => {
expect(event.data).toEqual({ data: { text: 'A', value: 12000, c: 's' } });
resolve();
});
dispatchFirstShapeEvent(canvas, 'label', 'click', { detail: 1 });
await fired;
});

afterAll(() => {
canvas?.destroy();
});
});
28 changes: 28 additions & 0 deletions __tests__/plots/api/chart-on-label-click.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Chart } from '../../../src';

export function chartOnLabelClick(context) {
const { container, canvas } = context;
const data = [
{ text: 'A', value: 12000, c: 's' },
{ text: 'B', value: 9800 },
{ text: 'C', value: 6789 },
{ text: 'D', value: 4569 },
];
const chart = new Chart({ container, canvas });
chart
.interval()
.data(data)
.encode('x', 'text')
.encode('y', 'value')
.legend(false)
.label({
text: 'label',
cursor: 'pointer',
});

chart.on('label:click', (e) => console.log('click label', e, e.data));

const finished = chart.render();

return { chart, finished };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ export { chartOptionsCallbackChildren } from './chart-options-callback-children'
export { chartAutoFitSlider } from './chart-auto-fit-slider';
export { chart3d } from './chart-3d';
export { chartOnScrollbarFilter } from './chart-on-scrollbar-filter';
export { chartOnLabelClick } from './chart-on-label-click';
6 changes: 6 additions & 0 deletions site/docs/manual/extra-topics/event.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ chart.on('plot:click', (event) => console.log(event));
chart.on('component:click', (event) => console.log(event));
```

* Listen to global label events

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

### Click event

| Event name | Explanation | Callback parameters |
Expand Down
6 changes: 6 additions & 0 deletions site/docs/manual/extra-topics/event.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ chart.on('plot:click', (event) => console.log(event));
chart.on('component:click', (event) => console.log(event));
```

- 监听全局 label 事件

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

### 点击事件

| 事件名 | 说明 | 回调参数 |
Expand Down
27 changes: 20 additions & 7 deletions src/interaction/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function maybeElementRoot(node) {
return maybeRoot(node, (node) => node.className === 'element');
}

// For extended label.
function maybeLabelRoot(node) {
return maybeRoot(node, (node) => node.className === 'label');
}

function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
return (e) => {
if (!predicate(e)) return;
Expand All @@ -52,19 +57,27 @@ function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
const elementRoot = maybeElementRoot(target);
// If target is component or child of component.
const componentRoot = maybeComponentRoot(target);
const root = elementRoot || componentRoot;
// If target is babel or child of babel.
const babelRoot = maybeLabelRoot(target);

const root = elementRoot || componentRoot || babelRoot;

if (!root) return;
const { className: elementType, markType } = root;
const e1 = {
...e,
nativeEvent: true,
};
if (elementType === 'element') {
const e1 = {
...e,
nativeEvent: true,
data: { data: dataOf(root, view) },
};
e1['data'] = { data: dataOf(root, view) };
emitter.emit(`element:${eventType}`, e1);
emitter.emit(`${markType}:${eventType}`, e1);
} else if (elementType === 'label') {
//label children [Text2, Rect2, Path2],
e1['data'] = { data: root.attributes.datum };
emitter.emit(`label:${eventType}`, e1);
emitter.emit(`${className}:${eventType}`, e1);
} else {
const e1 = { ...e, nativeEvent: true };
emitter.emit(`component:${eventType}`, e1);
emitter.emit(`${className}:${eventType}`, e1);
}
Expand Down

0 comments on commit fb0911c

Please sign in to comment.