Home >Web Front-end >JS Tutorial >Introduction to two ways to use Echarts in Vue

Introduction to two ways to use Echarts in Vue

不言
不言Original
2018-07-03 14:35:571742browse

This article mainly introduces two ways to use Echarts in Vue. This article introduces you to you in very detail and has certain reference value. Friends in need can refer to

1. Directly introduce echarts

First npm install echarts

npm install echarts --save

Development:

main.js

import myCharts from './comm/js/myCharts.js'
Vue.use(myCharts)
myCharts.js
/**
 * 各种画echarts图表的方法都封装在这里
 */
import echarts from 'echarts'
(function() {
  var chart = {};
  chart.install = function(vue) {
    vue.prototype.$chart = {
      //画一条简单的线
      line1: function(id) {
        this.chart = echarts.init(document.getElementById(id));
        this.chart.clear();
        const optionData = {
          xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            smooth: true
          }]
        };
        this.chart.setOption(optionData);
      },
    }
  }
  if(typeof exports == 'object') {
    module.exports = chart
  }
})()
hello.vue
...
<p id="chart1"></p>
...
mounted() {
  this.$chart.line1(&#39;chart1&#39;);
},

2. Use vue-echarts

First npm install vue-echarts

npm install vue-echarts

Development:

main.js

import ECharts from &#39;vue-echarts/components/ECharts&#39;
import &#39;echarts/lib/chart/bar&#39;
import &#39;echarts/lib/component/tooltip&#39;
Vue.component(&#39;chart&#39;, ECharts)
hello.vue
...
<chart ref="chart1" :options="orgOptions" :auto-resize="true"></chart>
...
data: function() {
  return {
    orgOptions: {},
  }
},
...
mounted() {
  this.orgOptions = {
    xAxis: {
      type: &#39;category&#39;,
      data: [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;, &#39;Sat&#39;, &#39;Sun&#39;]
    },
    yAxis: {
      type: &#39;value&#39;
    },
    series: [{
      data: [820, 932, 901, 934, 1290, 1330, 1320],
      type: &#39;line&#39;,
      smooth: true
    }]
  }
}

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to several import methods commonly used in VUE (modules, files)

Vue How to set login permissions for routing

#

The above is the detailed content of Introduction to two ways to use Echarts in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn