<script>
/**
 * vs-Bar-horizontal 2022-10-08
 *
 * width 图表宽度 330
 * height 图表高度 280
 * barWidth 柱子宽度 10
 * data 数据 []
 * barColor 柱子渐变颜色 ['#1268f3', '#08a4fa', '#01ccfe']
 * labelColor 右侧文字颜色 rgba(0, 216, 255, 1)
 * 
 * @chartClick 图表点击触发回调 返回params参数
 * 
 *  setEchartsRefresh echarts刷新 事件
 */
export default {
    name: "vs-bar-horizontal",
};
</script>
<script setup>
import {
    ref,
    reactive,
    inject,
    onMounted
} from "vue";
const echarts = inject("$echarts");

const props = defineProps({
    width: {
        type: Number,
        default: 330,
    },
    height: {
        type: Number,
        default: 280,
    },
    data: {
        type: Array,
        default: () => [
            { name: "事项一", value: 2233 },
            { name: "事项二", value: 6325 },
            { name: "事项三", value: 4096 },
            { name: "事项四", value: 5066 },
        ]
    },
    barWidth: {
        type: Number,
        default: 10,
    },
    barColor: {
        type: Array,
        default: () => ['#1268f3', '#08a4fa', '#01ccfe']
    },
    labelColor: {
        type: String,
        default: 'rgba(0, 216, 255, 1)'
    }
});


const style = reactive({
    width: props.width + 'px',
    height: props.height + 'px',
})

const emit = defineEmits(["chartClick"]);
const echartRef = ref(null)

let _echarts = null;
const init = function () {
    if (_echarts !== null) {
        _echarts.dispose();
        _echarts = null
    }
    let xAxisData = [];
    let seriesData = [];
    props.data.map((item) => {
        xAxisData.push(item.name);
        seriesData.push(item.value);
    });


    _echarts = echarts.init(echartRef.value);
    let option = {
        barWidth: 8,
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow",
            },
        },
        grid: {
            top: "5%",
            left: "5%",
            right: "10%",
            bottom: "5%",
            containLabel: true,
        },
        xAxis: {
            nameTextStyle: {
                color: "#fff", // 四个数字分别为上右下左与原位置距离
            },
            splitLine: {
                show: false,
            },
            type: "value",
            boundaryGap: [0, 0.01],
            axisLine: {
                show: true, // 不显示坐标轴线
            },
            axisLabel: {
                textStyle: {
                    color: "#FFFFFF", //坐标值得具体的颜色
                    fontSize: 13,
                },
            },
        },
        yAxis: {
            type: "category",
            axisTick: {
                show: false, // 不显示坐标轴刻度线
            },
            data: xAxisData,
            axisLabel: {
                textStyle: {
                    color: "#FFFFFF", //坐标值得具体的颜色
                    fontSize: 13,
                },
            },
        },
        series: [
            {
                itemStyle: {
                    normal: {
                        //这里设置柱形图圆角 [左上角，右上角，右下角，左下角]
                        barBorderRadius: [0, 5, 5, 0],
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            1,
                            0,
                            [
                                {
                                    offset: 0,
                                    color: props.barColor[0], // 0% 处的颜色
                                },
                                {
                                    offset: 0.6,
                                    color: props.barColor[1], // 60% 处的颜色
                                },
                                {
                                    offset: 1,
                                    color: props.barColor[2], // 100% 处的颜色
                                },
                            ],
                            false
                        ),
                    },
                },
                label: {
                    show: true,
                    position: "right",
                    textStyle: {
                        fontSize: 14,
                        fontStyle: 'bolder',
                        color: props.labelColor,
                    },
                },
                type: "bar",
                data: seriesData,
                barWidth: props.barWidth,
            },
        ],
    };

    _echarts.setOption(option);

    _echarts.on('click', (params) => {
        emit("chartClick", params);
    })
}

onMounted(() => {
    init()
})

//图表刷新
const setEchartsRefresh = function () {
    init()
};

defineExpose({
    setEchartsRefresh
});
</script>

<template>
    <div :style="style" class="vsBarHorizontal" ref="echartRef"></div>
</template>

<style  lang="scss" scope>
$main: var(--main);
$auxiliary: var(--auxiliary);
$light: var(--light);

.vsBarHorizontal {}
</style>
