From f474e7b38391983899c10d7509ccfc3f52e2cc4b Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Wed, 4 Feb 2026 17:18:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 全自动水压检测仪/ChartManager.cs | 59 +++++++++++++++-------- 全自动水压检测仪/NormalTemperatureMode.cs | 6 +-- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/全自动水压检测仪/ChartManager.cs b/全自动水压检测仪/ChartManager.cs index c3fd163..beab424 100644 --- a/全自动水压检测仪/ChartManager.cs +++ b/全自动水压检测仪/ChartManager.cs @@ -5,6 +5,7 @@ using OxyPlot.WindowsForms; using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Windows.Forms; namespace 全自动水压检测仪 @@ -23,7 +24,6 @@ namespace 全自动水压检测仪 private List _pressureData; private List _pressureSetData; private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点 - private int _dataPointIndex = 0; /// /// 初始化图表并添加到指定面板 @@ -55,20 +55,28 @@ namespace 全自动水压检测仪 LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal }); - // 配置X轴(时间轴 - 使用数据点索引) + // 配置X轴(时间轴 - 使用测试时间总秒数) var xAxis = new LinearAxis { Position = AxisPosition.Bottom, - Title = "时间", + Title = "测试时间", TitleFontSize = 12, FontSize = 10, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(230, 230, 230), MinorGridlineStyle = LineStyle.Dot, MinorGridlineColor = OxyColor.FromRgb(240, 240, 240), - StringFormat = "0", Minimum = 0, - Maximum = MAX_DATA_POINTS + Maximum = 60, + // 格式化X轴标签为HH:MM:SS格式 + LabelFormatter = value => + { + int totalSec = (int)value; + int h = totalSec / 3600; + int m = (totalSec % 3600) / 60; + int s = totalSec % 60; + return $"{h:D2}:{m:D2}:{s:D2}"; + } }; _plotModel.Axes.Add(xAxis); @@ -150,30 +158,21 @@ namespace 全自动水压检测仪 /// /// 实时压力值 /// 压力设定值(来自"压力设置(PSI)",地址2400) - /// 时间标签(可选,默认使用当前时间) - public void AddDataPoint(double pressure, double pressureSetValue, string time = null) + /// 测试时间(总秒数) + public void AddDataPoint(double pressure, double pressureSetValue, int totalSeconds) { if (_pressureSeries == null || _pressureSetSeries == null || _plotModel == null) return; - // 添加数据点(使用索引作为X轴值) - _pressureData.Add(new DataPoint(_dataPointIndex, pressure)); - _pressureSetData.Add(new DataPoint(_dataPointIndex, pressureSetValue)); - _dataPointIndex++; + // 添加数据点(使用测试时间总秒数作为X轴值) + _pressureData.Add(new DataPoint(totalSeconds, pressure)); + _pressureSetData.Add(new DataPoint(totalSeconds, pressureSetValue)); // 限制数据点数量,保持图表流畅 if (_pressureData.Count > MAX_DATA_POINTS) { _pressureData.RemoveAt(0); _pressureSetData.RemoveAt(0); - - // 重新索引数据点 - for (int i = 0; i < _pressureData.Count; i++) - { - _pressureData[i] = new DataPoint(i, _pressureData[i].Y); - _pressureSetData[i] = new DataPoint(i, _pressureSetData[i].Y); - } - _dataPointIndex = _pressureData.Count; } // 更新系列数据 @@ -183,6 +182,19 @@ namespace 全自动水压检测仪 _pressureSetSeries.Points.Clear(); _pressureSetSeries.Points.AddRange(_pressureSetData); + // 动态调整X轴范围 + if (_pressureData.Count > 0) + { + var xAxis = _plotModel.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom); + if (xAxis != null) + { + double minX = _pressureData.Min(p => p.X); + double maxX = _pressureData.Max(p => p.X); + xAxis.Minimum = minX; + xAxis.Maximum = maxX + 5; // 留一点余量 + } + } + // 刷新图表 _plotModel.InvalidatePlot(true); } @@ -194,7 +206,6 @@ namespace 全自动水压检测仪 { _pressureData?.Clear(); _pressureSetData?.Clear(); - _dataPointIndex = 0; if (_pressureSeries != null) { @@ -206,6 +217,14 @@ namespace 全自动水压检测仪 _pressureSetSeries.Points.Clear(); } + // 重置X轴范围 + var xAxis = _plotModel?.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom); + if (xAxis != null) + { + xAxis.Minimum = 0; + xAxis.Maximum = 60; + } + _plotModel?.InvalidatePlot(true); } diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index 9e4a6f2..c017215 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -516,13 +516,13 @@ namespace 全自动水压检测仪 var pressure = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]); var pressureSetValue = c.UshortToFloat(modbusData.Real12[1], modbusData.Real12[0]); - // 获取测试时间(从PLC读取的时分秒) + // 获取测试时间(从PLC读取的时分秒)并计算总秒数 int seconds = (modbusData.Real6 != null && modbusData.Real6.Length >= 1) ? modbusData.Real6[0] : 0; int minutes = (modbusData.Real7 != null && modbusData.Real7.Length >= 1) ? modbusData.Real7[0] : 0; int hours = (modbusData.Real8 != null && modbusData.Real8.Length >= 1) ? modbusData.Real8[0] : 0; - string testTime = $"{hours:D2}:{minutes:D2}:{seconds:D2}"; + int totalSeconds = hours * 3600 + minutes * 60 + seconds; - _chartManager?.AddDataPoint(pressure, pressureSetValue, testTime); + _chartManager?.AddDataPoint(pressure, pressureSetValue, totalSeconds); } catch (Exception chartEx) {