echarts 折线图vue封装

效果

在这里插入图片描述

echarts 折线图vue封装

<template>
  <!--为echarts准备一个具备大小的容器dom-->
  <div class="line" :id="Line"  :style="{height:'100%',width:'100%'}" />
</template>
<script>
import echarts from 'echarts'
export default {
  name: 'Line',
  props: ['id', 'getData'],
  data () {
    return {
      Line: this.id || 'Line',
      charts: ''
    }
  },
  // 实时监听父组件传过来的值,进而执行drawBar方法,重绘柱状图
  watch: {
    getData: {
      handler (value) {
        this.drawLine(value)
      },
      deep: true
    }
  },
  mounted () {
    this.drawLine()
  },
  methods: {
    drawLine ({
      legendData = [],
      xAxisData = [],
      series = [],
      colorArray = []
    } = {}) {
      this.charts = echarts.init(document.getElementById(this.Line))
      this.charts.setOption({
        title: {
          text: ''
        },
        color: colorArray,
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          x: 'center',
          y: 'bottom',
          padding: [300, 0, 0, 0],
          data: legendData
        },
        grid: {
          left: '0',
          right: '4%',
          bottom: '10%',
          top: '5%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: true,
          data: xAxisData
        },
        yAxis: {
          type: 'value'
        },
        series: series
      })
    }
  }
}
</script>

使用

<template>
      <div>
       <--宽度,高度-->
        <line:get-data="objectData" id="barId" style="height: 250px"></line>
      </div>
</template>
<script>
import Line from '@/components/line'

export default {
  components: {
    Line 
  },
   data ()  {
	   return {
	       objectData: {
			        legendData: ['转发次数', '活跃用户'],
                    xAxisData: ['20200423', '20200424', '20200425', '20200426'],
		            series: [
		              {
		                name: '转发次数',
		                type: 'line',
		                stack: '总量',
		                smooth: true,
		                seriesLayoutBy: 'row',
		                data: [3,4,5,6],
		                areaStyle: { normal: {
		                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		                    offset: 0,
		                    color: '#6AF5BD'
		                  }, {
		                    offset: 1,
		                    color: '#FFFFFF'
		                  }])
		                } }
		              },
		              {
		                name: '活跃用户',
		                type: 'line',
		                stack: '总量',
		                smooth: true,
		                seriesLayoutBy: 'row',
		                data: [1,2,3,4],
		                areaStyle: { normal: {
		                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		                    offset: 0,
		                    color: '#F7948C'
		                  }, {
		                    offset: 1,
		                    color: '#FFFFFF'
		                  }])
		                } }
		              }
		            ],
		            colorArray: ['#6AF5BD', '#F7948C']
     		 }
	  	 }
     }
</script>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜丶陌颜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值