PieChart.vue 1.99 KB
<template>
  <div>
    <p class="line-color-title">{{title}}</p>
    <div ref="pieChart" style="margin-top:10px;width: 100%; height: 20vh;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import resize from './../mixin/resize'
export default {
  name: 'PieChart',
  mixins:[resize],
  props: {
    title:{
      type: String,
      default: '',
    },
    data: {
      type: Array,
      default: () => [],
    },
    colors: {
      type: Array,
      default: () => ['#50CEFB', '#5850FF','#FBBE50','#90D55C','#FDDB93','#A1EDFF','#6EA3FF','#FF5A92','#BCCAFF','#9F6EFF'],
    },
  },
  mounted() {
    this.initChart(this.$refs.pieChart);
    this.drawChart();
  },
  methods: {
    drawChart() {
      const chart = echarts.init(this.$refs.pieChart);

      const option = {
        // tooltip: {
        //   trigger: 'item',
        // },
        legend: {
          show: false,
        },
        series: [
          {
            type: 'pie',
            radius: ['40%', '60%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'outside',
              formatter: function(params) {
                return '{a|' + params.value  + '%'+ '}'
                +'\n'+'\n'+
                ' {b|' + params.name + '}'
              },
              rich: {
                a: {
                  color: '#50CEFB',
                  fontWeight: 'bold',
                  fontSize:'18px'
                },
                b: {
                  color: '#333',
                  fontSize:'14px',
                }
              }
            },
            labelLine: {
              show: true
            },
            data: this.data,
            itemStyle: {
              color: (params) => {
                return this.colors[params.dataIndex];
              },
            },
          },
        ],
      };

      chart.setOption(option);
    },
  },
};
</script>

<style lang="scss">
/* 您可以根据需要添加自定义样式 */
</style>