React19通用ECharts組件的使用示例
好,我給你做一個 React 19 通用 ECharts 組件,
支持餅圖 / 折線圖 / 柱狀圖,colors 可選,不傳也能顯示,防止重復(fù)渲染。
1.src/services/echarts.ts
注冊所有需要的圖表類型和組件(不用再分開寫折線/餅圖/柱狀圖)
// 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';
// 按需注冊組件
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
ToolboxComponent,
LineChart,
PieChart,
BarChart,
CanvasRenderer
]);
export default echarts;
2.src/components/EChartsBase.tsx
一個通用的 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) {
// 如果已有實例,先銷毀,防止 React 19 重復(fù)渲染
if (chartInstance.current) {
chartInstance.current.dispose();
}
chartInstance.current = echarts.init(chartRef.current);
chartInstance.current.setOption(option);
}
// 監(jiān)聽窗口大小變化
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: '訪問來源',
type: 'pie',
radius: '50%',
data: [
{ value: 1048, name: '搜索引擎' },
{ value: 735, name: '直接訪問' },
{ value: 580, name: '郵件營銷' }
]
}
]
};
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: ['蘋果', '香蕉', '橘子', '梨子'] },
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>
);
}
? 特點:
- React 19 兼容
- 餅圖 / 折線圖 / 柱狀圖通用
colors可選,不傳也正常顯示- 自動銷毀實例,避免 React 19 重復(fù)渲染導(dǎo)致的圖表不顯示
- 自動響應(yīng)窗口大小變化
到此這篇關(guān)于React19通用ECharts組件的使用示例的文章就介紹到這了,更多相關(guān)React通用ECharts內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react-native 封裝視頻播放器react-native-video的使用
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
字節(jié)封裝React組件手機號自動校驗格式FormItem
這篇文章主要為大家介紹了字節(jié)封裝React組件手機號自動校驗格式FormItem,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
React-Hook中使用useEffect清除定時器的實現(xiàn)方法
這篇文章主要介紹了React-Hook中useEffect詳解(使用useEffect清除定時器),主要介紹了useEffect的功能以及使用方法,還有如何使用他清除定時器,需要的朋友可以參考下2022-11-11

