multipleEchart.vue 6.74 KB
<template>
  <div>
    <div class="echart" ref="myChart" :style="{ width: width, height: height }"></div>
  </div>
</template>

<script>
export default {
  props: {
    width: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: ''
    },
    totalData: {
      type: Array,
      // default: () => []
      default() {
        return []
      }
    }
  },
  data() {
    return {
      dateArray: [],
      customerCount: [], //全部客户
      customerRatio: [], //全部环比
      entmicroCount: [], //企微客户
      entmicroRatio: [] //企微客户环比
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.totalData && this.totalData.length > 0) {
        this.dateArray = this.totalData.map(item => item.dateTime) //日期
        this.customerCount = this.totalData.map(item => item.newCustomerCount)
        this.customerRatio = this.totalData.map(item => item.customerRatio)
        this.entmicroCount = this.totalData.map(item => item.newEntmicroCount)
        this.entmicroRatio = this.totalData.map(item => item.entmicroRatio)

        this.initChart()
      }
    })
  },
  watch: {
    totalData: {
      deep: true,
      handler(val) {
        this.dateArray = this.totalData.map(item => item.dateTime) //日期
        this.customerCount = this.totalData.map(item => item.newCustomerCount)
        this.customerRatio = this.totalData.map(item => item.customerRatio)
        this.entmicroCount = this.totalData.map(item => item.newEntmicroCount)
        this.entmicroRatio = this.totalData.map(item => item.entmicroRatio)
        this.initChart()
      }
    }
  },
  methods: {
    initChart() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(this.$refs.myChart)
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        // toolbox: {
        //   feature: {
        //     dataView: { show: true, readOnly: false },
        //     magicType: { show: true, type: ["line", "bar"] },
        //     restore: { show: true },
        //     saveAsImage: { show: true },
        //   },
        // },
        legend: {
          x: 'center',
          // y: 'bottom',
          bottom: '50px',
          // textStyle: {
          //   //图例的公用文本样式。
          //   fontSize: 14,
          //   color: "#333",
          // },
          itemGap: 20, //图例每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。
          data: ['全部客户', '全部客户环比', '企微客户', '企微客户环比'], //图例的数据数组。
          inactiveColor: '#ccc' //图例关闭时的颜色。
        },
        grid: {
          //直角坐标系内绘图网格
          bottom: '20%', //grid 组件离容器下侧的距离。
          left: '1%',
          right: '5%',
          containLabel: true //grid 区域是否包含坐标轴的刻度标签。
        },
        dataZoom: [
          //dataZoom 组件 用于区域缩放,从而能自由关注细节的数据信息,或者概览数据整体,或者去除离群点的影响。#bed0f7
          {
            bottom: '30px',
            type: 'inside',
            show: true,
            // backgroundColor: '#ff0000',
            // borderColor: '#ff0000',
            height: '1%',
            // borderColor: '#8FDFFE',

            // backgroundColor: '#8fdffe99',
            handleIcon: 'M512 512m-208 0a6.5 6.5 0 1 0 416 0 6.5 6.5 0 1 0-416 0Z M512 192C335.264 192 192 335.264 192 512c0 176.736 143.264 320 320 320s320-143.264 320-320C832 335.264 688.736 192 512 192zM512 800c-159.072 0-288-128.928-288-288 0-159.072 128.928-288 288-288s288 128.928 288 288C800 671.072 671.072 800 512 800z',

            handleColor: '#bed0f7',

            handleSize: 15,

            handleStyle: {
              borderColor: '#bed0f7',

              shadowBlur: 4,

              shadowOffsetX: 1,

              shadowOffsetY: 1,

              shadowColor: '#e5e5e5'
            },

            start: 0,
            end: 100
          },
          {
            type: 'inside',
            start: 0,
            end: 100
          }
        ],
        xAxis: [
          {
            type: 'category',
            data: this.dateArray, //['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '单位:人',
            min: 0,
            max: 100,
            interval: 10,
            axisLabel: {
              formatter: '{value}'
            },
            splitLine: {
              show: false //是否显示分割线
            }
          },
          {
            type: 'value',
            name: '环比(%)',
            // min: "-20",
            // max: "20",
            // interval: 10,
            axisLabel: {
              formatter: '{value}'
            },
            splitLine: {
              show: false //是否显示分割线
            }
          }
        ],
        series: [
          {
            name: '全部客户',
            type: 'bar',
            data: this.customerCount, //[33600, 18000, 17500, 20000, 17000, 480, 1000, 800, 520, 650, 920, 600],
            label: {
              show: true,
              // formatter: '{b}: {c}',
              color: '#666666',
              position: 'top'
            },
            itemStyle: {
              color: '#5850ff'
            }
          },
          {
            name: '全部客户环比',
            type: 'line',
            yAxisIndex: 1,
            data: this.customerRatio, //[2.0, 2.2, -3.3, 4.5, 6.3, -10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
            itemStyle: {
              color: '#5850ff'
            }
          },
          {
            name: '企微客户',
            type: 'bar',
            data: this.entmicroCount, //[22000, 10000, 10000, 13000, 11500, 4000, 7000, 6500, 4500, 5500, 7000, 6000],
            itemStyle: {
              color: '#FE9C1C'
            },
            label: {
              show: true,
              color: '#666666',
              position: 'top'
            }
          },
          {
            name: '企微客户环比',
            type: 'line',
            yAxisIndex: 1,
            data: this.entmicroRatio, //[2.6, 2.2, 3.0, 3.8, 6.8, 10.9, 18.3, 23.0, 23.9, 16.0, 10.0, 8.2],
            itemStyle: {
              color: '#FE9C1C'
            }
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>