ECharts vue 折线图组件

2020-10-15  本文已影响0人  独西楼Q

1.安装ECharts插件
官网实例

npm install echarts --save

2.vue组件

<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
// 引入折线图组件
require('echarts/lib/chart/line');
// 引入提示框和title组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    data: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      chart: null
    }
  },
  created () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  watch: {
    data: {
      deep: true,
      handler () {
        this.initChart();
      }
    }
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: '折线图',
          left: 'center',
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['高压', '低压',],
          right: 10
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          // feature: {
          //   saveAsImage: {}
          // }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '高压',
            type: 'line',
            stack: '总量',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '低压',
            type: 'line',
            stack: '总量',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
        ]
      })
    }
  }
}
</script>

上一篇下一篇

猜你喜欢

热点阅读