React19通用ECharts組件的使用示例
好,我給你做一個(gè) React 19 通用 ECharts 組件,
支持餅圖 / 折線圖 / 柱狀圖,colors 可選,不傳也能顯示,防止重復(fù)渲染。
1.src/services/echarts.ts
注冊(cè)所有需要的圖表類(lèi)型和組件(不用再分開(kāi)寫(xiě)折線/餅圖/柱狀圖)
// src/services/echarts.ts
import * as echarts from 'echarts/core';
import {
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
ToolboxComponent
} from 'echarts/components';
import { LineChart, PieChart, BarChart } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';
// 按需注冊(cè)組件
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
ToolboxComponent,
LineChart,
PieChart,
BarChart,
CanvasRenderer
]);
export default echarts;
2.src/components/EChartsBase.tsx
一個(gè)通用的 React 19 ECharts 組件
// src/components/EChartsBase.tsx
import React, { useRef, useEffect } from 'react';
import echarts from '@/services/echarts';
interface EChartsBaseProps {
option: echarts.EChartsOption;
width?: string;
height?: string;
}
const EChartsBase: React.FC<EChartsBaseProps> = ({
option,
width = '100%',
height = '400px'
}) => {
const chartRef = useRef<HTMLDivElement | null>(null);
const chartInstance = useRef<echarts.EChartsType | null>(null);
useEffect(() => {
if (chartRef.current) {
// 如果已有實(shí)例,先銷(xiāo)毀,防止 React 19 重復(fù)渲染
if (chartInstance.current) {
chartInstance.current.dispose();
}
chartInstance.current = echarts.init(chartRef.current);
chartInstance.current.setOption(option);
}
// 監(jiān)聽(tīng)窗口大小變化
const handleResize = () => {
chartInstance.current?.resize();
};
window.addEventListener('resize', handleResize);
return () => {
chartInstance.current?.dispose();
window.removeEventListener('resize', handleResize);
};
}, [option]);
return <div ref={chartRef} style={{ width, height }} />;
};
export default EChartsBase;
3. 使用示例(不傳colors也能顯示)
// src/pages/DemoCharts.tsx
import React from 'react';
import EChartsBase from '@/components/EChartsBase';
export default function DemoCharts() {
const pieOption = {
title: { text: '示例餅圖', left: 'center' },
tooltip: { trigger: 'item' },
legend: { bottom: 0 },
series: [
{
name: '訪問(wèn)來(lái)源',
type: 'pie',
radius: '50%',
data: [
{ value: 1048, name: '搜索引擎' },
{ value: 735, name: '直接訪問(wèn)' },
{ value: 580, name: '郵件營(yíng)銷(xiāo)' }
]
}
]
};
const lineOption = {
title: { text: '示例折線圖' },
tooltip: { trigger: 'axis' },
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
yAxis: { type: 'value' },
series: [
{ data: [150, 230, 224, 218, 135, 147, 260], type: 'line', smooth: true }
]
};
const barOption = {
title: { text: '示例柱狀圖' },
tooltip: {},
xAxis: { type: 'category', data: ['蘋(píng)果', '香蕉', '橘子', '梨子'] },
yAxis: { type: 'value' },
series: [
{ data: [5, 20, 36, 10], type: 'bar' }
]
};
return (
<div style={{ padding: 20 }}>
<EChartsBase option={pieOption} height="300px" />
<EChartsBase option={lineOption} height="300px" />
<EChartsBase option={barOption} height="300px" />
</div>
);
}
? 特點(diǎn):
- React 19 兼容
- 餅圖 / 折線圖 / 柱狀圖通用
colors可選,不傳也正常顯示- 自動(dòng)銷(xiāo)毀實(shí)例,避免 React 19 重復(fù)渲染導(dǎo)致的圖表不顯示
- 自動(dòng)響應(yīng)窗口大小變化
到此這篇關(guān)于React19通用ECharts組件的使用示例的文章就介紹到這了,更多相關(guān)React通用ECharts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- React?+?ECharts動(dòng)態(tài)折線圖完整實(shí)現(xiàn)代碼
- 使用react+echarts實(shí)現(xiàn)變化趨勢(shì)縮略圖
- React中如何使用echarts寫(xiě)出3d旋轉(zhuǎn)扇形圖
- React?echarts?組件的封裝使用案例
- React如何以Hook的方式使用Echarts
- react echarts刷新不顯示問(wèn)題的解決方法
- react中使用echarts,并實(shí)現(xiàn)tooltip循環(huán)輪播方式
- React項(xiàng)目搭建與Echarts工具使用詳解
- react使用echart繪制地圖的案例
- React-ChartJS?使用教程
相關(guān)文章
react-native 封裝視頻播放器react-native-video的使用
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
React中使用axios發(fā)送請(qǐng)求的幾種常用方法
本文主要介紹了React中使用axios發(fā)送請(qǐng)求的幾種常用方法,主要介紹了get和post請(qǐng)求,具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08
字節(jié)封裝React組件手機(jī)號(hào)自動(dòng)校驗(yàn)格式FormItem
這篇文章主要為大家介紹了字節(jié)封裝React組件手機(jī)號(hào)自動(dòng)校驗(yàn)格式FormItem,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
React使用Mobx6.x共享狀態(tài)問(wèn)題
這篇文章主要介紹了React使用Mobx6.x共享狀態(tài)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
React-Hook中使用useEffect清除定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了React-Hook中useEffect詳解(使用useEffect清除定時(shí)器),主要介紹了useEffect的功能以及使用方法,還有如何使用他清除定時(shí)器,需要的朋友可以參考下2022-11-11
React中ES5與ES6寫(xiě)法的區(qū)別總結(jié)
這篇文章主要總結(jié)介紹了關(guān)于React中ES5與ES6的寫(xiě)法區(qū)別,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04

