vue引入echarts与使用

75 阅读1分钟

一、安装在vue根目录运行

npm install echarts --save

二、main.js

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

三、html.vue

import * as echarts from 'echarts'

export default {
  name: "console",
  data() {
    return {
      option : {
        title: {
          text: 'Step Line'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['Step Start', 'Step Middle', 'Step End']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: 'Step Start',
            type: 'line',
            step: 'start',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: 'Step Middle',
            type: 'line',
            step: 'middle',
            data: [220, 282, 201, 234, 290, 430, 410]
          },
          {
            name: 'Step End',
            type: 'line',
            step: 'end',
            data: [450, 432, 401, 454, 590, 530, 510]
          }
        ]
      },
    };
  },
  created() {

  },
  methods:{
  },
  mounted() {  //注意一定要写在这里面

    let this_ = this;
    let chart = echarts.init(document.getElementById('echarts'));
    chart.setOption(this.option);
    //建议加上以下这一行代码,不加的效果图如下(当浏览器窗口缩小的时候)。超过了div的界限(红色边框)
    window.addEventListener('resize',function() {chart.resize()});
  },

}

欢迎各位小伙伴来我的QQ交流群一起学习 :842167453