AntvF2自定义饼图样式

<template>
  <div class="arrivalCanvas">
    <canvas id="arrivalCanvas" style="width: 100%;height: 100%" class="canvas"></canvas>
  </div>
</template>
<script type="text/ecmascript-6">
/** F2按需引入start */
const F2 = require('@antv/f2/lib/core')
const Legend = require('@antv/f2/lib/plugin/legend')
const Tooltip = require('@antv/f2/lib/plugin/tooltip')
const Guide = require('@antv/f2/lib/plugin/guide')
require('@antv/f2/lib/geom/area')
require('@antv/f2/lib/geom/interval')
require('@antv/f2/lib/component/guide')
require('@antv/f2/lib/coord/polar')
require('@antv/f2/lib/geom/adjust/stack')
F2.Chart.plugins.register(Legend)
F2.Chart.plugins.register(Tooltip)
F2.Chart.plugins.register(Guide)
import Toast from '@base/toast/index.js'
import Staff from '@api/staff/staff.js'
export default {
  name: 'arrivalCanvas',
  props: {
    adminUserId: {
      type: String
    },
    shopId: {
      type: [String,Number]
    }
  },
  data() {
    return {
      arrivalData: [{
        name: '近30天到店3次及以上',
        type: 'arrive1_2',
        value: 0,
        a: 1,
        color: '#975FE4'
      }, {
        name: '近30天到店1~2次',
        type: 'arrive3',
        value: 0,
        a: 1,
        color: '#3BA0FF'
      }, {
        name: '30天~90(含)天到店',
        type: 'overMonth1',
        value: 0,
        a: 1,
        color: '#36CBCB'
      }, {
        name: '90天~180(含)天到店',
        type: 'overMonth2',
        value: 0,
        a: 1,
        color: '#4DCB73'
      }, {
        name: '超过180天到店',
        type: 'overMonth6',
        value: 0,
        a: 1,
        color: '#FAD337'
      }],
      total: 0,
      isEmpty: false
    }
  },
  mounted() {
    this.getAllInformation()
  },
  methods: {
    getAllInformation() {
      let params = {
        adminUserId: this.adminUserId
      }
      if(this.shopId) {
        params.shopId = this.shopId
      }
      this.$axios
        .post(Staff.getArriveRecentYearOverview, params)
        .then(res => {
          this.setAllInformation(res.result)
        })
        .catch(error => {
          Toast.close()
          Toast.start({
            info: error.msg,
            type: error.type === 'service' ? 'error' : 'default',
            time: 1500
          })
        })
    },
    setAllInformation(result) {
      let total = 0
      this.isEmpty = result.every(v=>v.count === 0)
      result.forEach(v=>{
        this.arrivalData.forEach(it=>{
          if(v.arriveRangeType === it.type) {
            it.value = v.count
            total += v.count
          }
        })
      })
      this.total = total
      this.$nextTick(() => {
        this.drawRevenueChart()
      })
    },
    drawRevenueChart() {
      let legendItems = [];
      this.arrivalData.map(function(obj) {
        let item = {
          name: obj.name,
          value: obj.value + '人',
          marker: {
            symbol: 'circle',
            radius: 3,
            fill: obj.color
          }
        }
        legendItems.push(item)
      });

      var chart = new F2.Chart({
        id: 'arrivalCanvas',
        pixelRatio: window.devicePixelRatio,
        padding: [0, 'auto']
      });
      chart.source(this.arrivalData);
      chart.tooltip(false);
      chart.legend({
        position: 'right',
        custom: true,
        items: legendItems,
        offsetY: 5,
        nameStyle: {
          fill: '#999',
          fontSize: 12,
          fontWeight: 400,
          height: 16
        },
        valueStyle: {
          fill: '#333',
          fontSize: 12,
          height: 16
        },
        clickable: false
      });
      chart.coord('polar', {
        transposed: true,
        innerRadius: 0.75,
        radius: 0.85
      });
      chart.axis(false);
      chart.guide().text({
        position: ['50%', '50%'],
        content: this.total + '人',
        style: {
          fontSize: '16',
          fill: '#333'
        }
      });
      if(this.isEmpty) {
        chart.interval().position('1*a').color('name', ['#975FE4', '#3BA0FF', '#36CBCB', '#4DCB73', '#FAD337']).adjust('stack').style({
          lineWidth: 4,
          stroke: '#fff'
        })
      } else {
        chart.interval().position('1*value').color('name', ['#975FE4', '#3BA0FF', '#36CBCB', '#4DCB73', '#FAD337']).adjust('stack').style({
          lineWidth: 4,
          stroke: '#fff'
        })
      }
      chart.render();
    }
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.arrivalCanvas
  display flex
  align-items center
</style>

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King_960725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值