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

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/dashboard/mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        },
        barAndLineX: {
            type: Array,
            default: () => ['机具']
        },
        // 折线图 Y 轴数据源1
        lineY1: {
            type: Array,
            default: () => {
                return []
            }
        },
        // 折线图 Y 轴数据源2
        lineY2: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        barAndLineX: {
            deep: true,
            handler() {
                this.initChart()
            }
        },
        lineY1: {
            deep: true,
            handler() {
                this.initChart()
            }
        },
        lineY2: {
            deep: true,
            handler() {
                this.initChart()
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.chartData)
        },
        setOptions({ expectedData, actualData } = {}) {
            this.chart.setOption({
                xAxis: {
                    type: 'category',
                    // data: ['卸扣5t', '卸扣8t', '手扳葫芦', '绞磨', '机具1', '机具2', '机具13'],
                    data: this.barAndLineX,
                    boundaryGap: false, // 折线与 y 轴不留间隙
                    axisTick: {
                        show: true // 隐藏刻度线
                    },
                    axisLabel: {
                        rotate: -15, // x 轴标签倾斜 45°
                        margin: 30, // 标签与轴线的距离
                        padding: [0, 20, 0, 0],
                        color: '#333'
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#a8a7a7' // x 轴轴线颜色
                        }
                    }
                },
                yAxis: {
                    axisTick: {
                        show: false // 隐藏刻度线
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#a8a7a7' // x 轴轴线颜色
                        }
                    }
                },
                grid: {
                    left: 10,
                    right: 50,
                    bottom: '12%', // 底部留出空间给图例
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross' // 十字准星指示器
                    },
                    padding: [5, 10]
                },
                legend: {
                    data: ['需求量', '待供应量'],
                    icon: 'path://M 0,0 h 16 a 6,6 0 0 1 6,6 v 8 a 6,6 0 0 1 -6,6 h -16 a 6,6 0 0 1 -6,-6 v -8 a 6,6 0 0 1 6,-6 z',
                    bottom: 10, // 图例放在 x 轴下方
                    itemWidth: 20, // 图例项宽度
                    itemHeight: 13, // 图例项高度
                    itemGap: 10, // 图例项之间的间距
                    padding: [5, 10], // 图例内边距
                    textStyle: {
                        color: '#333', // 图例文字颜色
                        lineHeight: 13
                    }
                },
                series: [
                    {
                        name: '需求量',
                        type: 'line',
                        smooth: false, // 平滑曲线
                        data: this.lineY1, // 数据
                        itemStyle: {
                            color: '#33aeff' // 折线颜色
                        },
                        lineStyle: {
                            color: '#33aeff', // 折线颜色
                            width: 1 // 折线宽度
                        },
                        areaStyle: {
                            color: 'rgba(56, 136, 250, 0.3)' // 区域背景颜色
                        },
                        animationDuration: 2800,
                        animationEasing: 'cubicInOut',
                        symbol: 'none', // 隐藏数据点圆点
                        showSymbol: false // 确保不显示数据点
                    },
                    {
                        name: '待供应量',
                        type: 'line',
                        smooth: false, // 平滑曲线
                        data: this.lineY2, // 数据
                        itemStyle: {
                            color: '#ff8e9d' // 折线颜色
                        },
                        lineStyle: {
                            color: '#ff8e9d', // 折线颜色
                            width: 1 // 折线宽度
                        },
                        areaStyle: {
                            color: ' rgba(255, 0, 90, 0.3)' // 区域背景颜色
                        },
                        animationDuration: 2800,
                        animationEasing: 'quadraticOut',
                        symbol: 'none', // 隐藏数据点圆点
                        showSymbol: false // 确保不显示数据点
                    }
                ]
            })
        }
    }
}
</script>
