pictorialBar.vue 5.49 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: ''
    },
    entmicroAddCustomerCount: {
      type: Number,
      default: 0
    },
    entmicroNotAddCustomerCount: {
      type: Number,
      default: 0
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(this.$refs.myChart)
      // 绘制图表
      var option
      var spirit =
        'path://M 254.911 16.6565 C 253.775 16.1755 253.241 15.6256 249.786 15.0415 C 249.419 14.9556 249.052 14.8525 248.684 14.7322 C 248.017 14.3027 247.817 13.392 248.234 12.7048 C 248.301 12.5845 248.401 12.4814 248.501 12.3783 C 248.985 11.8629 249.369 11.2615 249.636 10.5915 C 249.786 10.2135 249.92 9.8011 250.053 9.37157 L 250.087 9.44029 C 250.287 9.54338 250.437 9.42311 250.571 9.21693 C 250.971 8.44376 251.238 7.58468 251.372 6.72561 C 251.439 6.4507 251.272 6.1758 251.005 6.10707 C 250.938 6.08989 250.854 6.08989 250.788 6.10707 L 250.688 6.15862 C 250.704 5.93526 250.721 5.7119 250.721 5.48854 C 250.871 2.61922 248.718 0.162258 245.93 0.00762427 C 243.143 -0.14701 240.756 2.06941 240.606 4.93873 C 240.606 5.07618 240.589 5.23081 240.606 5.36827 C 240.606 5.62599 240.622 5.90089 240.639 6.15862 L 240.522 6.10707 C 240.255 6.05553 239.988 6.24452 239.938 6.51943 C 239.921 6.58816 239.921 6.67406 239.938 6.74279 C 240.072 7.61905 240.339 8.46094 240.739 9.23411 C 240.873 9.44029 241.04 9.56056 241.223 9.45747 L 241.29 9.3372 C 241.407 9.74956 241.54 10.1447 241.674 10.5055 C 241.908 11.1756 242.275 11.7942 242.742 12.3096 C 243.377 12.8251 243.477 13.7529 242.976 14.4058 C 242.876 14.5432 242.759 14.6463 242.625 14.7322 C 241.974 15.0243 237.217 15.9693 236.466 16.5363 C 235.331 17.8764 234.496 19.4915 234.029 21.2268 C 233.929 21.5361 234.096 21.8625 234.396 21.9656 C 234.463 21.9828 234.513 22 234.58 22 L 257.481 22 C 257.748 21.8282 258.082 21.7595 257.982 21.3815 C 257.615 20.0928 256.346 18.0654 254.911 16.6565 Z'

      var maxData = this.entmicroAddCustomerCount + this.entmicroNotAddCustomerCount
      option = {
        tooltip: {
          formatter: ' {c}人'
        },
        xAxis: {
          max: maxData, // 以x轴为基准,进行象形图的重复画图,最大值为100(可调整),为了下方好处理我调整为100
          splitLine: { show: false }, // x轴 轴线什么的都不需要
          axisLabel: { show: false },
          axisTick: { show: false },
          axisLine: { show: false },
          offset: 10
        },
        yAxis: {
          data: [], // 必须给data一个[],删掉会出错,具体原因是因为x,y轴都必须存在
          // 但我们在x轴重复图片,而不是y轴;但是y轴也不能展示出轴线、数据什么的
          show: false
        },
        grid: {
          top: 'bottom',
          height: 100,
          left: 55,
          right: 155
        },
        series: [
          {
            type: 'pictorialBar', // 代表女生的图
            itemStyle: {
              normal: {
                color: '#716AFF ' // 颜色设置为蓝色,可调整
              }
            },
            symbol: spirit, // 象形图的svg,上面定义了
            symbolMargin: '25%', // 边距
            symbolSize: ['25%', '25%'], // [宽,高], 依据symbolBoundingData的值来计算的,重复次数是根据symbolMargin,symbolSize来的,例子中我已经调成10个人
            symbolBoundingData: maxData, // 上面我定义成了maxData = 100,如果要保持男女一致,男方那边的symbolBoundingData与女方保持一致
            data: [
              {
                value: this.entmicroAddCustomerCount, // x轴最大为10000,value为7000,那么女生占70%(即7个粉红人)  如果改成75等等也会进行处理,也就是75%,但下方的男生就是25
                symbolClip: true, // 这个的意思是60只是我设置的maxData中的一部分,不是全部
                symbolRepeat: true // 图像重复
              }
            ],
            z: 10 // 层级更高(女生的60,会显示在上层,肉眼就是粉色60%,你可以注释层级试试)
          },
          {
            type: 'pictorialBar', // 这是控制蓝色的男生的,由于你本身的svg是蓝色,这边没有改成其他颜色,如果需要展示别的颜色,你跟上边女生那样处理
            itemStyle: {
              normal: {
                color: '#FE9C1C' // 颜色设置为黄色,可调整
              }
            },
            animationDuration: 0, // 展示男生图的动画延迟时间,建议你单独注释掉看看效果
            symbolMargin: '25%',
            symbol: spirit,
            symbolSize: ['25%', '25%'], // [宽,高], 依据symbolBoundingData的值来计算的,为了显示的跟女生那边的大小一样,需要设置成一致。包括边距
            symbolBoundingData: maxData, // 同上需要设置成一样的symbolBoundingData
            data: [
              {
                value: this.entmicroNotAddCustomerCount, // value为3000,男生占30%
                symbolRepeat: 'fixed'
              }
            ],
            z: 5 // 层级更低
          }
        ]
      }
      option && myChart.setOption(option)
    }
  }
}
</script>

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