自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Vue和React中使用ECharts的多種方法

開發(fā)
俗話說:“工欲善其事,必先利其器”?,F(xiàn)如今已經(jīng)有許多成熟易用的可視化解決方案,例如ECharts,AntV等等。我們可以把這些解決方案比作是一套套成熟的“工具”,那我們?nèi)绾螌⑦@些“工具”應(yīng)用于當(dāng)前最熱門的兩個(gè)前端框架中呢?

 現(xiàn)在我們就以ECharts為例,來嘗試“工具”的各種用法。

Vue中運(yùn)用ECharts
首先我們要把ECharts下載下來:

  1. npm install echarts --save 

全局引用
全局引用的好處就是我們一次性把ECharts引入項(xiàng)目后,就可以在任何一個(gè)組件中使用ECharts了。

首先在項(xiàng)目的main.js中引入ECharts,然后將其綁定在vue的原型上面:

  1. import echarts from 'echarts' 
  2.  
  3. Vue.prototype.$echarts = echarts 

接下來我們就可以在自己想用ECharts的組件中引用了:

  1. <template> 
  2.   <div> 
  3.     <div id="myChart"></div> 
  4.   </div> 
  5. </template> 
  6.  
  7. <script> 
  8. export default
  9.   name'chart'
  10.   data () { 
  11.     return { 
  12.       chart: null
  13.       options: {} 
  14.     } 
  15.   }, 
  16.   mounted () { 
  17.     this.initOptions() 
  18.     this.initCharts() 
  19.   }, 
  20.   methods: { 
  21.     initOptions () { 
  22.       this.options = { 
  23.         xAxis: { 
  24.           type: 'category'
  25.           data: ['Mon''Tue''Wed''Thu''Fri''Sat''Sun'
  26.         }, 
  27.         yAxis: { 
  28.           type: 'value' 
  29.         }, 
  30.         series: [{ 
  31.           data: [820, 932, 901, 934, 1290, 1330, 1320], 
  32.           type: 'line' 
  33.         }] 
  34.       } 
  35.     }, 
  36.     initCharts () { 
  37.       this.chart = this.$echarts.init(document.getElementById('myChart')) 
  38.       this.chart.setOption(this.options) 
  39.     } 
  40.   } 
  41. </script> 
  42.  
  43. <style scoped> 
  44.   #myChart{ 
  45.     width: 400px; 
  46.     height: 400px; 
  47.   } 
  48. </style> 

看看效果:

 

按需引用
全局引用是把Echarts完整的引入,這樣做的缺點(diǎn)就是會(huì)額外的引入很多其他沒有用的配置文件,可能會(huì)導(dǎo)致項(xiàng)目體積過大。如果因此資源加載的時(shí)間過長的話,也會(huì)影響人們的體驗(yàn),畢竟人們都喜歡快和更快。

針對(duì)上述問題,我們可以采用按需引入的方式。如果有很多頁面都需要用到

Echarts的話,那我們就在main.js中引入:

  1. let echarts = require('echarts/lib/echarts'
  2.  
  3. require('echarts/lib/chart/line'
  4.  
  5. require('echarts/lib/component/tooltip'
  6. require('echarts/lib/component/title'
  7.  
  8. Vue.prototype.$echarts = echarts 

如果只是在偶爾幾個(gè)頁面引用,也可以單獨(dú)在.vue引入:

  1. <script> 
  2. let echarts = require('echarts/lib/echarts'
  3.  
  4. require('echarts/lib/chart/line'
  5.  
  6. require('echarts/lib/component/tooltip'
  7. require('echarts/lib/component/title'
  8.  
  9. </script> 

然后再改一下Echarts的配置項(xiàng):

  1. this.options = { 
  2.     title: { 
  3.       text: "測試表格" 
  4.     }, 
  5.     tooltip: { 
  6.       trigger'axis' 
  7.     }, 
  8.     xAxis: { 
  9.       type: 'category'
  10.       data: ['Mon''Tue''Wed''Thu''Fri''Sat''Sun'
  11.     }, 
  12.     yAxis: { 
  13.       type: 'value' 
  14.     }, 
  15.     series: [{ 
  16.       data: [820, 932, 901, 934, 1290, 1330, 1320], 
  17.       type: 'line' 
  18.     }] 

ref獲取DOM
我們可以發(fā)現(xiàn),上面的例子都是用 getElementById() 來獲取渲染圖表的div,同樣我們也可以用 ref 來對(duì)真實(shí)的DOM進(jìn)行操作。我們把代碼作以下修改:

  1. <template> 
  2.   <div> 
  3.     <div id="myChart" ref="myChart"></div> 
  4.   </div> 
  5. </template> 

  1. initCharts () { 
  2.   // this.chart = this.$echarts.init(document.getElementById('myChart')) 
  3.   this.chart = this.$echarts.init(this.$refs.myChart) 
  4.   this.chart.setOption(this.options) 

最終得到的效果是一樣的

React中運(yùn)用ECharts
在React中運(yùn)用ECharts的方式和Vue有很多相似之處,只是在寫法上有些許不同

全部引入
chart.jsx

  1. import React, { Component } from 'react'
  2. import echarts from 'echarts' 
  3. import './chart.less'
  4.  
  5. export class App extends Component { 
  6.     constructor(props) { 
  7.         super(props); 
  8.         this.state = { 
  9.             data:[820, 932, 901, 934, 1290, 1330, 1320] 
  10.         } 
  11.     } 
  12.  
  13.     componentDidMount(){ 
  14.         this.initCharts(); 
  15.     } 
  16.     //初始化 
  17.     initCharts = () => { 
  18.         let myChart = echarts.init(document.getElementById('myChart')); 
  19.         let option = { 
  20.             title: { 
  21.                 text: "測試表格-react" 
  22.               }, 
  23.               tooltip: { 
  24.                 trigger'axis' 
  25.               }, 
  26.               xAxis: { 
  27.                 type: 'category'
  28.                 data: ['Mon''Tue''Wed''Thu''Fri''Sat''Sun'
  29.               }, 
  30.               yAxis: { 
  31.                 type: 'value' 
  32.               }, 
  33.               series: [{ 
  34.                 data: this.state.data, 
  35.                 type: 'line' 
  36.               }] 
  37.         }; 
  38.         myChart.setOption(option); 
  39.         window.addEventListener("resize"function () { 
  40.             myChart.resize(); 
  41.         }); 
  42.     } 
  43.  
  44.     render(){ 
  45.         return ( 
  46.             <div className="chart"
  47.                 <div id="myChart"></div> 
  48.             </div> 
  49.         ) 
  50.     } 

chart.less

  1. .chart{ 
  2.     display: flex; 
  3.     flex: 1; 
  4.     #myChart{ 
  5.         width: 400px; 
  6.         height: 400px; 
  7.     } 

效果

按需引入
在React中,如果把ECharts整個(gè)引入,也會(huì)面臨項(xiàng)目包體積過大所造成的負(fù)面影響。當(dāng)然也可以在React中按需引入ECharts,方法和Vue類似

  1. import echarts = 'echarts/lib/echarts' 
  2.  
  3. import 'echarts/lib/chart/line' 
  4.  
  5. import 'echarts/lib/component/tooltip' 
  6. import 'echarts/lib/component/title' 

在React-Hooks中使用
在以前沒有Hook的時(shí)候,我們都是在class里面寫代碼,就如上述的方法一樣。但是現(xiàn)在既然Hook這個(gè)好東西出來了,哪有不用的道理?

  1. import React, { useEffect, useRef } from 'react'
  2. import echarts from 'echarts'
  3.  
  4. function MyChart () { 
  5.     const chartRef = useRef() 
  6.     let myChart = null 
  7.     const options = { 
  8.         title: { 
  9.             text: "測試表格-react-hook" 
  10.         }, 
  11.         tooltip: { 
  12.             trigger'axis' 
  13.         }, 
  14.         xAxis: { 
  15.             type: 'category'
  16.             data: ['Mon''Tue''Wed''Thu''Fri''Sat''Sun'
  17.         }, 
  18.         yAxis: { 
  19.             type: 'value' 
  20.         }, 
  21.         series: [{ 
  22.             data: [820, 932, 901, 934, 1290, 1330, 1320], 
  23.             type: 'line' 
  24.         }] 
  25.     } 
  26.  
  27.     function renderChart() { 
  28.         const chart = echarts.getInstanceByDom(chartRef.current
  29.         if (chart) { 
  30.             myChart = chart 
  31.         } else { 
  32.             myChart = echarts.init(chartRef.current
  33.         } 
  34.         myChart.setOption(options) 
  35.     } 
  36.  
  37.     useEffect(() => { 
  38.         renderChart() 
  39.         return () => { 
  40.             myChart && myChart.dispose() 
  41.         } 
  42.     }) 
  43.  
  44.     return ( 
  45.         <> 
  46.             <div style={{width: "400px", height: "400px"}} ref={chartRef} /> 
  47.         </> 
  48.     ) 
  49.  
  50. export default MyChart 

看看效果

既然我們已經(jīng)在Hook中成功引用了Echarts,那么為何不把代碼抽離出來,使之能讓我們進(jìn)行復(fù)用呢?我們可以根據(jù)實(shí)際情況把一些數(shù)據(jù)作為參數(shù)進(jìn)行傳遞:

useChart.js

  1. import React, { useEffect } from 'react'
  2. import echarts from 'echarts'
  3.  
  4. function useChart (chartRef, options) { 
  5.  
  6.     let myChart = null
  7.  
  8.     function renderChart() { 
  9.         const chart = echarts.getInstanceByDom(chartRef.current
  10.         if (chart) { 
  11.             myChart = chart 
  12.         } else { 
  13.             myChart = echarts.init(chartRef.current
  14.         } 
  15.         myChart.setOption(options) 
  16.     }; 
  17.  
  18.     useEffect(() => { 
  19.         renderChart() 
  20.     }, [options]) 
  21.  
  22.     useEffect(() => { 
  23.         return () => { 
  24.             myChart && myChart.dispose() 
  25.         } 
  26.     }, []) 
  27.  
  28.     return 
  29.  
  30. export default useChart 

接下來引用我們剛抽離好的Hook:

  1. import React, { useRef } from 'react' 
  2. import useChart from './useChart' 
  3.  
  4. function Chart () { 
  5.   const chartRef = useRef(null
  6.   const options = { 
  7.     title: { 
  8.         text: "測試表格 react-hook 抽離" 
  9.     }, 
  10.     tooltip: { 
  11.         trigger'axis' 
  12.     }, 
  13.     xAxis: { 
  14.         type: 'category'
  15.         data: ['Mon''Tue''Wed''Thu''Fri''Sat''Sun'
  16.     }, 
  17.     yAxis: { 
  18.         type: 'value' 
  19.     }, 
  20.     series: [{ 
  21.         data: [820, 932, 901, 934, 1290, 1330, 1320], 
  22.         type: 'line' 
  23.     }] 
  24.   } 
  25.   useChart (chartRef, options) 
  26.  
  27.   return ( 
  28.     <> 
  29.         <div style={{width: "400px", height: "400px"}} ref={chartRef} /> 
  30.     </> 
  31.   ) 
  32.  
  33. export default Chart 

最后
本文主要總結(jié)了ECharts作為數(shù)據(jù)可視化的高效工具在當(dāng)今熱門的幾種前端框架中的基本用法。相信對(duì)于這方面接觸較少的小伙伴來說應(yīng)該還是會(huì)有一定的幫助滴~

 

責(zé)任編輯:姜華 來源: 晨曦大前端
相關(guān)推薦

2023-06-08 09:00:00

2024-01-12 08:40:56

Python計(jì)算質(zhì)數(shù)質(zhì)數(shù)

2015-04-17 16:44:22

swiftOC

2009-10-20 15:39:20

Linux壓縮

2009-07-03 13:22:37

調(diào)用Servlet

2020-06-18 10:26:43

JavaScript開發(fā)技術(shù)

2013-08-26 09:51:57

2010-07-09 10:32:56

路由器協(xié)議

2009-05-18 17:16:50

2017-07-14 10:10:08

Vue.jsMixin

2018-06-07 14:45:11

Windows驗(yàn)證查看

2022-02-22 08:29:59

Vue前端防抖

2020-06-04 08:17:44

JavaScript延展操作運(yùn)算符開發(fā)

2023-05-24 16:41:41

React前端

2022-05-24 14:37:49

React條件渲染

2022-11-30 15:01:11

React技巧代碼

2022-06-10 08:01:17

ReduxReact

2010-08-16 16:39:48

DIV內(nèi)容居中

2018-10-08 08:00:00

前端ReactJavaScript

2022-04-11 09:37:49

商業(yè)智能CIO
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)