From e52782f16e511c77d3f92cddfff81a8b0dbea4d7 Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Wed, 4 Feb 2026 15:38:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B2=E7=BA=BF=E5=9B=BE=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 全自动水压检测仪/ChartManager.cs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/全自动水压检测仪/ChartManager.cs b/全自动水压检测仪/ChartManager.cs index 2b4ce83..aa4bc4a 100644 --- a/全自动水压检测仪/ChartManager.cs +++ b/全自动水压检测仪/ChartManager.cs @@ -9,13 +9,15 @@ namespace 全自动水压检测仪 { /// /// 图表管理器 - 负责实时曲线图的创建和数据更新 + /// 第一Y轴:实时压力(地址3130) + /// 第二Y轴:压力设定值(地址2400,对应"压力设置(PSI)") /// public class ChartManager { private ElementHost _chartHost; private LiveCharts.Wpf.CartesianChart _chart; - private ChartValues _pressureValues; - private ChartValues _temperatureValues; + private ChartValues _pressureValues; // 实时压力值(地址3130) + private ChartValues _pressureSetValues; // 压力设定值(地址2400) private ChartValues _timeLabels; private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点 @@ -31,8 +33,8 @@ namespace 全自动水压检测仪 targetPanel.Text = null; // 初始化数据集合 - _pressureValues = new ChartValues(); - _temperatureValues = new ChartValues(); + _pressureValues = new ChartValues(); // 实时压力 + _pressureSetValues = new ChartValues(); // 压力设定值 _timeLabels = new ChartValues(); // 创建WPF图表控件 @@ -94,11 +96,11 @@ namespace 全自动水压检测仪 ScalesYAt = 0 }); - // 添加压力设定值曲线 + // 添加压力设定值曲线(来自地址2400,对应"压力设置(PSI)") _chart.Series.Add(new LineSeries { Title = "压力设定值", - Values = _temperatureValues, + Values = _pressureSetValues, Stroke = System.Windows.Media.Brushes.Red, Fill = System.Windows.Media.Brushes.Transparent, PointGeometry = DefaultGeometries.Diamond, @@ -123,11 +125,11 @@ namespace 全自动水压检测仪 /// 添加新的数据点 /// /// 实时压力值 - /// 压力设定值 + /// 压力设定值(来自"压力设置(PSI)",地址2400) /// 时间标签(可选,默认使用当前时间) - public void AddDataPoint(double pressure, double temperature, string time = null) + public void AddDataPoint(double pressure, double pressureSetValue, string time = null) { - if (_pressureValues == null || _temperatureValues == null || _timeLabels == null) + if (_pressureValues == null || _pressureSetValues == null || _timeLabels == null) return; // 使用当前时间作为默认标签 @@ -136,14 +138,14 @@ namespace 全自动水压检测仪 // 添加数据点 _pressureValues.Add(pressure); - _temperatureValues.Add(temperature); + _pressureSetValues.Add(pressureSetValue); _timeLabels.Add(time); // 限制数据点数量,保持图表流畅 if (_pressureValues.Count > MAX_DATA_POINTS) { _pressureValues.RemoveAt(0); - _temperatureValues.RemoveAt(0); + _pressureSetValues.RemoveAt(0); _timeLabels.RemoveAt(0); } } @@ -154,7 +156,7 @@ namespace 全自动水压检测仪 public void ClearData() { _pressureValues?.Clear(); - _temperatureValues?.Clear(); + _pressureSetValues?.Clear(); _timeLabels?.Clear(); } @@ -189,7 +191,7 @@ namespace 全自动水压检测仪 _chartHost?.Dispose(); _chart = null; _pressureValues = null; - _temperatureValues = null; + _pressureSetValues = null; _timeLabels = null; } } From 719ad8838892179e7ead2d3fceed794baf2cdb64 Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Wed, 4 Feb 2026 17:01:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=9B=B2=E7=BA=BF?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 全自动水压检测仪/ChartManager.cs | 251 ++++++++++++++--------- 全自动水压检测仪/packages.config | 7 +- 全自动水压检测仪/全自动水压检测仪.csproj | 26 ++- 3 files changed, 175 insertions(+), 109 deletions(-) diff --git a/全自动水压检测仪/ChartManager.cs b/全自动水压检测仪/ChartManager.cs index aa4bc4a..c3fd163 100644 --- a/全自动水压检测仪/ChartManager.cs +++ b/全自动水压检测仪/ChartManager.cs @@ -1,9 +1,11 @@ -using LiveCharts; -using LiveCharts.Wpf; +using OxyPlot; +using OxyPlot.Axes; +using OxyPlot.Series; +using OxyPlot.WindowsForms; using System; +using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; -using System.Windows.Forms.Integration; namespace 全自动水压检测仪 { @@ -14,12 +16,14 @@ namespace 全自动水压检测仪 /// public class ChartManager { - private ElementHost _chartHost; - private LiveCharts.Wpf.CartesianChart _chart; - private ChartValues _pressureValues; // 实时压力值(地址3130) - private ChartValues _pressureSetValues; // 压力设定值(地址2400) - private ChartValues _timeLabels; + private PlotView _plotView; + private PlotModel _plotModel; + private LineSeries _pressureSeries; // 实时压力值(地址3130) + private LineSeries _pressureSetSeries; // 压力设定值(地址2400) + private List _pressureData; + private List _pressureSetData; private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点 + private int _dataPointIndex = 0; /// /// 初始化图表并添加到指定面板 @@ -33,92 +37,112 @@ namespace 全自动水压检测仪 targetPanel.Text = null; // 初始化数据集合 - _pressureValues = new ChartValues(); // 实时压力 - _pressureSetValues = new ChartValues(); // 压力设定值 - _timeLabels = new ChartValues(); + _pressureData = new List(); + _pressureSetData = new List(); - // 创建WPF图表控件 - _chart = new LiveCharts.Wpf.CartesianChart + // 创建PlotModel + _plotModel = new PlotModel { - Background = System.Windows.Media.Brushes.White, - DisableAnimations = false, - Hoverable = true, - DataTooltip = null, - LegendLocation = LegendLocation.Top + Title = "", + Background = OxyColors.White, + PlotAreaBorderColor = OxyColors.Gray }; - - // 配置X轴(时间轴) - _chart.AxisX.Add(new Axis + + // 配置图例 + _plotModel.Legends.Add(new OxyPlot.Legends.Legend { + LegendPosition = OxyPlot.Legends.LegendPosition.TopCenter, + LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal + }); + + // 配置X轴(时间轴 - 使用数据点索引) + var xAxis = new LinearAxis + { + Position = AxisPosition.Bottom, Title = "时间", - Labels = _timeLabels, - FontSize = 12, - Foreground = System.Windows.Media.Brushes.Black, - Separator = new LiveCharts.Wpf.Separator - { - Step = 5, - IsEnabled = true - } - }); + 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 + }; + _plotModel.Axes.Add(xAxis); - // 配置Y轴(压力) - _chart.AxisY.Add(new Axis + // 配置左Y轴(实时压力) + var yAxisLeft = new LinearAxis { - Title = "压力 (MPa)", - FontSize = 12, - Foreground = System.Windows.Media.Brushes.Blue, - LabelFormatter = value => value.ToString("F2"), - MinValue = 0 - }); + Position = AxisPosition.Left, + Title = "压力 (PSI)", + TitleFontSize = 12, + FontSize = 10, + TitleColor = OxyColors.Blue, + TextColor = OxyColors.Blue, + MajorGridlineStyle = LineStyle.Solid, + MajorGridlineColor = OxyColor.FromRgb(230, 230, 230), + MinorGridlineStyle = LineStyle.Dot, + MinorGridlineColor = OxyColor.FromRgb(240, 240, 240), + StringFormat = "F2", + Minimum = 0, + Key = "LeftAxis" + }; + _plotModel.Axes.Add(yAxisLeft); - // 配置第二Y轴(压力设定值) - _chart.AxisY.Add(new Axis + // 配置右Y轴(压力设定值) + var yAxisRight = new LinearAxis { - Title = "压力设定值 (MPa)", - FontSize = 12, - Foreground = System.Windows.Media.Brushes.Red, - LabelFormatter = value => value.ToString("F2"), - Position = AxisPosition.RightTop, - MinValue = 0 - }); + Position = AxisPosition.Right, + Title = "压力设定值 (PSI)", + TitleFontSize = 12, + FontSize = 10, + TitleColor = OxyColors.Red, + TextColor = OxyColors.Red, + MajorGridlineStyle = LineStyle.None, + StringFormat = "F2", + Minimum = 0, + Key = "RightAxis" + }; + _plotModel.Axes.Add(yAxisRight); - // 添加压力曲线 - _chart.Series.Add(new LineSeries + // 添加实时压力曲线 + _pressureSeries = new LineSeries { Title = "实时压力", - Values = _pressureValues, - Stroke = System.Windows.Media.Brushes.Blue, - Fill = System.Windows.Media.Brushes.Transparent, - PointGeometry = DefaultGeometries.Circle, - PointGeometrySize = 5, + Color = OxyColors.Blue, StrokeThickness = 2, - LineSmoothness = 0.3, - ScalesYAt = 0 - }); + MarkerType = MarkerType.Circle, + MarkerSize = 4, + MarkerFill = OxyColors.Blue, + YAxisKey = "LeftAxis" + }; + _plotModel.Series.Add(_pressureSeries); - // 添加压力设定值曲线(来自地址2400,对应"压力设置(PSI)") - _chart.Series.Add(new LineSeries + // 添加压力设定值曲线 + _pressureSetSeries = new LineSeries { Title = "压力设定值", - Values = _pressureSetValues, - Stroke = System.Windows.Media.Brushes.Red, - Fill = System.Windows.Media.Brushes.Transparent, - PointGeometry = DefaultGeometries.Diamond, - PointGeometrySize = 5, + Color = OxyColors.Red, StrokeThickness = 2, - LineSmoothness = 0.3, - ScalesYAt = 1 - }); + MarkerType = MarkerType.Diamond, + MarkerSize = 4, + MarkerFill = OxyColors.Red, + YAxisKey = "RightAxis" + }; + _plotModel.Series.Add(_pressureSetSeries); - // 创建ElementHost以承载WPF控件 - _chartHost = new ElementHost + // 创建PlotView控件 + _plotView = new PlotView { + Model = _plotModel, Dock = DockStyle.Fill, - Child = _chart + BackColor = System.Drawing.Color.White }; // 添加到目标面板 - targetPanel.Controls.Add(_chartHost); + targetPanel.Controls.Add(_plotView); } /// @@ -129,25 +153,38 @@ namespace 全自动水压检测仪 /// 时间标签(可选,默认使用当前时间) public void AddDataPoint(double pressure, double pressureSetValue, string time = null) { - if (_pressureValues == null || _pressureSetValues == null || _timeLabels == null) + if (_pressureSeries == null || _pressureSetSeries == null || _plotModel == null) return; - // 使用当前时间作为默认标签 - if (string.IsNullOrEmpty(time)) - time = DateTime.Now.ToString("HH:mm:ss"); - - // 添加数据点 - _pressureValues.Add(pressure); - _pressureSetValues.Add(pressureSetValue); - _timeLabels.Add(time); + // 添加数据点(使用索引作为X轴值) + _pressureData.Add(new DataPoint(_dataPointIndex, pressure)); + _pressureSetData.Add(new DataPoint(_dataPointIndex, pressureSetValue)); + _dataPointIndex++; // 限制数据点数量,保持图表流畅 - if (_pressureValues.Count > MAX_DATA_POINTS) + if (_pressureData.Count > MAX_DATA_POINTS) { - _pressureValues.RemoveAt(0); - _pressureSetValues.RemoveAt(0); - _timeLabels.RemoveAt(0); + _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; } + + // 更新系列数据 + _pressureSeries.Points.Clear(); + _pressureSeries.Points.AddRange(_pressureData); + + _pressureSetSeries.Points.Clear(); + _pressureSetSeries.Points.AddRange(_pressureSetData); + + // 刷新图表 + _plotModel.InvalidatePlot(true); } /// @@ -155,9 +192,21 @@ namespace 全自动水压检测仪 /// public void ClearData() { - _pressureValues?.Clear(); - _pressureSetValues?.Clear(); - _timeLabels?.Clear(); + _pressureData?.Clear(); + _pressureSetData?.Clear(); + _dataPointIndex = 0; + + if (_pressureSeries != null) + { + _pressureSeries.Points.Clear(); + } + + if (_pressureSetSeries != null) + { + _pressureSetSeries.Points.Clear(); + } + + _plotModel?.InvalidatePlot(true); } /// @@ -166,21 +215,26 @@ namespace 全自动水压检测仪 /// 是否为高温模式 public void UpdateChartMode(bool isHighTemperatureMode) { - if (_chart == null || _chart.Series.Count < 2) + if (_plotModel == null || _plotModel.Series.Count < 2) return; // 压力设定值曲线标题保持不变 - var pressureSetSeries = _chart.Series[1] as LineSeries; - if (pressureSetSeries != null) + if (_pressureSetSeries != null) { - pressureSetSeries.Title = "压力设定值"; + _pressureSetSeries.Title = "压力设定值"; } // 可以根据模式调整Y轴范围(压力设定值范围) - if (_chart.AxisY.Count > 1) + foreach (var axis in _plotModel.Axes) { - _chart.AxisY[1].MaxValue = 10; // 压力设定值通常不超过10MPa + if (axis.Key == "RightAxis") + { + axis.Maximum = 10; // 压力设定值通常不超过10MPa + break; + } } + + _plotModel.InvalidatePlot(true); } /// @@ -188,11 +242,12 @@ namespace 全自动水压检测仪 /// public void Dispose() { - _chartHost?.Dispose(); - _chart = null; - _pressureValues = null; - _pressureSetValues = null; - _timeLabels = null; + _plotView?.Dispose(); + _plotModel = null; + _pressureSeries = null; + _pressureSetSeries = null; + _pressureData = null; + _pressureSetData = null; } } } diff --git a/全自动水压检测仪/packages.config b/全自动水压检测仪/packages.config index 2c0a813..26df6df 100644 --- a/全自动水压检测仪/packages.config +++ b/全自动水压检测仪/packages.config @@ -7,11 +7,14 @@ - - + + + + + diff --git a/全自动水压检测仪/全自动水压检测仪.csproj b/全自动水压检测仪/全自动水压检测仪.csproj index 7b5d820..4917aed 100644 --- a/全自动水压检测仪/全自动水压检测仪.csproj +++ b/全自动水压检测仪/全自动水压检测仪.csproj @@ -12,6 +12,8 @@ 512 true true + + AnyCPU @@ -59,11 +61,15 @@ ..\packages\K4os.Hash.xxHash.1.0.8\lib\net462\K4os.Hash.xxHash.dll - - ..\packages\LiveCharts.0.9.7\lib\net45\LiveCharts.dll + + ..\packages\OxyPlot.Core.2.2.0\lib\net462\OxyPlot.dll + True + True - - ..\packages\LiveCharts.Wpf.0.9.7\lib\net45\LiveCharts.Wpf.dll + + ..\packages\OxyPlot.WindowsForms.2.2.0\lib\net462\OxyPlot.WindowsForms.dll + True + False ..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll @@ -74,8 +80,6 @@ ..\packages\NModbus4.2.1.0\lib\net40\NModbus4.dll - - ..\packages\SunnyUI.3.9.1\lib\net472\SunnyUI.dll @@ -121,9 +125,6 @@ - - - ..\packages\ZstdSharp.Port.0.8.6\lib\net462\ZstdSharp.dll @@ -284,4 +285,11 @@ + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + \ No newline at end of file From f474e7b38391983899c10d7509ccfc3f52e2cc4b Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Wed, 4 Feb 2026 17:18:47 +0800 Subject: [PATCH 3/3] =?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) {