pieChart.vue 1.83 KB
<template>
  <div class="chart-box">
    <div id="pie" class="chart-pie"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
const DISCOUNT_TYPE = {
  1: '满减券',
  2: '折扣券',
  3: '代金券',
  4: '兑换券',
  5: '购车代金券',
  6: '商品折扣券',
  7: '会员服务'
}
export default {
  name: 'PieChart',
  props: ['list'],
  data() {
    return {
      DISCOUNT_TYPE,
      option: {
        tooltip: {
          trigger: 'item',
          formatter: '{b}\n \n{c} 张'
        },
        series: [
          {
            name: '券类营销',
            type: 'pie',
            radius: '60%',
            label: {
              formatter: '{b}\n \n{c} 张'
            },
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = ['#5850FF', '#FE9C1C', '#FDB757', '#6495FA']
                  return colorList[params.dataIndex]
                }
              }
            },
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      },
      myChart: {}
    }
  },
  watch: {
    list(val) {
      const arr = this.list.map(item => {
        return {
          name: this.DISCOUNT_TYPE[item.discountType],
          value: item.totalIssue
        }
      })
      this.option.series[0].data = arr
      this.option && this.myChart.setOption(this.option)
    }
  },
  methods: {},
  mounted() {
    let chartDom = document.getElementById('pie')
    this.myChart = echarts.init(chartDom)
    this.option && this.myChart.setOption(this.option)
  }
}
</script>

<style lang="scss" scoped>
.chart-box {
  width: 100%;
  height: 185px;
}
.chart-pie {
  height: 185px;
}
</style>