React如何以Hook的方式使用Echarts
背景
目前組內(nèi)在開(kāi)發(fā)的項(xiàng)目使用的雖然是React,但是并不是之前的類式組件,所以寫(xiě)法有不少的區(qū)別
經(jīng)過(guò)查閱之后,現(xiàn)以Hook的方式使用Echarts,在此進(jìn)行總結(jié),方便之后的學(xué)習(xí)。
步驟
安裝Echarts
首先引入Echarts的模塊
npm install echarts --save
如果你使用的是 Typescript ,那么還需要引入 @types/echarts
npm install @types/echarts --save
代碼中引入Echarts
有兩種方式,二選其一,按需引入或全部引入,各有優(yōu)劣,各位可自行選擇
按需引入
代碼如下,使用了echarts,line,tooltip,legend
import echarts from 'echarts/lib/echarts' import 'echarts/lib/chart/line' import 'echarts/lib/component/title' import 'echarts/lib/component/tooltip'
按需引入的優(yōu)點(diǎn)是最后打包生成的文件較小,一般會(huì)采用這種方法
全部引入
import echarts from 'echarts'
使用Echarts
React自從新增了Hook特性以后,使用函數(shù)式組件,需要改動(dòng)一定的地方。
使用useEffect代替之前的初始化函數(shù)
提示
- 如果你熟悉 React class 的生命周期函數(shù)
- 你可以把 useEffect Hook 看做 componentDidMount,componentDidUpdate 和 componentWillUnmount 這三個(gè)函數(shù)的組合。
初始化圖表
使用getElementById初始化
綜上,可以在 useEffect 中寫(xiě)原先在 componentDidMount() 的初始化圖表的函數(shù)
let element = document.getElementById('main');
let myChart = echarts.init(element as HTMLDivElement);
let option = {
title: {
text: 'ECharts 入門示例',
},
tooltip: {
},
legend: {
data:['銷量', '利潤(rùn)', '比率']
},
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {
},
series: [
{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
},
{
name: '利潤(rùn)',
type: 'bar',
data: [30, 25, 15, 20, 20, 35]
},
{
name: '比率',
type: 'line',
data: [35, 30, 20, 25, 25, 40]
}]
};
myChart.setOption(option);
DOM中代碼如下
<div>
<div id={'main'} style={{height: 400}}/>
</div>
使用Ref引用初始化
除了上述的使用getElementById的方法以外,還可以使用ref引用的方式,如以下代碼所示,最后在useEffect里調(diào)用renderChart
const main2 = useRef(null);
let chartInstance = null;
let renderChart = () => {
const myChart = echarts.getInstanceByDom(main2.current as unknown as HTMLDivElement);
if(myChart)
chartInstance = myChart;
else
chartInstance = echarts.init(main2.current as unknown as HTMLDivElement);
chartInstance.setOption({
title: {
text: '燃盡圖'
},
tooltip: {
trigger: 'axis'
},
toolbox: {
feature: {
saveAsImage: {}
}
},
legend: {
data: ['Remain', 'Ideal']
},
xAxis: {
boundaryGap: false,
type: 'category',
data: ['1-1', '1-2', '1-3', '1-5', '1-6', '1-7', '1-8', '1-9']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Remain',
data: [140, 110, 100, 90, 70, 30, 10, 0],
type: 'line'
},
{
name: 'Ideal',
data: [140, 120, 100, 80, 60, 40, 20, 0],
type: 'line'
}
]
})
};
DOM中代碼如下
<div>
<div style={{height: 400}} ref={main2}/>
</div>
整體代碼
整體的代碼如下所示
import React, {useEffect, useRef, useState} from "react";
import echarts from 'echarts';
const Test: React.FC = () => {
const main2 = useRef(null);
let chartInstance = null;
let renderChart = () => {
const myChart = echarts.getInstanceByDom(main2.current as unknown as HTMLDivElement);
if(myChart)
chartInstance = myChart;
else
chartInstance = echarts.init(main2.current as unknown as HTMLDivElement);
chartInstance.setOption({
title: {
text: '燃盡圖'
},
tooltip: {
trigger: 'axis'
},
toolbox: {
feature: {
saveAsImage: {}
}
},
legend: {
data: ['Remain', 'Ideal']
},
xAxis: {
boundaryGap: false,
type: 'category',
data: ['1-1', '1-2', '1-3', '1-5', '1-6', '1-7', '1-8', '1-9']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Remain',
data: [140, 110, 100, 90, 70, 30, 10, 0],
type: 'line'
},
{
name: 'Ideal',
data: [140, 120, 100, 80, 60, 40, 20, 0],
type: 'line'
}
]
})
};
let initChart = () => {
let element = document.getElementById('main');
let myChart = echarts.init(element as HTMLDivElement);
let option = {
title: {
text: 'ECharts 入門示例',
},
tooltip: {
},
legend: {
data:['銷量', '利潤(rùn)', '比率']
},
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {
},
series: [
{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
},
{
name: '利潤(rùn)',
type: 'bar',
data: [30, 25, 15, 20, 20, 35]
},
{
name: '比率',
type: 'line',
data: [35, 30, 20, 25, 25, 40]
}]
};
myChart.setOption(option);
};
useEffect(() => {
initChart();
renderChart();
});
return(
<div>
<div id={'main'} style={{height: 400}}/>
<div style={{height: 400}} ref={main2}/>
</div>
);
};
export default Test
最終效果
最后的結(jié)果如下圖所示

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談React Native 傳參的幾種方式(小結(jié))
這篇文章主要介紹了淺談React Native 傳參的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
React項(xiàng)目經(jīng)驗(yàn)總結(jié)及遇到的坑
這篇文章主要介紹了React項(xiàng)目經(jīng)驗(yàn)總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
ReactNative頁(yè)面跳轉(zhuǎn)實(shí)例代碼
這篇文章主要介紹了ReactNative頁(yè)面跳轉(zhuǎn)的代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
React實(shí)現(xiàn)一個(gè)拖拽排序組件的示例代碼
這篇文章主要給大家介紹了React實(shí)現(xiàn)一個(gè)拖拽排序組件?-?支持多行多列、支持TypeScript、支持Flip動(dòng)畫(huà)、可自定義拖拽區(qū)域,文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
React Ref Callback使用場(chǎng)景最佳實(shí)踐詳解
這篇文章主要為大家介紹了React Ref Callback使用場(chǎng)景最佳實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
使用react-router4.0實(shí)現(xiàn)重定向和404功能的方法
本篇文章主要介紹了使用react-router4.0實(shí)現(xiàn)重定向和404功能的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
使用react+redux實(shí)現(xiàn)計(jì)數(shù)器功能及遇到問(wèn)題
使用redux管理數(shù)據(jù),由于Store獨(dú)立于組件,使得數(shù)據(jù)管理獨(dú)立于組件,解決了組件之間傳遞數(shù)據(jù)困難的問(wèn)題,非常好用,今天重點(diǎn)給大家介紹使用react+redux實(shí)現(xiàn)計(jì)數(shù)器功能及遇到問(wèn)題,感興趣的朋友參考下吧2021-06-06

