<template>
  <div  :style="currentStyle" ref="mychart">
  </div>
</template>
<script>
import Base from '@/components/mychart/basechart.vue';
// 引入饼状图组件
require('echarts/lib/chart/pie');
export default {
    name: 'pie',
    extends: Base,
    data () {
        return {
            mychartconfig: {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    data: []
                },
                series: [ {
                    name: '',
                    type: 'pie',
                    radius: '66%',
                    center: ['50%', '50%'],
                    data: [],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }]
            },
            myConfig:{
                cfieldname:'',  // 分类字段名
                cdata:'',      // 分类数据
                yfieldname:'',   //值字段名
                mychartconfig:{}
            }
        };
    },
    methods: {
        customInitMychartConfig () {
            let curlConfig = Object.assign({},this.mychartconfig,this.myConfig.mychartconfig); 
            let tempData = {};
            let legendData = [];
            let self = this;
            let data = [];
            if (this.currentData && this.currentData.length > 0) {
                this.currentData.forEach(el => {
                    let name = el[self.myConfig.cfieldname];
                    if (!tempData[name])  {
                        legendData.push(name);
                    }
                });
                 // 处理分类
                if (this.myConfig.cdata && this.myConfig.cdata.length > 0) {
                    legendData = this.$Method.copy(this.myConfig.cdata);
                } 
                // 处理值
                legendData.forEach(c => {
                    let temp = {
                        name:c
                    };
                    this.currentData.some(el => {
                        if (el[self.myConfig.cfieldname] === c) {
                            if (el[self.myConfig.yfieldname]) {
                                temp.value = el[self.myConfig.yfieldname];
                            } else {
                                temp.value =  0;
                            }
                            data.push(temp);
                            return true;
                        }
                    })
                });
            }
            // 处理分类
            curlConfig.legend.data = legendData;
            // 处理数据
            curlConfig.series[0].data = data;
            // 处理颜色
            if (!this.mychartconfig.color) {
                if (legendData.length > 0) {
                    curlConfig.color  = this._getGlobalColor('pie', legendData.length);
                } 
            } 
            return curlConfig;
        }
       
    }
};
</script>