<template>
    <div class="BarChart-box">
        <div ref="echart" class="BarChart-echarts" :style="`width : ${chart?.clientWidth}px`"/>
    </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
const props = defineProps({
    datas: {
        type: Array,
        default: () => ([])
    }
})

//初始变量
let chart = null;
const echart = ref(null)
const datas = ref(props.datas)

//监听
watch(() => [
    props.datas
], ([data]) => {
    datas.value = data
    setDatas(data)
})

//初始化图表
const initChart = () => {
    chart = echarts.init(echart.value)
    setDatas(props.datas)
}

//监听浏览器窗口变化
const windowResize = () => {
    window.addEventListener("resize", resizeEvent);
}

const resizeEvent = () => {
    window.requestAnimationFrame(() => {
        chart.resize();
    })
}

const onResize = () => {
    nextTick(() => {
        chart.resize();
    })
}

//设置数据
const setDatas = (data) => {
    let AxisData = [], neiYeRatio = [], waiYeRatio = [];
    for (let i = 0; i < data.length; i++) {
        AxisData.push(data[i].title)
        neiYeRatio.push(data[i]?.neiYeRatio)
        waiYeRatio.push(data[i]?.waiYeRatio)
    }
    setOptions(AxisData,neiYeRatio,waiYeRatio)
}

//设置图表
const setOptions = (AxisData,neiYeRatio,waiYeRatio) => {
    let xLabelWidth = ((chart.getWidth() * 1 - 30 - 40) / AxisData.length) - 30
    chart.setOption({
        color: ['#1ACC96', '#3187FF'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            data: ['内业', '外业']
        },
        xAxis: [
            {
                type: 'category',
                data: AxisData,
                axisLabel: {
                    width: xLabelWidth,
                    overflow:"breakAll",
                    interval: 0,
                    rotate: 0
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                interval: 30,
                axisLabel: {
                    formatter: '{value}%'
                }
            }
        ],
        series: [
            {
                name: '内业',
                type: 'bar',
                //stack: '名称1',
                data: neiYeRatio,
                tooltip: {
                    valueFormatter(value) {
                        return value + '%';
                    }
                },
            },
            {
                name: '外业',
                type: 'bar',
                //stack: '名称1',
                data: waiYeRatio,
                tooltip: {
                    valueFormatter(value) {
                        return value + '%';
                    }
                },
            }
        ],
    })
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        initChart()
        windowResize()
    })
})

//被卸载
onUnmounted(() => {
    window.removeEventListener("resize",resizeEvent);
    chart.dispose()
    chart = null
})

// 暴露出去
defineExpose({
    onResize
})
</script>

<style lang="scss" scoped>
.BarChart-box {
    height: 100%;
    overflow: hidden;
    position: relative;
    .BarChart-echarts {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2;
        width: 100%;
        height: 100%;
    }
}
</style>
