<template>
    <div
        class="linechart"
        :id="id"
        style="width: calc(100vw - 270px); min-width: 1096px; height: calc(80vh - 260px)"
    ></div>
</template>

<script>
import echarts from "echarts";
import { mapState } from "vuex";

export default {
    name: "lineChartMainPage", // 首页-折线图
    props: {
        id: {
            default: "lineChartMainPage",
            type: String,
        },
        dataList: {
            default: function () {
                return [];
            },
            type: Array,
        },
    },
    data() {
        return {
            tipData: [], //线数据
            showData: [], //展示数据
            charts: "",
        };
    },

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

    computed: {
        ...mapState(["themeColor"]),
    },

    methods: {
        initStateList() {
            let xData = [];
            let yData = [];
            for (let index = 0; index < this.dataList.length; index++) {
                const element = this.dataList[index];
                xData.push(element.toDate.slice(5));
                yData.push(element.num);
            }

            let option = {
                title: {
                    text: "暂无数据",
                    show: this.dataList.length === 0,
                    left: "center",
                    top: "center",
                },
                tooltip: {
                    //提示框，可以在全局也可以在
                    trigger: "item", //提示框的样式
                    formatter: "{b}: {c}",
                    color: "#000", //提示框的背景色
                    textStyle: {
                        //提示的字体样式
                        color: "#ffffff",
                    },
                    confine: true,
                },
                xAxis: {
                    type: "category",
                    boundaryGap: false,
                    data: xData,
                },
                yAxis: {
                    minInterval: 1,
                },
                series: [
                    {
                        data: yData,
                        type: "line",
                        smooth: true,
                        symbol: "circle",
                        symbolSize: 6,
                        lineStyle: {
                            normal: {
                                width: 1.5,
                                color: this.themeColor,
                            },
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(
                                0,
                                0,
                                0,
                                1,
                                [
                                    {
                                        offset: 0,
                                        color: "rgba(51, 151, 255, 0.42)",
                                    },
                                    {
                                        offset: 1,
                                        color: "rgba(170, 197, 255, 0.12)",
                                    },
                                ]
                            ),
                        },
                        itemStyle: {
                            normal: {
                                color: this.themeColor,
                            },
                        },
                    },
                ],
                grid: {
                    top: "10%",
                    left: "20px",
                    right: "20px",
                    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() {
        // console.log("destroyed");
        this.charts.dispose();
    },
};
</script>

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