multipleEchart.vue 9.97 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: ''
    },
    isCloseShowNumTag: {
      type: Boolean,
      default: false
    },
    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 * 100)

        this.entmicroCount = this.totalData.map(item => item.newEntmicroCount)
        this.entmicroRatio = this.totalData.map(item => item.entmicroRatio * 100)

        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 => this.Solution(item.customerRatio, 100))

        this.entmicroCount = this.totalData.map(item => item.newEntmicroCount)
        this.entmicroRatio = this.totalData.map(item => this.Solution(item.entmicroRatio, 100))

        this.initChart()
      }
    }
  },
  methods: {
    Solution(a, b) {
      if (a === 0 || b === 0) return 0 //

      let m = 0 //精度
      let s1 = a.toString()
      let s2 = b.toString()

      if (s1.indexOf('.') == -1 && s2.indexOf('.') == -1) {
        return a * b
      } else {
        //计算s1小数点后的位数
        m = m + (s1.indexOf('.') >= 0 ? s1.split('.')[1].length : 0)
        //计算s2小数点后的位数
        m = m + (s2.indexOf('.') >= 0 ? s2.split('.')[1].length : 0)
        //除小数点后的所有位数相乘/精度
        return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m)
      }
    },

    initChart() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(this.$refs.myChart)
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          },
          formatter: function(datas) {
            var res = datas[0].name + ' | 新增' + '<br/>'
            let circleColor
            for (var i = 0, length = datas.length; i < length; i++) {
              // if (datas[i].seriesName == '新增客户' || datas[i].seriesName == '新增企微客户') {
              //   res += datas[i].seriesName + ':' + datas[i].data + '人' + '<br/>'
              // } else {
              //   res += datas[i].seriesName + ':' + datas[i].data + '%' + '<br/>'
              // }
              if (datas[i].seriesName == '全部客户') {
                circleColor = '#928DFF'
                res += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${circleColor};"></span>` + datas[i].seriesName + ':' + datas[i].data + '人' + '<br/>'
              }
              if (datas[i].seriesName == '全部客户环比') {
                circleColor = '#5850FF'
                res += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${circleColor};"></span>` + datas[i].seriesName + ':' + datas[i].data + '%' + '<br/>'
              }
              if (datas[i].seriesName == '企微客户') {
                circleColor = '#FEBE6B'
                res += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${circleColor};"></span>` + datas[i].seriesName + ':' + datas[i].data + '人' + '<br/>'
              }
              if (datas[i].seriesName == '企微客户环比') {
                circleColor = '#FE9C1C'
                res += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${circleColor};"></span>` + datas[i].seriesName + ':' + datas[i].data + '%' + '<br/>'
              }
            }
            return res
          }

          // formatter: '{b0}<br/>' + '{a0}: {c0}人<br />{a1}: {c1}%<br />{a2}: {c2}人<br />{a3}: {c3}%'
        },

        // 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",
          // },
          // tooltip: {
          //   show: true
          // },
          itemGap: 20, //图例每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。
          // data: ['全部客户', '全部客户环比', '企微客户', '企微客户环比'], //图例的数据数组。
          // data: ['新增客户', '新增客户环比', '新增企微客户', '新增企微客户环比'], //图例的数据数组。
          data: ['全部客户', '全部客户环比', '企微客户', '企微客户环比'], //图例的数据数组。
          inactiveColor: '#ccc' //图例关闭时的颜色。
        },
        grid: {
          //直角坐标系内绘图网格
          bottom: '20%', //grid 组件离容器下侧的距离。
          left: '1%',
          right: '5%',

          containLabel: true //grid 区域是否包含坐标轴的刻度标签。
        },
        dataZoom: [
          //dataZoom 组件 用于区域缩放,从而能自由关注细节的数据信息,或者概览数据整体,或者去除离群点的影响。#bed0f7
          {
            bottom: '30px',
            // type: 'inside',

            type: 'slider',
            show: true,
            brushStyle: {
              show: false
            },
            // 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: this.isCloseShowNumTag ? false : true,
              // formatter: '{b}: {c}',
              color: '#666666',
              position: 'top'
            },
            itemStyle: {
              color: '#928DFF'
            }
          },
          {
            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: '#FEBE6B'
            },
            label: {
              show: this.isCloseShowNumTag ? false : 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>