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

<script>
/**
 * @file chart-bar
 * @author zhangping15
 * @date 2018-05-21
 */

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

export default {
    name: 'chart-bar',
    data() {
        return {
            datasource: null,
            chart: {
                grid: {
                    left: '3%',
                    right: '3%',
                    bottom: '10%',
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    textStyle: {
                        fontSize: 12
                    }
                },
                color: ['#60ACFC', '#AFE39B'],
                legend: {
                    data: [],
                    right: '10',
                    icon: 'rect',
                    itemWidth: 10,
                    itemHeight: 10,
                    itemGap: 25,
                    textStyle: {
                        color: '#333',
                        padding: [0, 0, 0, 5]
                    }
                },
                xAxis: {
                    type: 'category',
                    data: [],
                    axisLine: {
                        lineStyle: {
                            color: '#dbdbdb'
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#333',
                        fontSize: 12,
                        margin: 10
                    }
                },
                yAxis: {
                    type: 'value',
                    minInterval: 1,
                    splitLine: {
                        lineStyle: {
                            type: 'dashed',
                            color: ['#e7e7e7']
                        }
                    },
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#333',
                        fontSize: 12,
                        formatter: '{value}'
                    }
                },
                series: []
            },
            seriesItemDefault: {
                type: 'bar',
                barMaxWidth: 50,
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowColor: 'rgba(0,0,0,0.4)',
                            shadowBlur: 5,
                            shadowOffsetX: 3,
                            shadowOffsetY: 3
                        }
                    }
                }
            }
        };
    },
    watch: {
        datasource: {
            deep: true,
            handler() {
                this.initChart();
            }
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        getData() {
            this.$http.get('./chart/getbardata', {
                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.xAxis.data = this.datasource.xAxis;
            this.chart.series = this.datasource.series.map(item => {
                return {
                    ...item,
                    ...this.seriesItemDefault
                };
            });
            echarts.init(this.$refs.chart).setOption(this.chart);
        }
    }
};
</script>

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

</style>
