Skip to content

fix: ref unable to obtain chart instance #2918

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

Merged
merged 1 commit into from
May 25, 2025
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
17 changes: 14 additions & 3 deletions packages/plots/src/components/base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ export const BaseChart: ForwardRefExoticComponent<PropsWithoutRef<CommonConfig>
loading,
loadingTemplate,
errorTemplate,
onReady,
...rest
} = config;

const { chart, container } = useChart<Chart, CommonConfig>(Plots[chartType], rest);

useImperativeHandle(ref, () => chart.current!, [chart.current]);
const { chart, container } = useChart<Chart, CommonConfig>(Plots[chartType], {
...rest,
onReady: (chartInstance) => {
if (ref) {
if (typeof ref === 'function') {
ref(chartInstance);
} else {
(ref as React.MutableRefObject<Chart>).current = chartInstance;
}
}
onReady?.(chartInstance);
},
});

return (
<ErrorBoundary errorTemplate={errorTemplate}>
Expand Down
45 changes: 45 additions & 0 deletions packages/plots/tests/plots/ref-spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { render } from '@ant-design/charts-util';
import React, { act } from 'react';
import { Column } from '../../src';

const ref = React.createRef();

describe('get chart instance by ref', () => {
let container;
const data = [
{
date: '2010-01',
scales: 1998,
},
{
date: '2010-02',
scales: 1850,
},
{
date: '2010-03',
scales: 1250,
},
];
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('chart render', async () => {
const config = {
data,
xField: 'date',
yField: 'scales',
width: 600,
height: 300,
};
act(() => {
render(<Column {...config} ref={ref} />, container);
});
expect(ref.current).not.toBeUndefined();
});
});
25 changes: 25 additions & 0 deletions site/docs/manual/getting-started.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,28 @@ const Page: React.FC = () => {
};
export default Page;
```

## 获取图表实例

图表实例用于后续事件监听、绑定、图表下载等,目前提供 2 种获取方式。

```tsx | pure
// 通过 onReady 配置获取
const config = {
onReady: ( plot ) => {
const { chart } = plot;
chart.on('element:click', (evt) => {
console.log(evt.target)
})
}
}

// 通过 ref 获取
const ref = useRef(null);
return (
<div>
<button onClick={() => console.log(ref.current)}>点我</button>
<Line {...config} ref={ref} />
</div>
);
```