guonan
2025-04-30 742cecf2dd52906ff00a50461839bda83a3cbc6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<template>
  <div class="right">
    <div class="right-top">
      <span>模拟结果</span>
    </div>
    <div class="echartsBox" id="layerBox">
      <div class="echartCont">
        <p style="cursor: pointer" @click="rainClick">降雨数据</p>
        <div class="echartBox">
          <div id="echarts1" style="width: 100%; height: 100%"></div>
        </div>
      </div>
      <div class="echartCont">
        <p>断面模拟</p>
        <div class="echartBox">
          <div id="echarts2" style="width: 100%; height: 100%"></div>
        </div>
      </div>
      <div class="echartCont">
        <p style="cursor: pointer" @click="debuffClick">威胁对象</p>
        <div class="echartBox">
          <div class="table-container" ref="tableContainer">
            <el-table
              :data="tableData"
              style="width: 100%; font-size: 10px"
              height="100%"
              @row-click="handleRowClick"
            >
              <el-table-column label="影响区名称" width="30" align="center">
                <template #default="scope">
                  影响区{{ scope.row.zoneId }}
                </template>
              </el-table-column>
              <el-table-column
                prop="time"
                label="影响时间"
                width="50"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="population"
                label="人员(人)"
                width="23"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="room"
                label="房屋(间)"
                width="23"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="households"
                label="户数(户)"
                width="23"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="property"
                label="财产(万元)"
                width="23"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="maxDepth"
                label="最大水深(米)"
                width="28"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="maxVelocity"
                label="最大流速(m/s)"
                width="35"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="raininess"
                label="雨强(mm/h)"
                width="28"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="warningLevel"
                label="预警等级"
                width="28"
                align="center"
              ></el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import * as echarts from "echarts";
import { EventBus } from "@/eventBus"; // 引入事件总线
import {
  ref,
  onMounted,
  onBeforeUnmount,
  nextTick,
  watch,
  onUnmounted,
} from "vue";
import dayjs from "dayjs";
import { getRainfall } from "@/api";
 
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; //断面数据定时器
 
// 根据时间轴匹配的x轴的时间显示
const nowTime = ref(null);
 
const props = defineProps({
  isDynamicMode: {
    type: Boolean,
  },
  isFinish: {
    type: Boolean,
  },
});
 
watch(
  () => props.isFinish,
  (newVal) => {
    if (!newVal) {
      resetTable();
      chart1Data.value.resetLoading();
      chart1Data.value.startUpdating(1000); // 每隔 1 秒更新一次
      chart2Data.value.resetLoading();
      chart2Data.value.startUpdating(1000); // 每隔 1 秒更新一次
    }
  }
);
// 清除函数=====================================================================
// 监听事件总线的自定义事件
EventBus.on("reset-table", () => {
  resetTable(); // 调用重置表格的函数
});
 
// 清除echarts图表
EventBus.on("clear-echart", () => {
  chart1Data.value.resetLoading();
  chart2Data.value.resetLoading();
});
 
// 清除威胁对象中的数据
const resetTable = () => {
  currentIndex.value = 0;
  tableData.value = [];
  if (dataIntervalId) {
    clearInterval(dataIntervalId);
  }
  startAddingData();
};
 
// 暂停时停止所有的更新
const handleHideSchemeInfo = () => {
  // 停止所有动态更新
  if (intervalId1) {
    clearInterval(intervalId1);
    chart1Data.value.stopUpdating(); // 每隔 1 秒更新一次
    intervalId1 = null;
  }
  if (intervalId2) {
    clearInterval(intervalId2);
    chart2Data.value.stopUpdating(); // 每隔 1 秒更新一次
    intervalId2 = null;
  }
  if (dataIntervalId) {
    clearInterval(dataIntervalId);
    dataIntervalId = null;
  }
};
 
// 监听时间轴结束模拟
EventBus.on("hide-schemeInfo", handleHideSchemeInfo);
 
// 监听父组件传递的数据变化
watch(
  () => props.isDynamicMode,
  (isEnabled) => {
    isPaused.value = !isEnabled;
 
    if (isEnabled) {
      // 如果启用了动态模式
      if (jsonData.value.length === 0) {
        getDangerInfo(); // 加载初始数据
      } else {
        startAddingData(); // 开始添加数据
      }
 
      // 启动第一个图表的定时更新
      if (!intervalId1 && chart1Data.value) {
        intervalId1 = setInterval(() => {
          chart1Data.value.startUpdating(1000);
        }, 10);
      }
 
      // 启动第二个图表的定时更新
      if (!intervalId2 && chart2Data.value) {
        intervalId2 = setInterval(() => {
          chart2Data.value.startUpdating(1000); // 每隔 1 秒更新一次
        }, 10);
      }
    } else {
      handleHideSchemeInfo();
    }
  },
  { immediate: true } // 立即执行监听器
);
// 点击数据实现面片闪动的触发函数
function handleRowClick(row) {
  console.log("Row clicked:", row);
  // 触发事件,将当前行的 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 getDangerInfo = async () => {
  try {
    const response = await fetch(
      "/json/listMaxInfluenceArea_wgs84_output.json"
    );
    const result = await response.json();
    console.log("Loaded JSON data:", result);
    if (result && result.data && Array.isArray(result.data.items)) {
      jsonData.value = result.data.items;
      console.log("jsonData is an array with length:", jsonData.value.length);
      startAddingData();
    } else {
      console.error(
        "Invalid JSON format: 'data.items' is missing or not an array!"
      );
    }
  } catch (error) {
    console.error("Error fetching data:", error);
  }
};
 
const startAddingData = () => {
  // 如果定时器已存在,先清除
  if (dataIntervalId) {
    clearInterval(dataIntervalId);
  }
 
  dataIntervalId = setInterval(() => {
    if (isPaused.value) {
      console.log("Loading is paused. Waiting for resume...");
      return; // 如果暂停,则不执行后续逻辑
    }
 
    if (currentIndex.value < jsonData.value.length) {
      const newItem = jsonData.value[currentIndex.value];
      tableData.value.unshift(newItem);
      scrollToTop();
      currentIndex.value++;
    } else {
      console.log("All items added.");
      clearInterval(dataIntervalId);
    }
  }, 2000);
};
 
const scrollToTop = () => {
  nextTick(() => {
    if (tableContainer.value) {
      tableContainer.value.scrollTop = 0;
    }
  });
};
 
const rainClick = () => {
  rainChangeShow.value = !rainChangeShow.value;
  let desc = { func_name: "RainChange", visibility: rainChangeShow.value };
  ps.emitMessage(desc);
};
 
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");
};
 
const chartssize = (container, charts) => {
  if (!charts || !charts.style) return;
  const style = window.getComputedStyle(container);
  charts.style.width = style.width;
  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;
};
 
const setEcharts1 = () => {
  // 1. 初始化图表
  const chartDom = document.getElementById("echarts1");
  myChart1 = echarts.init(chartDom);
 
  // 2. 数据定义(完全保持您的原始变量)
  let rainfallData = ref([]);
  let data1 = ref([]);
  let data2 = ref([]);
  let xAxisData = ref([]);
  let updateInterval = null;
  let dataIndex = ref(0);
 
 
  // 4. 动态计算Y轴(新增的核心逻辑)
  const getDynamicYAxis = (dataArray) => {
    const currentMax = Math.max(...dataArray, 1); // 确保最小显示范围
    const step = Math.ceil(currentMax / 3);
    return {
      max: step * 3,
      interval: step,
    };
  };
 
  // 5. 加载数据(完全保持您的逻辑)
  const loadJsonData = async () => {
    try {
      const response = await fetch("/json/rainfall.json");
      const result = await response.json();
 
      if (result?.data?.length) {
        rainfallData.value = result.data;
        xAxisData.value = ["00:00"] || syncTimeWithTimeline(); // 初始时间点
 
        if (rainfallData.value.length > 0) {
          data1.value = [rainfallData.value[0].value];
          data2.value = [rainfallData.value[0].total];
          updateChart(); // 初始化图表
        }
      }
    } catch (error) {
      console.error("数据加载失败:", error);
    }
  };
 
  // 6. 图表配置(仅修改Y轴部分)
  const updateChart = () => {
    const option = {
      // >>> 保持您原有的所有配置 <<<
      animation: false,
      tooltip: { trigger: "axis" },
      grid: { left: "1%", right: "1%", bottom: "1%", containLabel: true },
      legend: {
        data: ["降雨数据", "累计雨量"],
        textStyle: { color: "#fff" },
      },
 
      // >>> 您原来的xAxis配置(完全不变) <<<
      xAxis: [
        {
          type: "category",
          data: xAxisData.value, // 您的时间数据
          axisLabel: { color: "#fff", rotate: 0 },
        },
      ],
 
      // >>> 修改的Y轴配置(动态范围) <<<
      yAxis: [
        {
          // 左侧Y轴(降雨)
          type: "value",
          name: "单位:mm",
          min: 0,
          ...getDynamicYAxis(data1.value), // 动态范围
          axisLabel: { color: "#fff" },
          splitLine: { show: false },
        },
        {
          // 右侧Y轴(累计)
          type: "value",
          name: "单位:mm",
          min: 0,
          ...getDynamicYAxis(data2.value), // 动态范围
          axisLabel: { color: "#fff" },
          splitLine: { show: true },
        },
      ],
 
      // >>> 保持您原有的series配置 <<<
      series: [
        {
          name: "降雨数据",
          type: "bar",
          data: data1.value,
          itemStyle: { color: "#3268fe" },
        },
        {
          name: "累计雨量",
          type: "line",
          yAxisIndex: 1,
          data: data2.value,
          lineStyle: { color: "#ffb637" },
        },
      ],
    };
    myChart1.setOption(option, true);
  };
 
  // 7. 数据更新(保持您的时间逻辑)
  const updateData = () => {
    if (dataIndex.value < rainfallData.value.length) {
      const item = rainfallData.value[dataIndex.value];
      data1.value.push(item.value);
      data2.value.push(item.total);
      xAxisData.value.push(syncTimeWithTimeline()); // 您的时间生成方法
      dataIndex.value++;
      updateChart(); // 动态调整Y轴
    } else {
      stopUpdating();
    }
  };
 
  // 8. 控制方法(完全不变)
  const startUpdating = (interval = 1000) => {
    if (!updateInterval) {
      updateInterval = setInterval(updateData, interval);
    }
  };
 
  const stopUpdating = () => {
    if (updateInterval) {
      clearInterval(updateInterval);
      updateInterval = null;
    }
  };
 
  const resetLoading = () => {
    stopUpdating();
    dataIndex.value = 0;
    data1.value = [];
    data2.value = [];
    xAxisData.value = ["00:00"];
    if (rainfallData.value.length) {
      data1.value = [rainfallData.value[0].value];
      data2.value = [rainfallData.value[0].total];
    }
    updateChart();
  };
 
  // 9. 初始化
  loadJsonData();
 
  return {
    myChart1,
    startUpdating,
    stopUpdating,
    resetLoading,
  };
};
 
const setEcharts2 = () => {
  const chartDom = document.getElementById("echarts2");
  myChart2 = echarts.init(chartDom);
 
  let jsonData = ref([]);
  let data1 = ref([]);
  let data2 = ref([]);
  let xAxisData = ref([]);
  let updateInterval = null;
  let dataIndex = ref(0);
 
  // 加载 JSON 数据(完全保持原有逻辑)
  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;
        xAxisData.value = ["00:00"] || syncTimeWithTimeline(); // 保持您的时间初始化
        
        if (jsonData.value.length > 0) {
          data1.value.push(jsonData.value[0].value);
          data2.value.push(jsonData.value[0].total);
        }
        updateChart(); // 初始化时自动计算Y轴范围
      }
    } catch (error) {
      console.error("数据加载失败:", error);
    }
  };
 
  // 动态计算Y轴参数(新增核心功能)
  const calculateDynamicYAxis = (dataArray) => {
    const currentMax = Math.max(...dataArray, 1); // 确保最小显示范围
    const step = Math.ceil(currentMax / 3);
    return {
      max: step * 3,
      interval: step
    };
  };
 
  // 更新图表配置(仅修改Y轴部分)
  const updateChart = () => {
    const option = {
      // >>> 保持您原有的所有配置 <<<
      animation: false,
      tooltip: {
        trigger: "axis",
        axisPointer: { type: "cross" }
      },
      grid: {
        left: "1%",
        right: "1%",
        bottom: "1%",
        containLabel: true
      },
      legend: {
        data: ["实时流量", "累计流量"],
        textStyle: { color: "#fff" },
        right: "10px"
      },
      
      // >>> 您原来的xAxis配置(完全不变) <<<
      xAxis: [{
        type: "category",
        data: xAxisData.value,
        axisLabel: { color: "#fff", rotate: 0 }
      }],
      
      // >>> 修改的Y轴配置(动态范围) <<<
      yAxis: [
        { // 左侧Y轴(实时流量)
          type: "value",
          name: "单位:m³/min",
          min: 0,
          ...calculateDynamicYAxis(data1.value),
          axisLabel: { 
            formatter: "{value}",
            color: "#fff",
            align: "right"
          },
          nameTextStyle: {
            padding: [0, 0, 0, 20],
            color: "#fff"
          },
          splitLine: { show: false }
        },
        { // 右侧Y轴(累计流量)
          type: "value",
          name: "单位:m³",
          min: 0,
          ...calculateDynamicYAxis(data2.value),
          axisLabel: { 
            formatter: "{value}",
            color: "#fff",
            align: "left" 
          },
          nameTextStyle: {
            padding: [0, 10, 0, 0],
            color: "#fff"
          },
          splitLine: { show: true }
        }
      ],
      
      // >>> 保持您原有的series配置 <<<
      series: [
        {
          name: "实时流量",
          type: "bar",
          tooltip: { valueFormatter: value => value + " m³/min" },
          data: data1.value,
          itemStyle: { color: "blue" }
        },
        {
          name: "累计流量",
          type: "line",
          yAxisIndex: 1,
          tooltip: { valueFormatter: value => value + " m³" },
          data: data2.value,
          lineStyle: { color: "#ffb637" }
        }
      ]
    };
    myChart2.setOption(option, true); // 强制刷新
  };
 
  // 数据更新(保持您的时间逻辑)
  const updateData = () => {
    if (dataIndex.value < jsonData.value.length) {
      const newItem = jsonData.value[dataIndex.value];
      data1.value.push(newItem.value);
      data2.value.push(newItem.total);
      xAxisData.value.push(syncTimeWithTimeline()); // 您的时间生成方法
      dataIndex.value += 2; // 保持您的索引步长
      updateChart(); // 自动调整Y轴
    } else {
      stopUpdating();
    }
  };
 
  // 控制方法(完全不变)
  const startUpdating = (interval = 1000) => {
    if (!updateInterval) {
      updateInterval = setInterval(updateData, interval);
    }
  };
 
  const stopUpdating = () => {
    if (updateInterval) {
      clearInterval(updateInterval);
      updateInterval = null;
    }
  };
 
  const resetLoading = () => {
    stopUpdating();
    dataIndex.value = 0;
    data1.value = [];
    data2.value = [];
    xAxisData.value = ["00:00"];
    if (jsonData.value.length) {
      data1.value.push(jsonData.value[0].value);
      data2.value.push(jsonData.value[0].total);
    }
    updateChart();
  };
 
  // 初始化
  loadJsonData();
 
  return {
    myChart2,
    startUpdating,
    stopUpdating,
    resetLoading
  };
};
 
const handleResize = () => {
  const chartBox1 = document.getElementById("echarts1");
  const chartBox2 = document.getElementById("echarts2");
  if (chartBox1 && myChart1) chartssize(chartBox1, myChart1);
  if (chartBox2 && myChart2) chartssize(chartBox2, myChart2);
};
 
onMounted(() => {
  // 时间轴时间的变化
  EventBus.on("time-update", (time) => {
    nowTime.value = time;
  });
  chart1Data.value = setEcharts1();
  chart2Data.value = setEcharts2();
  myChart1 = chart1Data.value.myChart1;
  myChart2 = chart2Data.value.myChart2;
  window.addEventListener("resize", handleResize);
});
 
onBeforeUnmount(() => {
  nowTime.value = null;
  if (intervalId1) clearInterval(intervalId1);
  if (intervalId2) clearInterval(intervalId2);
  if (dataIntervalId) clearInterval(dataIntervalId);
  if (myChart1) myChart1.dispose();
  if (myChart2) myChart2.dispose();
  window.removeEventListener("resize", handleResize);
});
onUnmounted(() => {
  EventBus.off("reset-table"); // 移除事件监听
  EventBus.off("clear-echart");
  EventBus.off("time-update"); // 清理事件监听
});
</script>
 
<style lang="less" scoped>
@import url("../../assets/css/right.css");
@import url("../../assets/css/infobox.css");
 
:deep(.el-table td),
:deep(.el-table th) {
  padding: 0 !important;
}
 
:deep(.el-table .cell) {
  line-height: normal !important;
  padding: 0 1px !important;
}
 
.table-container {
  margin-left: 20px;
  max-height: 100%;
  overflow-y: auto;
}
 
.warp {
  height: calc(27% - 1px);
  width: 95%;
  position: absolute;
  margin: 0 auto;
  overflow: hidden;
  color: white;
 
  ul {
    list-style: none;
    padding: 0;
    margin: 0 auto;
 
    li,
    a {
      display: block;
      height: 30px;
      line-height: 30px;
      display: flex;
      justify-content: space-between;
      font-size: 15px;
    }
 
    .date {
      width: 85px;
    }
  }
}
</style>