<template>
  <div class="h-full w-full" ref="MyEchart"></div>
</template>

<script>
export default {
  props: {
    dataset: {
      type: Object,
      default: () => {},
    },
    title: {
      type: String,
      default: "",
    },
    endValue: {
      type: Number,
      default: 6,
    },
    color: {
      type: String,
      default: "#0090FF",
    },
  },
  data() {
    return {
      myChart: null,
      timer: null,
    };
  },
  watch: {
    dataset: {
      handler(newVal) {
        this.$nextTick(() => {
          this.initChart(this.title, newVal);
        });
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    // 适配
    adapterEcharts() {
      this.myChart && this.myChart.resize();
    },
    initChart(title = "", dataset) {
      let data = dataset.source || [];
      let dom = this.$refs.MyEchart;
      this.myChart = this.$echarts.init(dom);
      let option = {
        title: {
          text: title,
          top: "5%",
          textStyle: {
            fontSize: 14,
          },
        },
        grid: {
          top: "25%",
          left: "15%",
          right: "5%",
          bottom: "12%",
        },
        // 鼠标移入信息查看
        tooltip: {
          trigger: "axis",
          borderWidth: 0,
        },
        dataZoom: [
          {
            // 开启数据平移
            type: "inside",
            show: true,
            startValue: 0,
            endValue: this.endValue - 1,
          },
        ],
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLabel: {
            textStyle: {
              color: "#333",
            },
          },
          axisLine: {
            lineStyle: {
              color: "#D9D9D9",
            },
          },
        },
        yAxis: {
          type: "value",
          splitNumber: 3, // 配置 Y 轴数值间隔
          axisLabel: {
            textStyle: {
              color: "#666",
            },
          },
          nameTextStyle: {
            color: "#666",
            fontSize: 12,
            lineHeight: 40,
          },
          splitLine: {
            lineStyle: {
              type: "dashed",
              color: "#E9E9E9",
            },
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        series: [
          {
            name: "数据量",
            type: "line",
            smooth: true,
            // showSymbol: false,
            symbolSize: 8,
            lineStyle: {
              color: this.hexToRgba(this.color, 1),
            },
            itemStyle: {
              color: this.hexToRgba(this.color, 1),
            },
            areaStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: this.hexToRgba(this.color, 0.3),
                  },
                  {
                    offset: 1,
                    color: this.hexToRgba(this.color, 0.1),
                  },
                ],
                global: false,
              },
              global: false,
            },
          },
        ],
        dataset,
      };
      this.myChart.setOption(option);
      // 适配
      window.addEventListener("resize", this.adapterEcharts);
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        if (option.dataZoom[0].endValue >= data.length - 1) {
          option.dataZoom[0].endValue = this.endValue - 1;
          option.dataZoom[0].startValue = 0;
        } else {
          option.dataZoom[0].endValue += 1;
          option.dataZoom[0].startValue += 1;
        }
        option && this.myChart.setOption(option);
      }, 2000);
      //鼠标移入组件时停止轮播
      this.myChart.on("mousemove", () => {
        clearInterval(this.timer); //清除循环
      });
      //鼠标移出组件时恢复轮播
      this.myChart.on("mouseout", () => {
        clearInterval(this.timer); //清除循环
        this.timer = setInterval(() => {
          if (option.dataZoom[0].endValue >= data.length - 1) {
            option.dataZoom[0].endValue = this.endValue - 1;
            option.dataZoom[0].startValue = 0;
          } else {
            option.dataZoom[0].endValue += 1;
            option.dataZoom[0].startValue += 1;
          }
          option && this.myChart.setOption(option);
        }, 2000);
      });
    },
    hexToRgba(hex, opacity) {
      let rgbaColor = "";
      let reg = /^#[\da-f]{6}$/i;
      if (reg.test(hex)) {
        rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt(
          "0x" + hex.slice(3, 5)
        )},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
      }
      return rgbaColor;
    },
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.adapterEcharts);
    clearInterval(this.timer);
  },
};
</script>

<style lang="less" scoped></style>
