pie.vue 2.84 KB
<template>
  <div class="pie-container flex">
    <canvas class="canvas" :id="refId"></canvas>
    <div class="list-info">
      <template v-for="(legend, index) in chartData">
        <div class="list-info-item" :key="index">
          <div class="font-12 color-2f line-18 flex">
            <div class="circle" :style="{ background: color[index] }"></div>
            {{ legend.text }}
          </div>
          <div class="font-16 color-3 line-24 bold m-l-14">{{ legend.value }}</div>
        </div>
      </template>

      <!-- <div class="list-info-item">
        <div class="font-12 color-2f line-18 flex">
          <div class="circle"></div>
          已跟进 (人)
        </div>
        <div class="font-16 color-3 line-24 bold m-l-14">396</div>
      </div> -->
    </div>
  </div>
</template>
<script>
import F2 from '@antv/f2/lib/index-all'

export default {
  name: 'pieChart',
  data() {
    return {
      chart: null,
      chartColor: this.color,
      refId: this.id,
      chartData: this.list
    }
  },
  props: {
    color: {
      type: Array,
      default: () => []
    },
    list: {
      type: Array,
      default: () => []
    },
    specialData: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: ''
    },
    centerData: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    list() {
      this.initCanvas()
    }
  },
  mounted() {
    this.initCanvas()
  },
  methods: {
    initCanvas() {
      let id = this.refId
      let { chartData, chartColor, specialData,centerData } = this
      let data = specialData.length>0?specialData:chartData
      this.chart && this.chart.clear()
      this.chart = null
      this.chart = new F2.Chart({
        id,
        pixelRatio: window.devicePixelRatio,
        padding: [20, 20, 20, 20]
      })

      this.chart.source(data)
      this.chart.coord('polar', {
        transposed: true,
        radius: 0.9,
        innerRadius: 0.74
      })
      this.chart.axis(false)
      this.chart.legend(false)
      this.chart.tooltip(false)
      this.chart.guide().html({
        position: ['50%', '53%'],
        html: `<div style="width: 250px;height: 40px;text-align: center;">
      <div style="font-size: 12px;color: rgba(47, 64, 86, 0.7);">${centerData.text}</div>
      <div style="font-size: 18px;color: #343434;margin: 0;font-weight: bold;line-height: 24px;">${centerData.rate}</div>
    </div>`
      })
      this.chart.interval().position('const*value').adjust('stack').color('text', chartColor)
      this.chart.render()
    }
  }
}
</script>
<style lang="scss" scoped>
.canvas {
  width: calc(100% - 135px);
  height: 160px;
}
.list-info-item {
  margin-top: 16px;
  .circle {
    width: 8px;
    height: 8px;
    background: #3975c6;
    border-radius: 2px;
    margin-right: 6px;
  }
}
.list-info-item:nth-child(1) {
  margin-top: 0;
}
</style>