<template>
  <v-chart :padding="padding" :forceFit="true" :height="height" :data="data" :scale="scale" :onClick="handleClick">
    <v-tooltip :showTitle="false" dataKey="item*percent" />
    <v-axis />
    <!-- <v-legend v-if="!row" dataKey="item" /> -->
    <v-pie position="percent" color="item" :v-style="pieStyle" :label="labelConfig" />
    <v-coord type="theta" :radius="1" :innerRadius="row ? 0.4 : 0" />
  </v-chart>
</template>

<script>
const DataSet = require('@antv/data-set')
import { ChartEventMixins } from '@/components/chart/mixins/ChartMixins'

export default {
  name: 'ChartPie',
  mixins: [ChartEventMixins],
  props: {
    row: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    height: {
      type: Number,
      default: 254
    },
    dataSource: {
      type: Array,
      default: () => [
        // { item: '租赁', count: 30 },
        // { item: '空置', count: 60 },
        // { item: '购买', count: 10 }
      ]
    }
  },
  data() {
    return {
      scale: [
        {
          dataKey: 'percent',
          min: 0,
          formatter: '.0%'
        }
      ],
      pieStyle: {
        stroke: '#fff',
        lineWidth: 1
      },
      labelConfig: [
        'percent',
        {
          formatter: (val, item) => {
            return item.point.item + ': ' + val
          },
          offset: 10
        }
      ],
      padding: ['25', '100', '45', '100']
    }
  },
  computed: {
    data() {
      let dv = new DataSet.View().source(this.dataSource)
      // 计算数据百分比
      dv.transform({
        type: 'percent',
        field: 'count',
        dimension: 'item',
        as: 'percent'
      })
      return dv.rows
    }
  }
}
</script>
