<template>
<div ref="chart" class="xp-chart-pie-double"></div>
</template>

<script>
/**
 * @file 双饼图
 * @author zhangping15
 * @date 2018-07-09
 */

import * as echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/pie';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/legend';

export default {
    name: 'chart-pie-double',
    data() {
        return {
            datasource: null,
            chart: {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} :<br/> {c} ({d}%)',
                    textStyle: {
                        fontSize: 12,
                        fontWeight: 100
                    }
                },
                color: ['#60ACFC', '#4EBECD', '#63D5B2', '#A1E497', '#DCEF7A'],
                legend: {
                    data: [],
                    orient: 'vertical',
                    top: 'middle',
                    itemWidth: 15,
                    itemHeight: 10,
                    itemGap: 15,
                    left: 780,
                    icon: 'rect',
                    textStyle: {
                        color: '#333',
                        padding: [0, 0, 0, 5]
                    }
                },
                series: [
                    {
                        type: 'pie',
                        radius: '55%',
                        center: [220, '50%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        label: {
                            normal: {
                                show: false
                            }
                        },
                        data: []
                    },  {
                        type: 'pie',
                        radius: '55%',
                        center: [560, '50%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        label: {
                            normal: {
                                show: false
                            }
                        },
                        data: []
                    }
                ]
            }
        };
    },
    mounted() {
        this.getData();
    },
    watch: {
        datasource: {
            deep: true,
            handler() {
                this.initChart();
            }
        }
    },
    methods: {
        getData() {
            this.$http.get('./chart/getdoublepie', {
                params: {}
            }).then(res => {
                this.datasource = res.data;
            });
        },
        initChart() {
            if (!this.datasource) {
                return;
            }

            this.chart.legend.data = this.datasource.series.map(item => item.name);
            this.chart.series[0].data = this.datasource.series;
            this.chart.series[1].data = this.datasource.series2;
            echarts.init(this.$refs.chart).setOption(this.chart);
        }
    }
};
</script>

<style lang="less" src="./index.less">

</style>
