<template>
<div ref="chart" class="xp-chart-pie-double-ring"></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-ring',
    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',
                        center: [220, '50%'],
                        radius: ['35%', '60%'],
                        avoidLabelOverlap: false,
                        stillShowZeroSum: false,
                        label: {
                            normal: {
                                position: 'inside',
                                formatter: '{d}%',
                                color: '#fff',
                                fontSize: 12
                            }
                        },
                        data: []
                    }, {
                        type: 'pie',
                        center: [560, '50%'],
                        radius: ['35%', '60%'],
                        avoidLabelOverlap: false,
                        stillShowZeroSum: false,
                        label: {
                            normal: {
                                position: 'inside',
                                formatter: '{d}%',
                                color: '#fff',
                                fontSize: 12
                            }
                        },
                        data: []
                    }
                ]
            }
        };
    },
    mounted() {
        this.getData();
    },
    watch: {
        datasource: {
            deep: true,
            handler() {
                this.initChart();
            }
        }
    },
    methods: {
        getData() {
            this.$http.get('./chart/getdoublering', {
                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>
