<template>
    <div id="sale-count" style="width: 100%;height: 260px"></div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
import {nextTick, onActivated, onMounted, watch} from "vue";


const props = defineProps({
    data: {
        type: Array,
        default: []
    }
})


onMounted(() => {
    initUserGrowthChart()
    window.addEventListener('resize', function () {
        myEcharts.resize()
    });
})

watch(() => props.data, () => {
    initUserGrowthChart()
})

onActivated(() => {
    nextTick(() => {
        myEcharts.resize()
    })
})

//
let myEcharts: any = null

const initUserGrowthChart = () => {
    if(myEcharts==null) {
        myEcharts = echarts.init(window.document.getElementById('sale-count') as HTMLElement)
    }
    const option = {
        xAxis: {
            name:'月份',
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
        },
        yAxis: {
            name:'销量',
            type: 'value',
            axisLine:{
                show:true
            },
            splitLine: {
                show: false
            }
        },
        grid: {
            top: 30 ,
            right: 40,
            bottom: 20,
            left: 30,
        },
        series: [
            {
                type: 'bar',
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)'
                },
                data: function () {
                    let res = []
                    for (let i in props.data) {
                        res.push([props.data[i].monthly,props.data[i].saleCount])
                    }
                    return res
                }(),
                barMaxWidth: '50',
                label:{
                    show:true
                }
            }
        ]
    }
    myEcharts.setOption(option)

}


</script>

<style scoped>

</style>
