mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-20 06:34:55 +00:00
feat(chat-sdk/chatitem): 消息支持导出图表图片 (#1937)
This commit is contained in:
41
webapp/packages/chat-sdk/src/hooks/useExportByEcharts.ts
Normal file
41
webapp/packages/chat-sdk/src/hooks/useExportByEcharts.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { message } from 'antd';
|
||||
import { ECharts } from 'echarts';
|
||||
|
||||
export interface ExportByEchartsProps {
|
||||
instanceRef: React.MutableRefObject<ECharts | undefined>;
|
||||
question: string;
|
||||
options?: Parameters<ECharts['getConnectedDataURL']>[0];
|
||||
}
|
||||
|
||||
export const useExportByEcharts = ({ instanceRef, question, options }: ExportByEchartsProps) => {
|
||||
const handleSaveAsImage = () => {
|
||||
if (instanceRef.current) {
|
||||
return instanceRef.current.getConnectedDataURL({
|
||||
type: 'png',
|
||||
pixelRatio: 2,
|
||||
backgroundColor: '#fff',
|
||||
excludeComponents: ['toolbox'],
|
||||
...options,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const downloadImage = (url: string) => {
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${question}.png`;
|
||||
a.click();
|
||||
};
|
||||
|
||||
const downloadChartAsImage = () => {
|
||||
const url = handleSaveAsImage();
|
||||
if (url) {
|
||||
downloadImage(url);
|
||||
message.success('导出图片成功');
|
||||
} else {
|
||||
message.error('该条消息暂不支持导出图片');
|
||||
}
|
||||
};
|
||||
|
||||
return { downloadChartAsImage };
|
||||
};
|
||||
Reference in New Issue
Block a user