-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 增加对echarts图表的支持 * 增加对echarts的支持
- Loading branch information
Showing
6 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,4 @@ | |
"maxToken": 16000, | ||
"price": 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// EChartsCodeBlock.tsx | ||
|
||
import React, { useMemo } from 'react'; | ||
import EChartsRenderer from './EChartsRenderer'; | ||
import * as echarts from 'echarts'; | ||
|
||
|
||
interface EChartsCodeBlockProps { | ||
code: string; | ||
} | ||
|
||
const EChartsCodeBlock: React.FC<EChartsCodeBlockProps> = ({ code }) => { | ||
const getOption = useMemo(() => { | ||
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; | ||
} | ||
}, [code]); | ||
|
||
return getOption ? <EChartsRenderer getOption={getOption} /> : null; | ||
}; | ||
|
||
export default EChartsCodeBlock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useRef, useEffect } from 'react'; | ||
import { ECharts } from 'echarts'; | ||
|
||
interface EChartsRendererProps { | ||
getOption: () => any; | ||
} | ||
|
||
|
||
const EChartsRenderer: React.FC<EChartsRendererProps> = ({ getOption }) => { | ||
const chartRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
let chartInstance: ECharts | null = null; | ||
|
||
(async () => { | ||
const echarts = await import('echarts'); | ||
await import('echarts-gl'); | ||
if (chartRef.current) { | ||
chartInstance = echarts.init(chartRef.current, { renderer: 'svg', ssr: true }); | ||
const option = getOption(); | ||
chartInstance.setOption(option); | ||
} | ||
})(); | ||
|
||
return () => { | ||
if (chartInstance) { | ||
chartInstance.dispose(); | ||
} | ||
}; | ||
}, [getOption]); | ||
|
||
return <div style={{ width: '100%', height: '400px', minWidth: '600px' }} ref={chartRef} />; | ||
}; | ||
|
||
export default EChartsRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'echarts-gl' { | ||
export default echarts-gl | ||
} | ||
|