Skip to content

Commit

Permalink
adapt echarts
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Sep 4, 2023
1 parent 32f482b commit bd419a2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 55 deletions.
22 changes: 22 additions & 0 deletions client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/src/components/ChatBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ const ChatBox = (
return (
<Flex flexDirection={'column'} h={'100%'}>
<Box ref={ChatBoxRef} flex={'1 0 0'} h={0} w={'100%'} overflow={'overlay'} px={[4, 0]} pb={3}>
<Box maxW={['100%', '92%']} h={'100%'} mx={'auto'}>
<Box id="chat-container" maxW={['100%', '92%']} h={'100%'} mx={'auto'}>
{showEmpty && <Empty />}

{!!welcomeText && (
Expand Down
68 changes: 53 additions & 15 deletions client/src/components/Markdown/img/EChartsCodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
// EChartsCodeBlock.tsx

import React, { useMemo } from 'react';
import EChartsRenderer from './EChartsRenderer';
import React, { useEffect, useRef, useState } from 'react';
import * as echarts from 'echarts';
import type { ECharts } from 'echarts';
import { Box, Skeleton } from '@chakra-ui/react';

const EChartsCodeBlock = ({ code }: { code: string }) => {
const chartRef = useRef<HTMLDivElement>(null);
const eChart = useRef<ECharts>();
const [option, setOption] = useState<any>();
const [width, setWidth] = useState(400);

interface EChartsCodeBlockProps {
code: string;
}
useEffect(() => {
const clientWidth = document.getElementById('chat-container')?.clientWidth || 500;
setWidth(clientWidth * 0.9);
setTimeout(() => {
eChart.current?.resize();
}, 100);
}, []);

const EChartsCodeBlock: React.FC<EChartsCodeBlockProps> = ({ code }) => {
const getOption = useMemo(() => {
useEffect(() => {
let option;
try {
const optionFunction = new Function("echarts",'"use strict";' + code + '; return getChartOption;');
return optionFunction(echarts); // 添加 echarts 参数
} catch (error) {
console.error('Error parsing ECharts code:', '\n', error, '\n', 'Code:', code);
return null;
option = JSON.parse(code.trim());
option = {
...option,
toolbox: {
show: true,
feature: {
saveAsImage: {}
}
}
};
setOption(option);
} catch (error) {}

if (!option) return;

(async () => {
// @ts-ignore
await import('echarts-gl');
})();

if (chartRef.current) {
eChart.current = echarts.init(chartRef.current);
eChart.current.setOption(option);
eChart.current?.resize();
}

return () => {
if (eChart.current) {
eChart.current.dispose();
}
};
}, [code]);

return getOption ? <EChartsRenderer getOption={getOption} /> : null;
return (
<Box overflowX={'auto'}>
<Box h={'400px'} minW={'400px'} w={`${width}px`} ref={chartRef} />
{!option && <Skeleton isLoaded={true} fadeDuration={2} h={'400px'} w={`400px`}></Skeleton>}
</Box>
);
};

export default EChartsCodeBlock;
35 changes: 0 additions & 35 deletions client/src/components/Markdown/img/EChartsRenderer.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions client/src/types/echarts-gl.d.ts

This file was deleted.

0 comments on commit bd419a2

Please sign in to comment.