Echarts折線圖如何根據(jù)容器寬度自適應(yīng)展示
需求描述
如上圖所見,在開發(fā)過程中經(jīng)常會遇到容器自適應(yīng)展示的功能,那么如果在需要自適應(yīng)寬高的容器內(nèi),echarts折線圖要怎么處理呢?
改造前代碼實現(xiàn)流程
import React, { useRef, useEffect } from 'react'; import ReactDom from 'react-dom'; import * as echarts from 'echarts'; const Test = () => { const divRef = useRef < HTMLDivElement > (null); const chart = useRef < echarts.ECharts > (null); const fontColor = "#30eee9"; useEffect(() => { if (!divRef.current) return; let myChart = chart.current; if (!myChart) { chart.current = myChart; myChart = echarts.init(divRef.current); const option = { backgroundColor: "#11183c", grid: { left: "5%", right: "10%", top: "20%", bottom: "15%", containLabel: true, }, tooltip: { show: true, trigger: "item", }, legend: { show: true, x: "center", y: "35", icon: "stack", itemWidth: 10, itemHeight: 10, textStyle: { color: "#1bb4f6", }, data: ["已采納", "已發(fā)布"], }, xAxis: [ { type: "category", boundaryGap: false, axisLabel: { color: fontColor, }, axisLine: { show: true, lineStyle: { color: "#397cbc", }, }, axisTick: { show: false, }, splitLine: { show: false, lineStyle: { color: "#195384", }, }, data: [ "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", ], }, ], yAxis: [ { type: "value", name: "", min: 0, max: 1000, axisLabel: { formatter: "{value}", textStyle: { color: "#2ad1d2", }, }, axisLine: { lineStyle: { color: "#27b4c2", }, }, axisTick: { show: false, }, splitLine: { show: true, lineStyle: { color: "#11366e", }, }, }, ], series: [ { name: "已采納", type: "line", stack: "總量", symbol: "circle", symbolSize: 8, itemStyle: { normal: { color: "#0092f6", lineStyle: { color: "#0092f6", width: 1, } }, }, markPoint: { itemStyle: { normal: { color: "red", }, }, }, data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330], }, { name: "已發(fā)布", type: "line", stack: "總量", symbol: "circle", symbolSize: 8, itemStyle: { normal: { color: "#00d4c7", lineStyle: { color: "#00d4c7", width: 1, }, }, }, data: [220, 182, 191, 234, 290, 330, 310, 201, 154, 190, 330, 410], }, ], }; option && myChart.setOption(option); } }, []); return <div style={{ width: '100%', height: 280 }} ref={divRef}/> };
代碼思路
以上的功能實現(xiàn)是基于react 和 react hook,在頁面展示前先獲取dom元素,如果dom成功獲取到以后,為這個dom插入option相關(guān)的配置,那么現(xiàn)在并沒有根據(jù)容器的寬度自適應(yīng)的顯示折線圖,現(xiàn)在是把獲取dom和渲染的方法放在了useEfft中,那么要如何監(jiān)聽自身的容器寬高,并動態(tài)的渲染變化呢??
處理方案
使用ResizeObserver對象來監(jiān)聽指定Dom元素(div…)的變化(width、height等等)。一般,瀏覽器不支持該監(jiān)聽對象,所以需要引入第三方庫支持-resize-observer-polyfill
第一步,安裝 npm install resize-observer-polyfill;
第二步,頁面中引入 import ResizeObserver from 'resize-observer-polyfill';
第三步, 利用 ResizeObserver 對渲染折線圖的dom進(jìn)行監(jiān)聽,如果監(jiān)聽到渲染折線圖所在的dom元素寬高發(fā)生變化,利用echarts本身提供的resize事件重繪。
第四部, 頁面銷毀時,取消所有被ResizeObserver對象監(jiān)聽的節(jié)點
最終代碼
import React, { useRef, useEffect } from 'react'; import ReactDom from 'react-dom'; import * as echarts from 'echarts'; // 關(guān)鍵代碼 import ResizeObserver from 'resize-observer-polyfill'; const Test = () => { const divRef = useRef < HTMLDivElement > (null); const chart = useRef < echarts.ECharts > (null); // 關(guān)鍵代碼 const myObserver = useRef < ResizeObserver > (null); const fontColor = "#30eee9"; useEffect(() => { if (!divRef.current) return; let myChart = chart.current; if (!myChart) { chart.current = myChart; myChart = echarts.init(divRef.current); const option = { backgroundColor: "#11183c", grid: { left: "5%", right: "10%", top: "20%", bottom: "15%", containLabel: true, }, tooltip: { show: true, trigger: "item", }, legend: { show: true, x: "center", y: "35", icon: "stack", itemWidth: 10, itemHeight: 10, textStyle: { color: "#1bb4f6", }, data: ["已采納", "已發(fā)布"], }, xAxis: [ { type: "category", boundaryGap: false, axisLabel: { color: fontColor, }, axisLine: { show: true, lineStyle: { color: "#397cbc", }, }, axisTick: { show: false, }, splitLine: { show: false, lineStyle: { color: "#195384", }, }, data: [ "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", ], }, ], yAxis: [ { type: "value", name: "", min: 0, max: 1000, axisLabel: { formatter: "{value}", textStyle: { color: "#2ad1d2", }, }, axisLine: { lineStyle: { color: "#27b4c2", }, }, axisTick: { show: false, }, splitLine: { show: true, lineStyle: { color: "#11366e", }, }, }, ], series: [ { name: "已采納", type: "line", stack: "總量", symbol: "circle", symbolSize: 8, itemStyle: { normal: { color: "#0092f6", lineStyle: { color: "#0092f6", width: 1, } }, }, markPoint: { itemStyle: { normal: { color: "red", }, }, }, data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330], }, { name: "已發(fā)布", type: "line", stack: "總量", symbol: "circle", symbolSize: 8, itemStyle: { normal: { color: "#00d4c7", lineStyle: { color: "#00d4c7", width: 1, }, }, }, data: [220, 182, 191, 234, 290, 330, 310, 201, 154, 190, 330, 410], }, ], }; //關(guān)鍵代碼, 利用observe監(jiān)聽折線圖所在容器的寬度變化(divRef.current),在ResizeObserver回掉函數(shù)中entries返回的內(nèi)容可以獲取的到;最后將獲取到寬度賦值給resize方法進(jìn)行重繪 myObserver.current = new ResizeObserver((entries) => { myChart?.resize(entries[0]?.contentRect.width); }); myObserver.current.observe(divRef.current); myChart.setOption(option); } //關(guān)鍵代碼 return () => { if (chart?.current) chart.current.dispose(); //取消所有被ResizeObserver對象監(jiān)聽的節(jié)點 myObserver.current?.disconnect(); }; }, []); return ( <div className='line' style={{ width: '100%', height: 280 }} ref={divRef} /> ) };
總結(jié)
到此這篇關(guān)于Echarts折線圖如何根據(jù)容器寬度自適應(yīng)展示的文章就介紹到這了,更多相關(guān)Echarts折線圖寬度自適應(yīng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實現(xiàn)經(jīng)典貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)經(jīng)典貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Object.keys() 和 Object.getOwnPropertyNames() 的區(qū)別詳解
這篇文章主要介紹了Object.keys() 和 Object.getOwnPropertyNames() 的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05通過JavaScript實現(xiàn)動態(tài)圣誕樹詳解
這篇文章主要為大家介紹幾個好看的基于HTML+CSS+JS的圣誕樹,希望圣誕節(jié)那天圣誕老爺爺能把我喜歡的你塞到我床上。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12用js通過url傳參把數(shù)據(jù)從一個頁面?zhèn)鞯搅硪粋€頁面
如果是傳到新頁面的話,你網(wǎng)站基于什么語言開發(fā)直接用get或者post獲取,然后輸出到這個層2014-09-09微信小程序?qū)崿F(xiàn)天氣預(yù)報功能(附源碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)天氣預(yù)報功能(附源碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12