自变化折线图(两周数据)

<template>
  <div id="main" @mouseenter="ClearInterval" @mouseleave="SetInterval"></div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
const timerData = ref<string[]>([]);
const seriesData = ref<string[]>([]);
const seriesData2 = ref<string[]>([]);
let changeBaseLibrary: any = null;
let myChart: any;
let num = 0;
const MyCharts = () => {
  myChart = echarts.getInstanceByDom(document.getElementById("main") as HTMLDivElement);
  if (myChart == null) {
    myChart = echarts.init(document.getElementById("main") as HTMLDivElement);
  }
  var option;
  option = {
    title: {
      // text: '数据合规态势'
    },
    grid: {
      show: false,
      top: "20%", // 一下数值可为百分比也可为具体像素值
      right: "5%",
      bottom: "15%",
      left: "10%",
    },
    tooltip: {
      trigger: "axis",
      textStyle: {
        // 提示框浮层的文本样式。
        color: "#000",
        align: "left",
      },
    },
    color: ["#98c0fd", "#ff0000"],
    legend: {
      icon: "stack",
      data: ["合规风险", "合规隐患"],
      textStyle: {
        color: "#fff",
      },
      itemHeight: 7,
      left: 150,
      top: 10
    },
    xAxis: {
      type: "category",
      data: timerData.value,
      axisTick: {
        show: false,
      },
      axisLine: {
        lineStyle: {
          color: "#00d4fe",
        },
      },
      axisLabel: {
        color: "#fff",
        interval: 0,
      },
    },
    yAxis: {
      name: " 单位: 个",
      nameTextStyle: {
        color: "#fff",
        fontSize: "13",
        fontWeight: "800",
        padding: [0, 0, -2, 0]
      },
      type: "value",
      axisLabel: {
        color: "#fff",
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#00d4fe",
        },
      },
      splitLine: {
        lineStyle: {
          // 设置背景横线
          type: "dashed",
          color: "#00d4fe",
        },
      },
    },
    series: [
      {
        name: "合规风险",
        data: seriesData.value,
        type: "line",
        smooth: true,
        showSymbol: false,
      },
      {
        name: "合规隐患",
        data: seriesData2.value,
        type: "line",
        smooth: true,
        showSymbol: false,
      },
    ],
  };

  option &&
    myChart.setOption(option, {
      notMerge: true, //不和之前的option合并
    });
};
onMounted(() => {
  getNowFormatDate();
  setTimeout(() => {
    if (num >= 9) {
      SetInterval();
    }
  }, 200);
  // 监听页面的大小
  window.addEventListener("resize", () => {
    myChart.resize();
  });
});
let date = new Date();
let year: number | string = date.getFullYear();
let month: number | string = date.getMonth() + 1;
let strDate: number | string = date.getDate();
let day = strDate;
let count = 0;
const getNowFormatDate = () => {
  num++;
  if (num != 1 && num != 2) {
    count++;
    if (day >= 1) {
      day--;
    }
  }
  if (count <= 14) {
    if (day < 1) {
      day = getCurrentMonthLast((month > 1 ? year : (year as number) -= 1) + '-' + (month > 1 ? (month as number) -= 1 : month = 12));
    }
  } else {
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = strDate as number;
    count = 0;
  }

  seriesData.value.push(`${Math.ceil(Math.random() * 150)}`);
  seriesData2.value.push(`${Math.ceil(Math.random() * 150)}`);
  timerData.value.push(year + "." + month + "." + day);

  num < 10 && setTimeout(getNowFormatDate, 10);
  num >= 10 && deleteFirst();
  MyCharts();
};
const deleteFirst = () => {
  seriesData.value.shift();
  seriesData2.value.shift();
  timerData.value.shift();
};
// 关闭计时器
const ClearInterval = () => {
  clearInterval(changeBaseLibrary);
  changeBaseLibrary = null;
};
// 启动计时器
const SetInterval = () => {
  changeBaseLibrary = setInterval(getNowFormatDate, 3000);
};
// 获取指定月的最后一天
const getCurrentMonthLast = (date) => {
  let endDate = new Date(date); //date 是需要传递的时间如:2018-08
  let month = endDate.getMonth();
  let nextMonth = ++month;
  let nextMonthFirstDay = new Date(endDate.getFullYear(), nextMonth, 1);
  let dateString = new Date(nextMonthFirstDay.setDate(nextMonthFirstDay.getDate() - 1));
  return dateString.getDate();
};
</script>
<style lang="scss" scoped>
#main {
  width: 100%;
  height: 90%;
  margin: 0 auto;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值