美文网首页
ECharts vue 折线图组件

ECharts vue 折线图组件

作者: 独西楼Q | 来源:发表于2020-10-15 17:41 被阅读0次

    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>
    
    

    相关文章

      网友评论

          本文标题:ECharts vue 折线图组件

          本文链接:https://www.haomeiwen.com/subject/kaeopktx.html