<template>
  <div  :style="currentStyle" ref="mychart">
  </div>
</template>

<script>
import Base from '@/components/mychart/basechart.vue';
// 引入雷达组件
require('echarts/lib/chart/radar');
export default {
    name: 'radar',
    extends: Base,
    data () {
        return {
            mychartconfig: {
                tooltip: {},
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: []
                },
                radar:{
                    // shape: 'circle',
                    name: {
                        textStyle: {
                            color: '#fff',
                            backgroundColor: '#999',
                            borderRadius: 3,
                            padding: [3, 5]
                        }
                    },
                    center: ['50%', '53%'],
                    indicator: []
                },
                series: []
            },
            myConfig:{
                cfieldname: '', // 类型字段名
                xfieldname: '', // 标题字段名
                yfieldname: '', // 值类型字段
                mychartconfig:{}
            }
        };
    },
    methods: {
        customInitMychartConfig () {
            let curlConfig = Object.assign({},this.mychartconfig,this.myConfig.mychartconfig); 
            let tempData = {};
            let xtempData = {};
            let legendData = [];
            let self = this;
            let series = [];
            if (this.currentData && this.currentData.length > 0) {
                this.currentData.forEach(el => {
                    if (self.myConfig.cfieldname) {
                        let name = el[self.myConfig.cfieldname];
                        if (!tempData[name])  {
                            tempData[name] = [];
                            legendData.push(name);
                        }
                        tempData[name].push(el);
                    }
                    if (self.myConfig.xfieldname && self.myConfig.yfieldname) {
                        let name = el[self.myConfig.xfieldname];
                        if (!xtempData[name])  {
                            xtempData[name] = {};
                            xtempData[name].value = Number(el[self.myConfig.yfieldname]);
                        } else {
                            if (Number(el[self.myConfig.yfieldname]) > xtempData[name].value) {
                                xtempData[name].value = Number(el[self.myConfig.yfieldname]);
                            }
                        }
                    }
                });
                for (let t in xtempData) {
                    let xObj = {};
                    xObj.name = t;
                    xObj.max = xtempData[t].value;
                    curlConfig.radar.indicator.push(xObj)
                }
                
                // 处理值
                let temp = {
                    type: 'radar'
                };
                temp.data = [];
                if (this.myConfig.cfieldname  && legendData.length > 0) {
                    legendData.forEach(c => {
                        let radarObj = {};
                        radarObj.name = c;
                        radarObj.value = [];
                        let xflag = false;
                        for (let t in xtempData) {
                            xflag = false;
                            self.currentData.some(el => {
                                if (el[self.myConfig.cfieldname] === c && el[self.myConfig.xfieldname] === t) {
                                    radarObj.value.push(Number(el[self.myConfig.yfieldname])); 
                                    xflag = true;
                                    return true;
                                }
                            })
                            if (!xflag) {
                                radarObj.value.push(0);
                            }
                        }
                        temp.data.push(radarObj);
                    });
                }
                series.push(temp);
            }
            // 处理分类
            curlConfig.legend.data = legendData;
           
            // 处理数据
            curlConfig.series = series;

            return curlConfig;
        }
       
    }
};
</script>