React?echarts?組件的封裝使用案例
React echarts 組件的封裝
import React, { useEffect, useRef } from 'react'; import { useSize, useDebounceEffect } from 'ahooks'; import LoopShowTooltip from './echartsTooltipLoop'; import * as echarts from 'echarts'; const CommonChart = props => { const { chartId, options, autoTooltip } = props; const chartRef = useRef(); const size = useSize(chartRef); const loopRef = useRef(); useEffect(() => { let chartDom; let myChart; if (loopRef.current) { loopRef.current?.clearLoop(); loopRef.current = null; } setTimeout(() => { if (loopRef.current) { loopRef.current?.clearLoop(); loopRef.current = null; } if (chartRef) { chartDom = chartRef.current; myChart = echarts.init(chartDom); options && myChart.setOption(options); if (autoTooltip) { loopRef.current = new LoopShowTooltip(myChart, options, {}); } } }); window.onresize = () => { myChart.resize(); }; return () => { window.onresize = null; loopRef?.current?.clearLoop(); loopRef.current = null; }; }, [chartId, options]); useDebounceEffect(() => { let myChart; let chartDom; if (chartRef) { chartDom = chartRef.current; myChart = echarts.init(chartDom); options && myChart.setOption(options); myChart.resize(); } window.onresize = () => { myChart.resize(); }; }, [size], { wait: 100, }); return <div ref={chartRef} style={{ width: '100%', height: '100%' }}></div>; }; export default CommonChart;
使用案例
import React from "react"; import CommonChart from './pages/CommonChart/UI' const Demo = () => { let echarData = [122,112,233,123,122,788,900]; let yAxisData = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日']; const chartOptions = { grid: { top: '8%', bottom: '15%', left: '30%', right: '16%', // containLabel: true, }, tooltip: { trigger: 'item', show: true, backgroundColor: '#3A3F4D', borderWidth: 0, textStyle: { // 提示框浮層的文本樣式。 color: '#B1B6C2', fontStyle: 'normal', fontWeight: 'normal', fontFamily: 'sans-serif', fontSize: 14, }, formatter: record => { let result = `${record.name}:${record.value} 次`; return result; }, }, xAxis: { type: 'value', boundaryGap: [0, 0.01], splitLine: { show: false, }, }, yAxis: { type: 'category', data: yAxisData, scale: true, axisTick: { // x軸刻度線 show: false, alignWithLabel: true, }, axisLabel: { interval: 0, width: 80, overflow: 'truncate', ellipsis: '...', align: 'left', margin: 80, }, axisLine: { // 坐標(biāo)軸 show: false, }, }, series: [ { name: '2011', type: 'bar', showBackground: true, backgroundStyle: { color: '#1A1E28', }, barWidth: 12, // 柱圖寬度 itemStyle: { normal: { // 柱狀圖上顯示數(shù)量 label: { show: true, // 是否顯示 position: [220, 0], // 位置 formatter: '{@value}' + '次', // 內(nèi)容 color: '#A5ADBA', // 文字顏色 }, color: '#2275F0', // 柱子顏色 }, }, data: echarData, }, ], }; return ( <div style={{height:300, width: 400}}> <CommonChart options={chartOptions} /> </div> ); }; export default Demo;
到此這篇關(guān)于React echarts 組件的封裝的文章就介紹到這了,更多相關(guān)React echarts 組件封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React中setState/useState的使用方法詳細(xì)介紹
這篇文章主要介紹了React中setState/useState的使用方法,useState 和 setState 在React開(kāi)發(fā)過(guò)程中 使用很頻繁,但很多人都停留在簡(jiǎn)單的使用階段,并沒(méi)有正在了解它們的執(zhí)行機(jī)制2023-04-04記React connect的幾種寫(xiě)法(小結(jié))
這篇文章主要介紹了記React connect的幾種寫(xiě)法(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09React實(shí)現(xiàn)生成和導(dǎo)出Word文檔的方法詳解
React是一個(gè)流行的JavaScript庫(kù),用于構(gòu)建現(xiàn)代前端應(yīng)用程序,本文將深入探討如何在React中生成和導(dǎo)出Word文檔,感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09React學(xué)習(xí)之事件綁定的幾種方法對(duì)比
這篇文章主要給大家介紹了關(guān)于React學(xué)習(xí)之事件綁定的幾種方法對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09利用React Router4實(shí)現(xiàn)的服務(wù)端直出渲染(SSR)
這篇文章主要介紹了利用React Router4實(shí)現(xiàn)的服務(wù)端直出渲染(SSR),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01解析React?ref?命令代替父子組件的數(shù)據(jù)傳遞問(wèn)題
這篇文章主要介紹了React?-?ref?命令為什么代替父子組件的數(shù)據(jù)傳遞,使用?ref?之后,我們不需要再進(jìn)行頻繁的父子傳遞了,子組件也可以有自己的私有狀態(tài)并且不會(huì)影響信息的正常需求,這是為什么呢?因?yàn)槲覀兪褂昧?ref?命令的話,ref是可以進(jìn)行狀態(tài)的傳輸2022-08-08