Pie.vue 2.76 KB
<template>
  <div class='pie'>
    <div
      class="pie-canvas"
      ref='data'
    ></div>
    <div class='pie-title'>
      <slot></slot>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'Pie',
  props: {
    list: {
      type: Array,
      default: []
    }
  },
  data () {
    return {
      roseColors: ['#773ae6', '#372ef0', '#5242f3', '#0060ff', '#0096ff', '#00e5dd', '#36a8ff', '#0078f4'],
      canvas: null
    }
  },
  watch: {
    'list' (n, o) {
      if (n.length > 0) {
        this.createdCanvas()
      }
    }
  },
  methods: {
    createdCanvas () {
      if (this.canvas) this.canvas.clear()
      let roseColors = []
      let data = JSON.parse(JSON.stringify(this.list))
      let sum = data.reduce((pre, next) => pre + Number(next.value), 0)
      data.forEach((e, i) => {
        roseColors.push(this.roseColors[8 - data.length + i])
      })
      this.canvas = echarts.init(this.$refs.data)
      let options = {
        series: [{
          type: 'pie',
          radius: '65%',
          center: ['50%', 150],
          color: roseColors,
          data: data,
          label: {
            normal: {
              show: false,
              formatter: ['{b|{b}}', '{d|{d}%}'].join('\n'),
              rich: {
                d: {
                  color: '#fff',
                  fontSize: 16,
                },
                b: {
                  color: '#fff',
                  fontSize: 16,
                  lineHeight: 20
                },
              },
            }
          },
          labelLine: {
            normal: {
              lineStyle: {
                color: '#fff',
              },
              smooth: 0.1,
              length: 1,
              length2: 14,
            }
          },
          itemStyle: {
            normal: {
              shadowColor: 'rgba(0,0,0,0.5)',
              shadowBlur: 50,
            }
          }
        }]
      }
      let option = options.series[0]
      this.canvas.on('mouseover', params => {
        option.data[params.dataIndex].labelLine = option.data[params.dataIndex].label = {
          normal: {
            show: true
          }
        }
        this.canvas.setOption(options)
      })
      this.canvas.on('mouseout', params => {
        option.data[params.dataIndex].label = option.data[params.dataIndex].labelLine = {
          normal: {
            show: false
          }
        }
      })
      this.canvas.setOption(options)
    }
  }
}
</script>

<style scoped lang='scss'>
.pie {
  width: 100%;
  height: 100%;
  position: relative;
  &-canvas {
    width: 100%;
    height: 100%;
  }
  &-title {
    position: absolute;
    left: 0;
    bottom: 18px;
    width: 100%;
    text-align: center;
    font-size: 24px;
    color: #8e99a6;
  }
}
</style>