更新
This commit is contained in:
@@ -5,6 +5,7 @@ using OxyPlot.WindowsForms;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace 全自动水压检测仪
|
namespace 全自动水压检测仪
|
||||||
@@ -23,7 +24,6 @@ namespace 全自动水压检测仪
|
|||||||
private List<DataPoint> _pressureData;
|
private List<DataPoint> _pressureData;
|
||||||
private List<DataPoint> _pressureSetData;
|
private List<DataPoint> _pressureSetData;
|
||||||
private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点
|
private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点
|
||||||
private int _dataPointIndex = 0;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化图表并添加到指定面板
|
/// 初始化图表并添加到指定面板
|
||||||
@@ -55,20 +55,28 @@ namespace 全自动水压检测仪
|
|||||||
LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal
|
LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal
|
||||||
});
|
});
|
||||||
|
|
||||||
// 配置X轴(时间轴 - 使用数据点索引)
|
// 配置X轴(时间轴 - 使用测试时间总秒数)
|
||||||
var xAxis = new LinearAxis
|
var xAxis = new LinearAxis
|
||||||
{
|
{
|
||||||
Position = AxisPosition.Bottom,
|
Position = AxisPosition.Bottom,
|
||||||
Title = "时间",
|
Title = "测试时间",
|
||||||
TitleFontSize = 12,
|
TitleFontSize = 12,
|
||||||
FontSize = 10,
|
FontSize = 10,
|
||||||
MajorGridlineStyle = LineStyle.Solid,
|
MajorGridlineStyle = LineStyle.Solid,
|
||||||
MajorGridlineColor = OxyColor.FromRgb(230, 230, 230),
|
MajorGridlineColor = OxyColor.FromRgb(230, 230, 230),
|
||||||
MinorGridlineStyle = LineStyle.Dot,
|
MinorGridlineStyle = LineStyle.Dot,
|
||||||
MinorGridlineColor = OxyColor.FromRgb(240, 240, 240),
|
MinorGridlineColor = OxyColor.FromRgb(240, 240, 240),
|
||||||
StringFormat = "0",
|
|
||||||
Minimum = 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);
|
_plotModel.Axes.Add(xAxis);
|
||||||
|
|
||||||
@@ -150,30 +158,21 @@ namespace 全自动水压检测仪
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pressure">实时压力值</param>
|
/// <param name="pressure">实时压力值</param>
|
||||||
/// <param name="pressureSetValue">压力设定值(来自"压力设置(PSI)",地址2400)</param>
|
/// <param name="pressureSetValue">压力设定值(来自"压力设置(PSI)",地址2400)</param>
|
||||||
/// <param name="time">时间标签(可选,默认使用当前时间)</param>
|
/// <param name="totalSeconds">测试时间(总秒数)</param>
|
||||||
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)
|
if (_pressureSeries == null || _pressureSetSeries == null || _plotModel == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 添加数据点(使用索引作为X轴值)
|
// 添加数据点(使用测试时间总秒数作为X轴值)
|
||||||
_pressureData.Add(new DataPoint(_dataPointIndex, pressure));
|
_pressureData.Add(new DataPoint(totalSeconds, pressure));
|
||||||
_pressureSetData.Add(new DataPoint(_dataPointIndex, pressureSetValue));
|
_pressureSetData.Add(new DataPoint(totalSeconds, pressureSetValue));
|
||||||
_dataPointIndex++;
|
|
||||||
|
|
||||||
// 限制数据点数量,保持图表流畅
|
// 限制数据点数量,保持图表流畅
|
||||||
if (_pressureData.Count > MAX_DATA_POINTS)
|
if (_pressureData.Count > MAX_DATA_POINTS)
|
||||||
{
|
{
|
||||||
_pressureData.RemoveAt(0);
|
_pressureData.RemoveAt(0);
|
||||||
_pressureSetData.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.Clear();
|
||||||
_pressureSetSeries.Points.AddRange(_pressureSetData);
|
_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);
|
_plotModel.InvalidatePlot(true);
|
||||||
}
|
}
|
||||||
@@ -194,7 +206,6 @@ namespace 全自动水压检测仪
|
|||||||
{
|
{
|
||||||
_pressureData?.Clear();
|
_pressureData?.Clear();
|
||||||
_pressureSetData?.Clear();
|
_pressureSetData?.Clear();
|
||||||
_dataPointIndex = 0;
|
|
||||||
|
|
||||||
if (_pressureSeries != null)
|
if (_pressureSeries != null)
|
||||||
{
|
{
|
||||||
@@ -206,6 +217,14 @@ namespace 全自动水压检测仪
|
|||||||
_pressureSetSeries.Points.Clear();
|
_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);
|
_plotModel?.InvalidatePlot(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -516,13 +516,13 @@ namespace 全自动水压检测仪
|
|||||||
var pressure = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]);
|
var pressure = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]);
|
||||||
var pressureSetValue = c.UshortToFloat(modbusData.Real12[1], modbusData.Real12[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 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 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;
|
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)
|
catch (Exception chartEx)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user