<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
import mixins from '@/mixins';
import {legend, grid, yAxis, xAxis} from '@/defaultSetting';
export default {
    mixins: [mixins],
    data () {
        return {
        };
    },
    mounted() {
        this.eveSet();
    },
    methods: {
        // 对画布绑定事件
        eveSet() {
            this.chart.on('click', (params) => {
                let event = params.event.event;
                event.stopPropagation();
                this.$emit('goToPage', params);
            });
        },
        updateChart () {
            const { legendColor, axisColor } = this.themeColor;
            let legends = [];
            let series = [];
            let that = this;
            this.data.values.forEach(item => {
                this.$set(item, 'data1', [])
                legends.push(item.name);
                series.push({
                    name: item.name,
                    type: 'bar',
                    smooth: true,
                    stack: 'all',
                    barMaxWidth: 20,
                    barCategoryGap:'20%',/*多个并排柱子设置柱子之间的间距*/
                    label: {
                        normal: {
                            show: true,
                            formatter: '{b}',
                            position: [10, -20],
                            color: axisColor || '#333'
                        }
                    },
                    itemStyle: {
                        normal: {
                            areaStyle: {type: 'default'},
                            color: this.setting.barColor
                        }},
                    data: item.data
                });
            });
            const options = {
                color: this.setting.color,
                title: {
                    text: this.data.title
                },
                tooltip: {
                    trigger: 'item',
                    formatter: function (params) {
                        let str = params.name + ':' + params.value + (that.unit || '')
                        return str;
                    }
                },
                toolbox: this.setting.toolbox,
                grid: {
                    ...grid,
                    left: '1%',
                    right: '5%'
                },
                legend: {
                    ...legend,
                    data: legends,
                    textStyle: {
                        color: legendColor || '#666'
                    }
                },
                xAxis: [{
                    ...xAxis,
                    type: 'value',
                    name: this.unit,
                    position: 'bottom',
                    splitLine: {lineStyle:{type:'dashed'}},
                    axisLabel: {
                        color: axisColor || '#333'
                    }
                }],
                yAxis: [{
                    ...yAxis,
                    type: 'category',
                    axisLine: {show: false},
                    axisLabel: {show: false},
                    axisTick: {show: false},
                    splitLine: {show: false},
                    data: this.data.keys.map(item => {
                        return item.replace(' ', '\n');
                    })
                }],
                series: series
            };
            this.chart.setOption(options, true);
        }
    }
};
</script>
