| | |
| | | onUnmounted, |
| | | } from "vue"; |
| | | import dayjs from "dayjs"; |
| | | import { getRainfall } from "@/api"; |
| | | import { useSimStore } from "@/store/simulation"; |
| | | const simStore = useSimStore(); |
| | | const { rainFalls, intensityUnit } = simStore; |
| | | |
| | | let dataIntervalId = null; // 定时器 ID |
| | | let dataIntervalId = null; // 表格定时器 ID |
| | | const jsonData = ref([]); // JSON 数据 |
| | | const tableData = ref([]); // 表格数据 |
| | | const currentIndex = ref(0); // 当前加载索引 |
| | | const isPaused = ref(false); // 是否暂停标志 |
| | | const chart1Data = ref(null); |
| | | const chart2Data = ref(null); |
| | | let intervalId1 = null; |
| | | let intervalId2 = null; |
| | | const chart1Data = ref(null); //降雨数据 |
| | | const chart2Data = ref(null); //断面数据 |
| | | let intervalId1 = null; //降雨数据定时器 |
| | | let intervalId2 = null; //断面数据定时器 |
| | | |
| | | // 根据时间轴匹配的x轴的时间显示 |
| | | const nowTime = ref(null); |
| | | |
| | | const isFinished = ref(true); |
| | | |
| | | const props = defineProps({ |
| | | isDynamicMode: { |
| | |
| | | watch( |
| | | () => props.isFinish, |
| | | (newVal) => { |
| | | isFinished.value = newVal; |
| | | if (!newVal) { |
| | | resetTable(); |
| | | chart1Data.value.resetLoading(); |
| | |
| | | EventBus.on("reset-table", () => { |
| | | resetTable(); // 调用重置表格的函数 |
| | | }); |
| | | |
| | | // 清除echarts图表 |
| | | EventBus.on("clear-echart", () => { |
| | | clearEchartData(); // 调用清除函数 |
| | | chart1Data.value.resetLoading(); |
| | | chart2Data.value.resetLoading(); |
| | | }); |
| | | |
| | | |
| | | // 清除 echart1/2数据的函数 |
| | | const clearEchartData = () => { |
| | | if (myChart1) { |
| | | const currentOption = myChart1.getOption(); // 获取当前图表配置 |
| | | currentOption.series.forEach((series) => { |
| | | series.data = []; // 清空每个系列的数据 |
| | | }); |
| | | currentOption.xAxis[0].data = []; |
| | | myChart1.setOption(currentOption); |
| | | } |
| | | if (myChart2) { |
| | | const currentOption = myChart2.getOption(); |
| | | currentOption.series.forEach((series) => { |
| | | series.data = []; |
| | | }); |
| | | currentOption.xAxis[0].data = []; |
| | | myChart2.setOption(currentOption); |
| | | } |
| | | }; |
| | | // 清除威胁对象中的数据 |
| | | const resetTable = () => { |
| | | currentIndex.value = 0; |
| | |
| | | if (intervalId1) { |
| | | clearInterval(intervalId1); |
| | | chart1Data.value.stopUpdating(); // 每隔 1 秒更新一次 |
| | | |
| | | intervalId1 = null; |
| | | } |
| | | if (intervalId2) { |
| | | console.log(intervalId2, "暂停"); |
| | | clearInterval(intervalId2); |
| | | chart2Data.value.stopUpdating(); // 每隔 1 秒更新一次 |
| | | intervalId2 = null; |
| | |
| | | dataIntervalId = null; |
| | | } |
| | | }; |
| | | |
| | | // 监听时间轴结束模拟 |
| | | EventBus.on("hide-schemeInfo", handleHideSchemeInfo); |
| | | |
| | | // 监听父组件传递的数据变化 |
| | |
| | | // 触发事件,将当前行的 ID 发送到地图组件 |
| | | EventBus.emit("row-clicked", row.id); |
| | | } |
| | | const listData = cityData.listData; |
| | | const data = ref([ |
| | | 8.16, 15.38, 13.94, 9.46, 86.42, 71.32, 28.52, 25.9, 13.74, 14.54, 15.53, |
| | | 9.17, 0, 0.09, 0.86, 8.15, 44.8, 21.86, 6.2, 4.98, 2.82, 2.36, 3.1, 1.06, |
| | | ]); |
| | | |
| | | const rainChangeShow = ref(false); |
| | | const tableContainer = ref(null); |
| | | |
| | | let myChart1 = null; |
| | | let myChart2 = null; |
| | | |
| | | const getRandomInt = (min = 0, max = 100) => { |
| | | const minCeiled = Math.ceil(min); |
| | | const maxFloored = Math.floor(max); |
| | | return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); |
| | | }; |
| | | // 威胁对象数据 |
| | | const getDangerInfo = async () => { |
| | | try { |
| | | const response = await fetch( |
| | |
| | | }); |
| | | }; |
| | | |
| | | const getRainfallData = async () => { |
| | | try { |
| | | const res = await getRainfall(); |
| | | rainfallData.value = res.data; |
| | | } catch (error) { |
| | | console.error("Error fetching rainfall data:", error); |
| | | } |
| | | }; |
| | | |
| | | const rainClick = () => { |
| | | rainChangeShow.value = !rainChangeShow.value; |
| | | let desc = { func_name: "RainChange", visibility: rainChangeShow.value }; |
| | |
| | | }; |
| | | |
| | | const debuffClick = () => { |
| | | // Assuming you have access to parent components in a different way in Vue 3 |
| | | // You might need to use provide/inject or props/emits instead |
| | | console.log("Debuff click"); |
| | | }; |
| | | |
| | |
| | | charts.style.height = style.height; |
| | | }; |
| | | |
| | | // 时间轴时间截取处理 |
| | | const syncTimeWithTimeline = () => { |
| | | // 2025-05-24 00:25 |
| | | // // 将时间字符串转换为分钟数 (格式: "YYYY-MM-DD mm:ss") |
| | | const timeParts = nowTime.value.split(" "); |
| | | const timeOnly = timeParts[1]; // 获取 "mm:ss" 部分 |
| | | return timeOnly; |
| | | if (nowTime.value) { |
| | | const timeParts = nowTime.value.split(" "); |
| | | const timeOnly = timeParts[1]; // 获取 "mm:ss" 部分 |
| | | return timeOnly; |
| | | } |
| | | }; |
| | | |
| | | const setEcharts1 = () => { |
| | | const chartDom = document.getElementById("echarts1"); |
| | | myChart1 = echarts.init(chartDom); |
| | | const myChart1 = echarts.init(chartDom); |
| | | |
| | | let rainfallData = ref([]); // 存储从 JSON 文件中加载的数据 |
| | | let data1 = ref([]); // 降雨数据数据 |
| | | let data2 = ref([]); // 累计雨量数据 |
| | | let xAxisData = ref([]); // 动态时间轴 |
| | | let updateInterval = null; // 定时器变量 |
| | | let dataIndex = ref(0); // 当前数据索引,用于按顺序更新 |
| | | // 图表数据 |
| | | let rainfallData = ref([]); |
| | | let data1 = ref([]); |
| | | let data2 = ref([]); |
| | | let xAxisData = ref(["00:00"]); |
| | | let updateInterval = null; |
| | | let dataIndex = ref(0); |
| | | |
| | | // 加载 JSON 数据 |
| | | const loadJsonData = async () => { |
| | | try { |
| | | const response = await fetch("/json/rainfall.json"); |
| | | const result = await response.json(); |
| | | if (result && result.data && Array.isArray(result.data)) { |
| | | rainfallData.value = result.data; |
| | | |
| | | // 计算 value 和 total 的最大值 |
| | | const maxValue = Math.max( |
| | | ...rainfallData.value.map((item) => item.value) |
| | | ); |
| | | const maxTotal = Math.max( |
| | | ...rainfallData.value.map((item) => item.total) |
| | | ); |
| | | |
| | | // 初始化时间轴(从00:00开始) |
| | | xAxisData.value = ["00:00"]; // 初始时间点 |
| | | |
| | | // 初始化图表(仅加载第一个数据点) |
| | | if (rainfallData.value.length > 0) { |
| | | data1.value.push(rainfallData.value[0].value); // 降雨数据 |
| | | data2.value.push(rainfallData.value[0].total); // 累计雨量 |
| | | } |
| | | |
| | | // 根据最大值设置 y 轴参数并初始化图表 |
| | | const yAxis1Params = calculateYAxisParams(maxValue); |
| | | const yAxis2Params = calculateYAxisParams(maxTotal); |
| | | updateChart(yAxis1Params, yAxis2Params); |
| | | } else { |
| | | console.error( |
| | | "Invalid JSON format: 'data' is missing or not an array!" |
| | | ); |
| | | } |
| | | } catch (error) { |
| | | console.error("Error fetching data:", error); |
| | | } |
| | | }; |
| | | |
| | | // 计算 y 轴参数(最多有 4 个点) |
| | | const calculateYAxisParams = (max) => { |
| | | const step = Math.ceil(max / 3); // 最多有 4 个点(包括 0),所以分成 3 份 |
| | | // 动态计算Y轴范围 |
| | | const getDynamicYAxis = (dataArray) => { |
| | | const currentMax = Math.max(...dataArray, 1); |
| | | const step = Math.ceil(currentMax / 3); |
| | | return { |
| | | max: step * 3, // 确保最大值是步长的整数倍 |
| | | max: step * 3, |
| | | interval: step, |
| | | }; |
| | | }; |
| | | |
| | | // 加载JSON数据 |
| | | const loadJsonData = async () => { |
| | | try { |
| | | const result = simStore.rainFalls; |
| | | if (result?.length) { |
| | | rainfallData.value = result; |
| | | |
| | | // 判断 intensityUnit 是否为 mm/15min |
| | | if (rainfallData.value.length > 0) { |
| | | // 如果是 mm/15min,则将所有 intensity * 60 |
| | | if (intensityUnit === "mm/15min") { |
| | | rainfallData.value = rainfallData.value.map((item) => ({ |
| | | ...item, |
| | | intensity: item.intensity * 60, |
| | | total: item.total * 60, |
| | | })); |
| | | } |
| | | |
| | | // 初始化 data1 和 data2(从 0 开始) |
| | | data1.value = [0]; |
| | | data2.value = [0]; |
| | | |
| | | // 使用第一个数据项的 time 作为初始值 |
| | | xAxisData.value = [rainfallData.value[0]?.time || "00:00"]; |
| | | |
| | | updateChart(); |
| | | } |
| | | } |
| | | } catch (error) { |
| | | console.error("数据加载失败:", error); |
| | | } |
| | | }; |
| | | |
| | | // 更新图表配置 |
| | | const updateChart = (yAxis1Params, yAxis2Params) => { |
| | | const updateChart = () => { |
| | | const option = { |
| | | animation: false, // 禁用动画 |
| | | tooltip: { |
| | | trigger: "axis", |
| | | axisPointer: { |
| | | type: "cross", |
| | | crossStyle: { |
| | | color: "#fff", |
| | | }, |
| | | }, |
| | | }, |
| | | animation: false, |
| | | tooltip: { trigger: "axis" }, |
| | | grid: { |
| | | left: "1%", |
| | | right: "1%", |
| | | bottom: "1%", |
| | | containLabel: true, |
| | | containLabel: false, |
| | | }, |
| | | legend: { |
| | | data: ["降雨数据", "累计雨量"], |
| | | textStyle: { |
| | | color: "#fff", |
| | | textStyle: { color: "#fff" }, |
| | | right: "10px", |
| | | selected: { |
| | | 降雨数据: true, |
| | | 累计雨量: true, |
| | | }, |
| | | right: "10px", // 将图例靠右贴边 |
| | | }, |
| | | xAxis: [ |
| | | { |
| | | type: "category", |
| | | data: xAxisData.value, // 使用动态时间轴 |
| | | axisPointer: { |
| | | type: "shadow", |
| | | }, |
| | | axisLabel: { |
| | | color: "#fff", |
| | | rotate: 0, // 将旋转角度设置为0,取消倾斜 |
| | | }, |
| | | data: xAxisData.value, |
| | | axisLabel: { color: "#fff", rotate: 0 }, |
| | | }, |
| | | ], |
| | | yAxis: [ |
| | |
| | | type: "value", |
| | | name: "单位:mm", |
| | | min: 0, |
| | | max: yAxis1Params.max, |
| | | interval: yAxis1Params.interval, |
| | | axisLabel: { |
| | | formatter: "{value}", |
| | | color: "#fff", |
| | | align: "right", // 将标签右对齐 |
| | | }, |
| | | ...getDynamicYAxis(data1.value), |
| | | axisLabel: { color: "#fff" }, |
| | | splitLine: { show: false }, |
| | | nameTextStyle: { |
| | | padding: [0, 0, 0, 30], // 在右侧添加一些内边距 |
| | | color: "#fff", |
| | | }, |
| | | splitLine: { |
| | | show: false, // 关闭左侧 y 轴的横线 |
| | | }, |
| | | }, |
| | | { |
| | | type: "value", |
| | | name: "单位:mm", |
| | | min: 0, |
| | | max: yAxis2Params.max, |
| | | interval: yAxis2Params.interval, |
| | | axisLabel: { |
| | | formatter: "{value}", |
| | | color: "#fff", |
| | | align: "left", // 将标签右对齐 |
| | | }, |
| | | ...getDynamicYAxis(data2.value), |
| | | axisLabel: { color: "#fff" }, |
| | | splitLine: { show: true }, |
| | | nameTextStyle: { |
| | | padding: [0, 10, 0, 0], // 在右侧添加一些内边距 |
| | | color: "#fff", |
| | | }, |
| | | splitLine: { |
| | | show: true, // 保留右侧 y 轴的横线 |
| | | }, |
| | | }, |
| | | ], |
| | |
| | | { |
| | | name: "降雨数据", |
| | | type: "bar", |
| | | tooltip: { |
| | | valueFormatter: function (value) { |
| | | return value + " mm/min"; |
| | | }, |
| | | }, |
| | | data: data1.value, |
| | | itemStyle: { |
| | | color: "#3268fe", |
| | | }, |
| | | label: { |
| | | show: false, |
| | | color: "#fff", |
| | | }, |
| | | itemStyle: { color: "#3268fe" }, |
| | | }, |
| | | { |
| | | name: "累计雨量", |
| | | type: "line", |
| | | yAxisIndex: 1, |
| | | tooltip: { |
| | | valueFormatter: function (value) { |
| | | return value + " mm"; |
| | | }, |
| | | }, |
| | | data: data2.value, |
| | | lineStyle: { |
| | | color: "#ffb637", |
| | | }, |
| | | label: { |
| | | show: false, // 确保标签不显示 |
| | | color: "#fff", |
| | | }, |
| | | lineStyle: { color: "#ffb637" }, |
| | | }, |
| | | ], |
| | | }; |
| | | |
| | | myChart1.setOption(option); |
| | | myChart1.setOption(option, true); |
| | | }; |
| | | |
| | | // 定义按顺序更新数据的方法 |
| | | // 数据更新 - 每次添加一条数据 |
| | | const updateData = () => { |
| | | if (dataIndex.value < rainfallData.value.length) { |
| | | // 获取当前索引的数据项 |
| | | const newItem = rainfallData.value[dataIndex.value]; |
| | | data1.value.push(newItem.value); // 添加降雨数据 |
| | | data2.value.push(newItem.total); // 添加累计雨量 |
| | | |
| | | const nextTime = syncTimeWithTimeline(); |
| | | xAxisData.value.push(nextTime); |
| | | |
| | | // 更新当前索引 |
| | | const item = rainfallData.value[dataIndex.value]; |
| | | data1.value.push(item.intensity); |
| | | data2.value.push(item.total); |
| | | xAxisData.value.push(item.time); // ✅ 改用 item.time |
| | | dataIndex.value++; |
| | | |
| | | // 更新图表 |
| | | updateChart( |
| | | { |
| | | max: myChart1.getOption().yAxis[0].max, |
| | | interval: myChart1.getOption().yAxis[0].interval, |
| | | }, // 左侧 y 轴保持不变 |
| | | { |
| | | max: myChart1.getOption().yAxis[1].max, |
| | | interval: myChart1.getOption().yAxis[1].interval, |
| | | } // 右侧 y 轴保持不变 |
| | | ); |
| | | updateChart(); |
| | | } else { |
| | | console.log("All data has been displayed."); |
| | | stopUpdating(); // 停止定时更新 |
| | | stopUpdating(); |
| | | } |
| | | }; |
| | | |
| | | // 启动定时更新 |
| | | const startUpdating = (interval = 1000) => { |
| | | if (!updateInterval) { |
| | | updateInterval = setInterval(updateData, interval); // 每隔 interval 毫秒更新一次数据 |
| | | console.log("Started updating..."); |
| | | } |
| | | // 控制方法:精确控制动画时间,最后一帧在第 90 秒 |
| | | const startUpdating = () => { |
| | | if (updateInterval || dataIndex.value >= rainfallData.value.length) return; |
| | | |
| | | const totalDuration = simStore.frameNum * 1000; // 90秒 |
| | | const totalPoints = rainfallData.value.length; |
| | | const startTime = Date.now(); |
| | | |
| | | const animate = (index = 0) => { |
| | | if (index >= totalPoints) { |
| | | stopUpdating(); |
| | | return; |
| | | } |
| | | |
| | | const now = Date.now(); |
| | | const expectedTime = (index / (totalPoints - 1)) * totalDuration; |
| | | const delay = Math.max(0, startTime + expectedTime - now); |
| | | |
| | | updateInterval = setTimeout(() => { |
| | | dataIndex.value = index + 1; // 因为是从 0 开始 push 的 |
| | | updateData(); |
| | | animate(index + 1); |
| | | }, delay); |
| | | }; |
| | | |
| | | animate(dataIndex.value); |
| | | }; |
| | | |
| | | // 停止定时更新 |
| | | const stopUpdating = () => { |
| | | if (updateInterval) { |
| | | clearInterval(updateInterval); |
| | | updateInterval = null; |
| | | console.log("Stopped updating..."); |
| | | } |
| | | clearTimeout(updateInterval); |
| | | updateInterval = null; |
| | | }; |
| | | |
| | | // 重置加载 |
| | | const resetLoading = () => { |
| | | stopUpdating(); // 停止定时器 |
| | | dataIndex.value = 0; // 重置数据索引 |
| | | data1.value = []; // 清空降雨数据数据 |
| | | data2.value = []; // 清空累计雨量数据 |
| | | xAxisData.value = ["00:00"]; // 重置时间轴 |
| | | |
| | | // 重新加载第一个数据点 |
| | | if (rainfallData.value.length > 0) { |
| | | data1.value.push(rainfallData.value[0].value); // 降雨数据 |
| | | data2.value.push(rainfallData.value[0].total); // 累计雨量 |
| | | } |
| | | |
| | | // 重新绘制图表 |
| | | updateChart( |
| | | { |
| | | max: myChart1.getOption().yAxis[0].max, |
| | | interval: myChart1.getOption().yAxis[0].interval, |
| | | }, // 左侧 y 轴保持不变 |
| | | { |
| | | max: myChart1.getOption().yAxis[1].max, |
| | | interval: myChart1.getOption().yAxis[1].interval, |
| | | } // 右侧 y 轴保持不变 |
| | | ); |
| | | |
| | | console.log("Reset loading..."); |
| | | stopUpdating(); |
| | | dataIndex.value = 0; |
| | | data1.value = [0]; |
| | | data2.value = [0]; |
| | | xAxisData.value = [rainfallData.value[0]?.time || "00:00"]; |
| | | updateChart(); |
| | | }; |
| | | |
| | | // 初始化加载 JSON 数据 |
| | | // 初始化 |
| | | loadJsonData(); |
| | | |
| | | return { |
| | |
| | | |
| | | const setEcharts2 = () => { |
| | | const chartDom = document.getElementById("echarts2"); |
| | | myChart2 = echarts.init(chartDom); |
| | | const myChart2 = echarts.init(chartDom); |
| | | |
| | | let jsonData = ref([]); // 存储从 JSON 文件中加载的数据 |
| | | let data1 = ref([]); // 实时流量数据 |
| | | let data2 = ref([]); // 累计流量数据 |
| | | let xAxisData = ref([]); // 动态时间轴 |
| | | let updateInterval = null; // 定时器变量 |
| | | let dataIndex = ref(0); // 当前数据索引,用于按顺序更新 |
| | | // 图表数据(与echarts1保持相同结构) |
| | | let flowData = ref([]); // 原始数据 |
| | | let data1 = ref([]); // 实时流量 |
| | | let data2 = ref([]); // 累计流量 |
| | | let xAxisData = ref(["00:00"]); |
| | | let updateInterval = ref(null); |
| | | let dataIndex = ref(0); |
| | | |
| | | // 加载 JSON 数据 |
| | | // 动态计算Y轴范围 |
| | | const calculateDynamicYAxis = (dataArray) => { |
| | | const currentMax = Math.max(...dataArray, 1); |
| | | const step = Math.ceil(currentMax / 3); |
| | | return { |
| | | max: step * 3, |
| | | interval: step, |
| | | }; |
| | | }; |
| | | const loadJsonData = async () => { |
| | | try { |
| | | const response = await fetch("/json/于家西沟断面下数据.json"); |
| | | const result = await response.json(); |
| | | if (result && result.data && Array.isArray(result.data)) { |
| | | jsonData.value = result.data; |
| | | |
| | | // 计算 value 和 total 的最大值 |
| | | const maxValue = Math.max(...jsonData.value.map((item) => item.value)); |
| | | const maxTotal = Math.max(...jsonData.value.map((item) => item.total)); |
| | | |
| | | // 初始化时间轴(从00:00开始) |
| | | xAxisData.value = ["00:00"] || syncTimeWithTimeline(); // 初始时间点 |
| | | |
| | | // 初始化图表(仅加载第一个数据点) |
| | | if (jsonData.value.length > 0) { |
| | | data1.value.push(jsonData.value[0].value); // 实时流量 |
| | | data2.value.push(jsonData.value[0].total); // 累计流量 |
| | | if (result?.data?.length) { |
| | | flowData.value = result.data; |
| | | if (flowData.value.length > 0) { |
| | | data1.value = [flowData.value[0].value]; |
| | | data2.value = [flowData.value[0].total]; |
| | | updateChart(); |
| | | } |
| | | |
| | | // 根据最大值设置 y 轴参数并初始化图表 |
| | | const yAxis1Params = calculateYAxisParams(maxValue); |
| | | const yAxis2Params = calculateYAxisParams(maxTotal); |
| | | updateChart(yAxis1Params, yAxis2Params); |
| | | } else { |
| | | console.error( |
| | | "Invalid JSON format: 'data' is missing or not an array!" |
| | | ); |
| | | } |
| | | } catch (error) { |
| | | console.error("Error fetching data:", error); |
| | | console.error("数据加载失败:", error); |
| | | } |
| | | }; |
| | | |
| | | // 计算 y 轴参数(最多有 4 个点) |
| | | const calculateYAxisParams = (max) => { |
| | | const step = Math.ceil(max / 3); // 最多有 4 个点(包括 0),所以分成 3 份 |
| | | return { |
| | | max: step * 3, // 确保最大值是步长的整数倍 |
| | | interval: step, |
| | | }; |
| | | }; |
| | | |
| | | // 更新图表配置 |
| | | const updateChart = (yAxis1Params, yAxis2Params) => { |
| | | // 图表配置(与echarts1保持相同结构和样式) |
| | | const updateChart = () => { |
| | | const option = { |
| | | animation: false, // 禁用动画 |
| | | animation: false, |
| | | tooltip: { |
| | | trigger: "axis", |
| | | axisPointer: { |
| | | type: "cross", |
| | | crossStyle: { |
| | | color: "#fff", |
| | | }, |
| | | }, |
| | | axisPointer: { type: "cross" }, |
| | | }, |
| | | grid: { |
| | | left: "1%", |
| | | right: "1%", |
| | | // 注释是因为 y轴上的单位被覆盖掉了 |
| | | // left: "1%", |
| | | // right: "1%", |
| | | bottom: "1%", |
| | | containLabel: true, |
| | | containLabel: false, |
| | | }, |
| | | legend: { |
| | | data: ["实时流量", "累计流量"], |
| | | textStyle: { |
| | | color: "#fff", |
| | | textStyle: { color: "#fff" }, |
| | | right: "10px", |
| | | selected: { |
| | | 实时流量: true, |
| | | 累计流量: true, |
| | | }, |
| | | right: "10px", // 将图例靠右贴边 |
| | | }, |
| | | xAxis: [ |
| | | { |
| | | type: "category", |
| | | data: xAxisData.value, // 使用动态时间轴 |
| | | axisPointer: { |
| | | type: "shadow", |
| | | }, |
| | | data: xAxisData.value, |
| | | axisLabel: { |
| | | color: "#fff", |
| | | rotate: 0, // 将旋转角度设置为0,取消倾斜 |
| | | rotate: 0, |
| | | }, |
| | | }, |
| | | ], |
| | |
| | | type: "value", |
| | | name: "单位:m³/min", |
| | | min: 0, |
| | | max: yAxis1Params.max, |
| | | interval: yAxis1Params.interval, |
| | | axisLabel: { |
| | | formatter: "{value}", |
| | | color: "#fff", |
| | | align: "right", // 将标签右对齐 |
| | | }, |
| | | ...calculateDynamicYAxis(data1.value), |
| | | axisLabel: { color: "#fff" }, |
| | | splitLine: { show: false }, |
| | | nameTextStyle: { |
| | | padding: [0, 0, 0, 20], // 在右侧添加一些内边距 |
| | | color: "#fff", |
| | | }, |
| | | splitLine: { |
| | | show: false, // 关闭左侧 y 轴的横线 |
| | | }, |
| | | }, |
| | | { |
| | | type: "value", |
| | | name: "单位:m³", |
| | | min: 0, |
| | | max: yAxis2Params.max, |
| | | interval: yAxis2Params.interval, |
| | | axisLabel: { |
| | | formatter: "{value}", |
| | | color: "#fff", |
| | | align: "left", // 将标签右对齐 |
| | | }, |
| | | ...calculateDynamicYAxis(data2.value), |
| | | axisLabel: { color: "#fff" }, |
| | | splitLine: { show: true }, |
| | | nameTextStyle: { |
| | | padding: [0, 10, 0, 0], // 在右侧添加一些内边距 |
| | | color: "#fff", |
| | | }, |
| | | splitLine: { |
| | | show: true, // 保留右侧 y 轴的横线 |
| | | }, |
| | | }, |
| | | ], |
| | |
| | | { |
| | | name: "实时流量", |
| | | type: "bar", |
| | | tooltip: { |
| | | valueFormatter: function (value) { |
| | | return value + " m³/min"; |
| | | }, |
| | | }, |
| | | data: data1.value, |
| | | itemStyle: { |
| | | color: "blue", // 设置柱状图颜色为蓝色 |
| | | }, |
| | | label: { |
| | | show: false, |
| | | color: "#fff", |
| | | color: "#3268fe", |
| | | }, |
| | | }, |
| | | { |
| | | name: "累计流量", |
| | | type: "line", |
| | | yAxisIndex: 1, |
| | | tooltip: { |
| | | valueFormatter: function (value) { |
| | | return value + " m³"; |
| | | }, |
| | | }, |
| | | data: data2.value, |
| | | lineStyle: { |
| | | color: "#ffb637", // 设置折线图线条颜色为黄色 |
| | | }, |
| | | label: { |
| | | show: false, // 确保标签不显示 |
| | | color: "#fff", |
| | | color: "#ffb637", |
| | | }, |
| | | }, |
| | | ], |
| | | }; |
| | | |
| | | myChart2.setOption(option); |
| | | myChart2.setOption(option, true); |
| | | }; |
| | | |
| | | // 定义按顺序更新数据的方法 |
| | | // 数据更新(与echarts1相同逻辑) |
| | | const updateData = () => { |
| | | if (dataIndex.value < jsonData.value.length) { |
| | | // 获取当前索引的数据项 |
| | | const newItem = jsonData.value[dataIndex.value]; |
| | | data1.value.push(newItem.value); // 添加实时流量 |
| | | data2.value.push(newItem.total); // 添加累计流量 |
| | | |
| | | const nextTime = syncTimeWithTimeline(); |
| | | xAxisData.value.push(nextTime); |
| | | |
| | | // 更新当前索引 |
| | | dataIndex.value += 2; |
| | | |
| | | // 更新图表 |
| | | updateChart( |
| | | { |
| | | max: myChart2.getOption().yAxis[0].max, |
| | | interval: myChart2.getOption().yAxis[0].interval, |
| | | }, // 左侧 y 轴保持不变 |
| | | { |
| | | max: myChart2.getOption().yAxis[1].max, |
| | | interval: myChart2.getOption().yAxis[1].interval, |
| | | } // 右侧 y 轴保持不变 |
| | | ); |
| | | if (dataIndex.value < flowData.value.length) { |
| | | const item = flowData.value[dataIndex.value]; |
| | | data1.value.push(item.value); |
| | | data2.value.push(item.total); |
| | | xAxisData.value.push(syncTimeWithTimeline()); |
| | | dataIndex.value++; |
| | | updateChart(); |
| | | } else { |
| | | console.log("All data has been displayed."); |
| | | stopUpdating(); // 停止定时更新 |
| | | stopUpdating(); |
| | | } |
| | | }; |
| | | |
| | | // 启动定时更新 |
| | | // 控制方法(与echarts1完全一致) |
| | | const startUpdating = (interval = 1000) => { |
| | | if (!updateInterval) { |
| | | updateInterval = setInterval(updateData, interval); // 每隔 interval 毫秒更新一次数据 |
| | | console.log("Started updating..."); |
| | | updateInterval = setInterval(updateData, interval); |
| | | } |
| | | }; |
| | | |
| | | // 停止定时更新 |
| | | const stopUpdating = () => { |
| | | if (updateInterval) { |
| | | clearInterval(updateInterval); |
| | | updateInterval = null; |
| | | console.log("Stopped updating..."); |
| | | } |
| | | }; |
| | | |
| | | // 重置加载 |
| | | const resetLoading = () => { |
| | | stopUpdating(); // 停止定时器 |
| | | dataIndex.value = 0; // 重置数据索引 |
| | | data1.value = []; // 清空实时流量数据 |
| | | data2.value = []; // 清空累计流量数据 |
| | | xAxisData.value = ["00:00"]; // 重置时间轴 |
| | | |
| | | // 重新加载第一个数据点 |
| | | if (jsonData.value.length > 0) { |
| | | data1.value.push(jsonData.value[0].value); // 实时流量 |
| | | data2.value.push(jsonData.value[0].total); // 累计流量 |
| | | stopUpdating(); |
| | | dataIndex.value = 0; |
| | | data1.value = []; |
| | | data2.value = []; |
| | | xAxisData.value = ["00:00"]; |
| | | if (flowData.value.length > 0) { |
| | | data1.value = [flowData.value[0].value]; |
| | | data2.value = [flowData.value[0].total]; |
| | | } |
| | | |
| | | // 重新绘制图表 |
| | | updateChart( |
| | | { |
| | | max: myChart2.getOption().yAxis[0].max, |
| | | interval: myChart2.getOption().yAxis[0].interval, |
| | | }, // 左侧 y 轴保持不变 |
| | | { |
| | | max: myChart2.getOption().yAxis[1].max, |
| | | interval: myChart2.getOption().yAxis[1].interval, |
| | | } // 右侧 y 轴保持不变 |
| | | ); |
| | | |
| | | console.log("Reset loading..."); |
| | | updateChart(); |
| | | }; |
| | | |
| | | // 初始化加载 JSON 数据 |
| | | // 初始化 |
| | | loadJsonData(); |
| | | |
| | | return { |
| | |
| | | }; |
| | | |
| | | onMounted(() => { |
| | | // getRainfallData(); |
| | | // 时间轴时间的变化 |
| | | EventBus.on("time-update", (time) => { |
| | | nowTime.value = time; |
| | | }); |
| | |
| | | }); |
| | | |
| | | onBeforeUnmount(() => { |
| | | EventBus.off("time-update"); // 清理事件监听 |
| | | nowTime.value = null; |
| | | if (intervalId1) clearInterval(intervalId1); |
| | | if (intervalId2) clearInterval(intervalId2); |
| | |
| | | onUnmounted(() => { |
| | | EventBus.off("reset-table"); // 移除事件监听 |
| | | EventBus.off("clear-echart"); |
| | | EventBus.off("time-update"); // 清理事件监听 |
| | | }); |
| | | </script> |
| | | |