echarts组件——饼图

echarts组件——饼图

饼图,环形图
在这里插入图片描述
组件代码

<template>
  <div :class="classname" :style="{height:height,width:width}" />
</template>

<script>
//   环形图
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    classname: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    color: { // 花瓣颜色
      type: Array,
      default: () => ['#5788F7', '#50CD89', '#FFC700', '#895CE8', '#0FC6C2', '#B5BDCB', '#fc8452', '#9a60b4', '#ea7ccc']
    },
    labelshow: { // 是否显示标签
      type: Boolean,
      default: true
    },
    emphasisshow: {
      type: Boolean,
      default: false
    },
    piedata: {
      type: Array,
      default: () => [
        { value: 320, name: 'Industries' },
        { value: 240, name: 'Technology' },
        { value: 149, name: 'Forex' },
        { value: 100, name: 'Gold' }
      ]
    }
  },
  data() {
    return {
      chart: null,
      legenddata: []
    }
  },
  watch: {
    piedata: {
      deep: true,
      handler(val) {
        this.$nextTick(() => {
          this.piedata.forEach((item) => {
            this.legenddata.push(item.name)
          })
          this.initChart()
        })
      }
    }
  },
  mounted() {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        color: this.color,
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          data: this.legenddata,
          // orient: 'vertical',
          width: '66%',
          top: '250',
          left: 'center',
          icon: 'rect',
          itemWidth: 12,
          itemHeight: 12,
          itemGrap: 10,
          formatter: ['{a|{name}}'].join('\n'),
          textStyle: {
            rich: {
              a: {
                width: 80,
                lineHeight: 20,
                fontSize: 12,
                fontWeight: 400
              }
            }
          }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: [40, 85],
            center: ['50%', '30%'],
            label: {
              show: this.labelshow,
              position: 'inner',
              color: '#ffffff',
              formatter: (params) => {
                if (params.percent) {
                  return `${params.percent}%`
                } else {
                  return ''
                }
              }
            },
            emphasis: {
              label: {
                show: this.emphasisshow
              }
            },
            data: this.piedata,
            animationEasing: 'cubicInOut',
            animationDuration: 2000
          }
        ]
      })
    }
  }
}
</script>

使用组件,传值格式可以看组件的默认数据的格式
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值