<script>
/**
 * vs-line-multiple 2022-10-19
 *
 * width 图表宽度 330
 * height 图表高度 280
 * lineWidth 折线宽度 3
 * data 数据 []
 * isArea 是否使用面积样式 true
 * smooth 是否平滑曲线显示 true
 * lineColor 折线颜色  ["#3deaff", "#FFB652", '#EF6482', '#87BF62', '#1999F7', '#CC6749', '#7216FD']  内置7个，超过自定义
 * 
 * @chartClick 图表点击触发回调 返回params参数
 * 
 *  setEchartsRefresh echarts刷新 事件
 */
export default {
    name: "vs-line-multiple",
};
</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,
    },
    lineWidth: {
        type: Number,
        default: 3,
    },
    isArea: {
        type: Boolean,
        default: true,
    },
    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: () => ["#3deaff", "#FFB652", '#EF6482', '#87BF62', '#1999F7', '#CC6749', '#7216FD']
    }
});


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 = [];

    seriesData.forEach((item, index) => {
        legend.push(item.name);
        let seriesItem = {
            name: item.name,
            type: "line",
            smooth: props.smooth, //是否平滑曲线显示
            symbolSize: 0,
            lineStyle: {
                normal: {
                    color: props.lineColor[index], // 线条颜色
                    shadowColor: props.lineColor[index] + '50',
                    shadowBlur: 10,
                    width: props.lineWidth
                },
            },
            data: item.value,
        };
        if (props.isArea) {
            seriesItem.areaStyle = {
                //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
                color: new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                        { offset: 0, color: props.lineColor[index] + 'CC' },
                        { offset: 1, color: props.lineColor[index] + '00' },
                    ],
                    false
                ),

                shadowColor: props.lineColor[index] + 'CC', //阴影颜色
                shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
            }
        }
        series.push(seriesItem)
    });


    let option = {
        tooltip: {
            trigger: "axis",
        },
        legend: {
            data: legend,
            itemWidth: 15,
            itemHeight: 3,
                       width:'90%',
            type: 'scroll',
            pageTextStyle: {
                color: '#FFFFFF'
            },
            textStyle: {
                color: '#FFFFFF',
                width: 100,
                overflow: 'truncate'
            },
        },
        grid: {
            top: "15%",
            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="vsLineMultiple" ref="echartRef"></div>
</template>

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

.vsLineMultiple {}
</style>
