<template>
    <chart-el :chartOpt="chartConf" />
</template>

<script>
import mixinChart from 'runner/common/mixins/mixinChart'

export default {
    name: 'BarPillarChart',

    mixins: [mixinChart],

    data () {
        return {
            chartConf: {}
        }
    },

    props: {
        // 图表布局配置 - width, height
        layout: {
            type: Object,
            default () {
                return {
                    width: '100%',
                    height: '136px'
                }
            }
        },

        // 图表配置
        option: {
            type: Object,
            default () {
                return {
                    base: {},
                    seriesData: []
                }
            }
        }
    },

    mounted () {
        this.initComponents()
    },

    methods: {
        initComponents () {
            const { width = '100%', height = '90px' } = this.layout
            const option = this.getChartOption() || {}
            this.chartConf = {
                width,
                height,
                option
            }
        },

        /**
         * @method getSeriesData 图表数据
         */
        getSeriesData () {
            const defSeriesOpts = {
                type: 'bar',
                barWidth: '7px',
                itemStyle: {
                    borderRadius: [3, 3, 0, 0],
                    color: {
                        // 图形渐变颜色方法，四个数字分别代表，右，下，左，上，offset表示0%到100%
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [
                            {
                                offset: 0,
                                color: '#24E8FF'
                            },
                            {
                                offset: 1,
                                color: '#00BEFF'
                            }
                        ]
                    }
                },
                label: {
                    show: false
                },
                data: []
            }

            let result = [defSeriesOpts]
            const { seriesData = [] } = this.option
            if (seriesData.length) {
                const temp = []
                seriesData.forEach(item => {
                    temp.push(this.deepMerge({}, defSeriesOpts, item))
                })
                result = temp
            }

            return result
        },

        /**
         * @method getSeriesData 图表数据
         */
        getChartOption () {
            const defaultOption = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'line'
                    }
                },
                title: {
                    show: false
                },
                grid: {
                    left: 0,
                    right: 0,
                    bottom: 10,
                    top: 10,
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    axisLabel: {
                        interval: 0,
                        show: true,
                        fontSize: 14,
                        color: '#469CCC'
                    },
                    data: []
                },
                yAxis: [
                    {
                        type: 'value',
                        max: 'dataMax',
                        axisTick: {
                            show: false
                        },
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: 'rgba(17, 63, 116, 1)'
                            }
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: 'rgba(17, 63, 116, 1)'
                            }
                        },
                        axisLabel: {
                            color: '#469CCC',
                            fontSize: 14,
                            fontFamily: 'CenturyGothic',
                            formatter: '{value}%'
                        }
                    }
                ]
            }
            // 添加series
            const result = this.deepMerge({}, defaultOption, this.option.base)
            result.series = this.getSeriesData()
            return result
        }
    }
}
</script>
