guohua.wang
2024-05-06 a72cd635633dd72ee835f69788c0d71b6e3ae3c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 
<template>
  <div class="piechart-wrapper">
    <div class="charts1" :id="id" :style="'wdith:100%;height:'+ customerHeight +'px;'"></div>
  </div>
</template>
 
<script>
import * as echarts from 'echarts/core';
import { PieChart } from 'echarts/charts';
import { TitleComponent,TooltipComponent,GridComponent } from 'echarts/components';//LegendComponent
import { CanvasRenderer } from 'echarts/renderers';
echarts.use( [TitleComponent, TooltipComponent, GridComponent, PieChart, CanvasRenderer] );
import elementResizeDetectorMaker from 'element-resize-detector'
 
export default {
  name:'PieChartLabel',
  props:{
    id:{
      type:String,
      default(){
        return 'chart' + ( +new Date() );
      }
    },
    customerName:{
      type:String,
      default: '访问来源'
    },
    customerHeight:{
      type:Number,
      default: 130
    },
    dataList:{
      type:Array,
      default(){
        return []
      }
    },
    colorList:{
      type:Array,
      default(){
        return ['#F16B4B', '#D8361B']
      }
    }
  },
  data() {
    return {
      myChart:null,
    }
  },
  watch:{
    dataList:{
      handler:function(newValue){
        this.dataList = newValue;
        this.DrawPieChart(newValue);
      },
      deep:true,
      immediate:false
    }
  },
  create() {},
  mounted() {
    this.DrawPieChart(this.dataList);
 
    let _this = this;
    let erd = elementResizeDetectorMaker();
    erd.listenTo(document.getElementById(this.id), (element) => {
      _this.myChart.resize();
    })
  },
  methods: {
    DrawPieChart(arr){
      this.myChart = echarts.init(document.getElementById(this.id));
      this.myChart.setOption({
        series: [
          {
            name: this.customerName,
            type: 'pie',
            radius: '70%',
            data: arr,
            silent:true,
            color: this.colorList,
            label: {
              color: '#F2270C',
              formatter:function(params){
                console.log(params);
                return params.name+"("+params.value+')'
              },
              fontSize: 10,
            },
            labelLine: {
              length: 12,
            }
          }
        ]
      },true)
    },
  },
};
</script>
 
<style scoped>
</style>