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

开发
俗话说:“工欲善其事,必先利其器”。现如今已经有许多成熟易用的可视化解决方案,例如ECharts,AntV等等。我们可以把这些解决方案比作是一套套成熟的“工具”,那我们如何将这些“工具”应用于当前最热门的两个前端框架中呢?

 现在我们就以ECharts为例,来尝试“工具”的各种用法。

Vue中运用ECharts
首先我们要把ECharts下载下来:

  1. npm install echarts --save 

全局引用
全局引用的好处就是我们一次性把ECharts引入项目后,就可以在任何一个组件中使用ECharts了。

首先在项目的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完整的引入,这样做的缺点就是会额外的引入很多其他没有用的配置文件,可能会导致项目体积过大。如果因此资源加载的时间过长的话,也会影响人们的体验,毕竟人们都喜欢快和更快。

针对上述问题,我们可以采用按需引入的方式。如果有很多页面都需要用到

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 

如果只是在偶尔几个页面引用,也可以单独在.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的配置项:

  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
我们可以发现,上面的例子都是用 getElementById() 来获取渲染图表的div,同样我们也可以用 ref 来对真实的DOM进行操作。我们把代码作以下修改:

  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中运用ECharts
在React中运用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整个引入,也会面临项目包体积过大所造成的负面影响。当然也可以在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的时候,我们都是在class里面写代码,就如上述的方法一样。但是现在既然Hook这个好东西出来了,哪有不用的道理?

  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 

看看效果

既然我们已经在Hook中成功引用了Echarts,那么为何不把代码抽离出来,使之能让我们进行复用呢?我们可以根据实际情况把一些数据作为参数进行传递:

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 

最后
本文主要总结了ECharts作为数据可视化的高效工具在当今热门的几种前端框架中的基本用法。相信对于这方面接触较少的小伙伴来说应该还是会有一定的帮助滴~

 

责任编辑:姜华 来源: 晨曦大前端
相关推荐

2015-04-17 16:44:22

swiftOC

2013-08-26 09:51:57

2020-06-18 10:26:43

JavaScript开发技术

2009-07-03 13:22:37

调用Servlet

2009-10-20 15:39:20

Linux压缩

2022-02-22 08:29:59

Vue前端防抖

2020-06-04 08:17:44

JavaScript延展操作运算符开发

2018-06-07 14:45:11

Windows验证查看

2023-06-08 09:00:00

2022-11-30 15:01:11

React技巧代码

2022-06-10 08:01:17

ReduxReact

2010-07-09 10:32:56

路由器协议

2017-07-14 10:10:08

Vue.jsMixin

2022-04-11 09:37:49

商业智能CIO

2024-01-12 08:40:56

Python计算质数质数

2023-05-24 16:41:41

React前端

2010-08-16 16:39:48

DIV内容居中

2018-10-08 08:00:00

前端ReactJavaScript

2009-05-18 17:16:50

2021-08-26 14:55:55

开发React代码
点赞
收藏

51CTO技术栈公众号