<script>
/**
 * vs-line-amass 2022-10-19
 *
 * width 图表宽度 330
 * height 图表高度 280
 * data 数据 []
 * smooth 是否平滑曲线显示 true
 * lineColor 折线颜色 二维数组  [['#80ffa5', '#01bfec'], ['#00ddff', '#4d77ff'], ['#37a2ff', '#7415db'], ['#ff0087', '#87009d']]  内置4个，超过自定义
 * 
 * @chartClick 图表点击触发回调 返回params参数
 * 
 *  setEchartsRefresh echarts刷新 事件
 */
export default {
    name: "vs-line-amass",
};
</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,
    },
    smooth: {
        type: Boolean,
        default: true,
    },
    data: {
        type: Array,
        default: () => [
            {
                name: "事项一", value: [
                    {
                        name: '2021',
                        value: 787
                    }, {
                        name: '2022',
                        value: 567
                    },
                ]
            },
            {
                name: "事项二", value: [
                    {
                        name: '2021',
                        value: 256
                    }, {
                        name: '2022',
                        value: 423
                    },
                ]
            },
            {
                name: "事项三", value: [
                    {
                        name: '2021',
                        value: 644
                    }, {
                        name: '2022',
                        value: 567
                    },
                ]
            },
            {
                name: "事项四", value: [
                    {
                        name: '2021',
                        value: 443
                    }, {
                        name: '2022',
                        value: 232
                    },
                ]
            },
        ]
    },
    lineColor: {
        type: Array,
        default: () => [['#80ffa5', '#01bfec'], ['#00ddff', '#4d77ff'], ['#37a2ff', '#7415db'], ['#ff0087', '#87009d']]
    }
});


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
    }
    _echarts = echarts.init(echartRef.value);

    let xAxisData = [];
    let seriesData = [];
    props.data.map((item) => {
        xAxisData.push(item.name);
        item.value.forEach((element, index) => {
            if (!seriesData[index]) {
                seriesData[index] = {};
                seriesData[index].name = element.name;
                seriesData[index].value = []
            }
            seriesData[index].value.push(element.value)
        });
    });

    const series = [];
    const legend = [];
    const color = [];
    seriesData.forEach((item, index) => {
        legend.push(item.name);
        color.push(props.lineColor[index][0])
        let seriesItem = {
            name: item.name,
            type: 'line',
            stack: 'Total',
            smooth: props.smooth,
            lineStyle: {
                width: 0
            },
            showSymbol: false,
            areaStyle: {
                opacity: 0.8,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: props.lineColor[index][0]
                    },
                    {
                        offset: 1,
                        color: props.lineColor[index][1]
                    }
                ])
            },
            emphasis: {
                focus: 'series'
            },
            data: item.value
        };
        series.push(seriesItem)
    });


    let option = {
        color: color,
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            width: '90%',
            type: 'scroll',
            pageTextStyle: {
                color: '#FFFFFF'
            },
            data: legend,
            itemWidth: 10,
            itemHeight: 10,
            itemStyle: {
                borderType: 'dashed'
            },
            textStyle: {
                color: '#FFFFFF',
                width: 100,
                overflow: 'truncate',

            },
        },
        grid: {
            top: "20%",
            left: "5%",
            right: "8%",
            bottom: "5%",
            containLabel: true,
        },
        xAxis: [
            {
                type: "category",
                axisLine: {
                    lineStyle: {
                        color: "rgba(255,255,255,.3)",
                    },
                },
                axisLabel: {
                    //坐标轴刻度标签的相关设置
                    textStyle: {
                        color: "#FFFFFF",
                    },
                },
                axisTick: { show: false },
                data: xAxisData
            }
        ],
        yAxis: [
            {
                type: "value",
                splitLine: {
                    lineStyle: {
                        color: "rgba(255,255,255,.3)",
                    },
                },
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: "#FFFFFF",
                    },
                },
                nameTextStyle: {
                    color: "#FFFFFF",
                },
                splitArea: {
                    show: false,
                },
            }
        ],
        series
    };

    _echarts.setOption(option);

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

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

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

defineExpose({
    setEchartsRefresh
});
</script>

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

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

.vsLineAmass {}
</style>
