您好,欢迎访问成都祈钰瑶科技有限公司!

全国咨询热线

18982081108
✕

成都网站建设公司,成都网站UI设计,成都网站安全维护,成都网站内容优化服务

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

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

创新互联建站专业为企业提供锦州网站建设、锦州做网站、锦州网站设计、锦州网站制作等企业网站建设、网页设计与制作、锦州企业网站模板建站服务,十载锦州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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

 
 
 
 
  1. npm install echarts --save

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

首先在项目的main.js中引入ECharts,然后将其绑定在vue的原型上面:

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

接下来我们就可以在自己想用ECharts的组件中引用了:

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

看看效果:

按需引用
全局引用是把Echarts完整的引入,这样做的缺点就是会额外的引入很多其他没有用的配置文件,可能会导致项目体积过大。如果因此资源加载的时间过长的话,也会影响人们的体验,毕竟人们都喜欢快和更快。

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

Echarts的话,那我们就在main.js中引入:

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

如果只是在偶尔几个页面引用,也可以单独在.vue引入:

 
 
 
 
  1. <script>
  2. let echarts = require('echarts/lib/echarts')
  3. require('echarts/lib/chart/line')
  4. require('echarts/lib/component/tooltip')
  5. require('echarts/lib/component/title')
  6. </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)
  5. }

最终得到的效果是一样的

React中运用ECharts
在React中运用ECharts的方式和Vue有很多相似之处,只是在写法上有些许不同

全部引入
chart.jsx

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

chart.less

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

效果

按需引入
在React中,如果把ECharts整个引入,也会面临项目包体积过大所造成的负面影响。当然也可以在React中按需引入ECharts,方法和Vue类似

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

在React-Hooks中使用
在以前没有Hook的时候,我们都是在class里面写代码,就如上述的方法一样。但是现在既然Hook这个好东西出来了,哪有不用的道理?

 
 
 
 
  1. import React, { useEffect, useRef } from 'react';
  2. import echarts from 'echarts';
  3. function MyChart () {
  4.     const chartRef = useRef()
  5.     let myChart = 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.     function renderChart() {
  26.         const chart = echarts.getInstanceByDom(chartRef.current)
  27.         if (chart) {
  28.             myChart = chart
  29.         } else {
  30.             myChart = echarts.init(chartRef.current)
  31.         }
  32.         myChart.setOption(options)
  33.     }
  34.     useEffect(() => {
  35.         renderChart()
  36.         return () => {
  37.             myChart && myChart.dispose()
  38.         }
  39.     })
  40.     return (
  41.         <>
  42.             <div style={{width: "400px", height: "400px"}} ref={chartRef} />
  43.         </>
  44.     )
  45. }
  46. export default MyChart

看看效果

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

useChart.js

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

接下来引用我们刚抽离好的Hook:

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

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