欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Vue中使用Echarts的两种方式

程序员文章站 2022-06-30 11:55:50
1. 直接引入echarts 先npm安装echarts npm install echarts --save 开发: main.js imp...

1. 直接引入echarts

先npm安装echarts

npm install echarts --save

开发:

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
...
<div id="chart1"></div>
...
mounted() {
  this.$chart.line1('chart1');
},

2、使用vue-echarts

先npm安装vue-echarts

npm install vue-echarts

开发:

main.js

import echarts from 'vue-echarts/components/echarts'
import 'echarts/lib/chart/bar'
import 'echarts/lib/component/tooltip'
vue.component('chart', echarts)
hello.vue
...
<chart ref="chart1" :options="orgoptions" :auto-resize="true"></chart>
...
data: function() {
  return {
    orgoptions: {},
  }
},
...
mounted() {
  this.orgoptions = {
    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
    }]
  }
}

总结

以上所述是小编给大家介绍的vue中使用echarts的两种方式,希望对大家有所帮助