<template>
    <div id="myDiffPie"></div>
</template>

<script>
    export default {
        name: 'BaseDiffPie',
        props: ['pieId','echartsData'],
        data() {
            return {
                pieData: [],
				types:this.$GLOBAL.EXERCISE_DIFFS()
            }
        },
        methods: {

            drawLine(data) {
                let that = this
                // 基于准备好的dom，初始化echarts实例
                let myPie = this.$echarts.init(document.getElementById('myDiffPie'), 'chalk')

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        'text':  this.$t('evaluation.echarts.diffPie'),
                        'left': 'center',
                        'textStyle': {
                            'fontSize': 14,
                            'color': '#595959'
                        }
                    },
					legend: {
						left: 'center',
						bottom: 0,
						type: 'scroll',
						formatter: function (name,val) {
						    if(data.length){
						    	return name + ' (' + data.filter(i => i.name === name)[0].value + that.$t('unit.text10') + ')';
						    }
						}
					},
                    color: ['#8378ea', '#e7bcf3', '#9fe6b8', '#ff9f7f', '#fb7293', '#e7bcf3', '#8378ea'],
                    // color: new Array(10).fill("#"+Math.random().toString(16).slice(-6)),
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    calculable: true,
                    series: [
                        {
                            name: that.$t('evaluation.typePieTitle'),
                            type: 'pie',
                            radius: [0, 80],
                            data: data
                        }
                    ]
                }

                // 绘制图表
                myPie.setOption(option)

                window.addEventListener('resize', function() {
                    myPie.resize()
                })
            }
        },
        mounted() {
			let arr = []
			let tempArr = []
			this.echartsData.item.forEach(i => {
				if(i.type === 'compose' && i.children.length){
					tempArr.push(...i.children)
				}else{
					tempArr.push(i)
				}
			})
			let typeList = this._.groupBy(tempArr, 'level')
			for(let key in typeList){
				arr.push({
					value:typeList[key].length,
					name: this.types[+key - 1]
				})
			}
			this.drawLine(arr)
        },
        computed: {
            // 获取最新知识点占比饼图数据
            getPieData() {
                return this.$store.state.totalAnalysis.knowledgeData
            }
        },
        watch: {
            echartsData(val) {

            }
        }

    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

    #myDiffPie {
        width: 100%;
        height: 320px;
        margin: 20px auto;
        display: block;
    }
</style>
