<template>
    <div class="linechart" :id="id" style="width: 100%; height: 100%"></div>
</template>

<script>
export default {
    name: "lineChartTaskInfo",
    props: {
        id: {
            default: "lineChartA",
            type: String,
        },
        xList: {
            default: function () {
                return [];
            },
            type: Array,
        },
        yList: {
            default: function () {
                return [];
            },
            type: Array,
        },
        yUnit: {
            default: "",
            type: String,
        },
    },

    data() {
        return {
            tipData: [], //线数据
            showData: [], //展示数据
            charts: "",
        };
    },

    watch: {
        yList(val) {
            if (val.length > 0) {
                this.initStateList();
            }
        },
    },

    methods: {
        initStateList() {
            let xData = this.xList;
            let yData = this.yList;

            let option = {
                title: {
                    text: "暂无数据",
                    show: yData.length === 0,
                    left: "center",
                    top: "center",
                },
                tooltip: {
                    //提示框，可以在全局也可以在
                    trigger: "item", //提示框的样式
                    formatter: "{b}: {c}",
                    // formatter: function (params) {
                    //     if (params.value == 0) return params.name + " : 0";
                    //     //格式化函数
                    //     return params.name + " : " + params.value.toFixed(4);
                    // },
                    color: "#000", //提示框的背景色
                    textStyle: {
                        //提示的字体样式
                        color: "#ffffff",
                    },
                    confine: true,
                },
                xAxis: {
                    name: "t(s)",
                    type: "category",
                    boundaryGap: false,
                    data: xData,
                },
                yAxis: {
                    name: this.yUnit,
                },
                series: [
                    {
                        data: yData,
                        type: "line",
                        smooth: true,
                        symbol: "circle",
                        symbolSize: 6,
                        lineStyle: {
                            normal: {
                                width: 1.5,
                                color: "#3397FF",
                            },
                        },
                        itemStyle: {
                            normal: {
                                color: "#3397FF",
                            },
                        },
                    },
                ],
                grid: {
                    top: "30px",
                    left: "30px",
                    right: "40px",
                    bottom: "10%",
                    containLabel: true,
                },
            };
            this.charts.setOption(option);
        },
    },

    mounted() {
        this.charts = this.$echarts.init(document.getElementById(this.id));

        this.initStateList();

        this.$nextTick(() => {
            window.addEventListener("resize", () => {
                this.charts.resize();
            });
        });
    },

    destroyed() {
        this.charts.dispose();
    },
};
</script>

<style lang='less' scoped>
.linechart {
    margin: 0 auto;
    overflow: hidden;
}
</style>