This commit is contained in:
GukSang.Jin
2026-02-05 16:13:03 +08:00
parent 63fa7d3972
commit e8befb8883

View File

@@ -12,8 +12,7 @@ namespace 全自动水压检测仪
{ {
/// <summary> /// <summary>
/// 图表管理器 - 负责实时曲线图的创建和数据更新 /// 图表管理器 - 负责实时曲线图的创建和数据更新
/// 一Y轴实时压力地址3130 /// 使用统一Y轴显示实时压力和压力设定值,便于数据对比
/// 第二Y轴压力设定值地址2400对应"压力设置(PSI)"
/// </summary> /// </summary>
public class ChartManager public class ChartManager
{ {
@@ -26,6 +25,7 @@ namespace 全自动水压检测仪
private const int MAX_DATA_POINTS = 3600; // 最多显示3600个数据点1小时 private const int MAX_DATA_POINTS = 3600; // 最多显示3600个数据点1小时
private double _initialMinX = 0; // 记录初始最小X值 private double _initialMinX = 0; // 记录初始最小X值
private bool _isUserZooming = false; // 用户是否正在缩放 private bool _isUserZooming = false; // 用户是否正在缩放
private double _maxPressureValue = 0; // 记录最大压力值用于动态调整Y轴
/// <summary> /// <summary>
/// 初始化图表并添加到指定面板 /// 初始化图表并添加到指定面板
@@ -48,14 +48,26 @@ namespace 全自动水压检测仪
{ {
Title = "", Title = "",
Background = OxyColors.White, Background = OxyColors.White,
PlotAreaBorderColor = OxyColors.Gray PlotAreaBorderColor = OxyColor.FromRgb(180, 180, 180),
PlotAreaBorderThickness = new OxyThickness(1.5),
Padding = new OxyThickness(10, 10, 20, 10)
}; };
// 配置图例 // 配置图例 - 放在图表外面顶部
_plotModel.Legends.Add(new OxyPlot.Legends.Legend _plotModel.Legends.Add(new OxyPlot.Legends.Legend
{ {
LegendPosition = OxyPlot.Legends.LegendPosition.TopCenter, LegendPosition = OxyPlot.Legends.LegendPosition.TopCenter,
LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal LegendOrientation = OxyPlot.Legends.LegendOrientation.Horizontal,
LegendPlacement = OxyPlot.Legends.LegendPlacement.Outside,
LegendBackground = OxyColor.FromAColor(250, OxyColors.White),
LegendBorder = OxyColor.FromRgb(200, 200, 200),
LegendBorderThickness = 1,
LegendPadding = 10,
LegendMargin = 8,
LegendSymbolLength = 30,
LegendSymbolMargin = 10,
LegendFontSize = 12,
LegendItemSpacing = 20
}); });
// 配置X轴时间轴 - 使用测试时间总秒数) // 配置X轴时间轴 - 使用测试时间总秒数)
@@ -63,15 +75,17 @@ namespace 全自动水压检测仪
{ {
Position = AxisPosition.Bottom, Position = AxisPosition.Bottom,
Title = "测试时间", Title = "测试时间",
TitleFontSize = 12, TitleFontSize = 13,
FontSize = 10, TitleFontWeight = OxyPlot.FontWeights.Bold,
FontSize = 11,
MajorGridlineStyle = LineStyle.Solid, MajorGridlineStyle = LineStyle.Solid,
MajorGridlineColor = OxyColor.FromRgb(230, 230, 230), MajorGridlineColor = OxyColor.FromRgb(220, 220, 220),
MajorGridlineThickness = 1,
MinorGridlineStyle = LineStyle.Dot, MinorGridlineStyle = LineStyle.Dot,
MinorGridlineColor = OxyColor.FromRgb(240, 240, 240), MinorGridlineColor = OxyColor.FromRgb(240, 240, 240),
MinorGridlineThickness = 0.5,
Minimum = 0, Minimum = 0,
Maximum = 60, Maximum = 60,
// 允许用户缩放和平移
IsPanEnabled = true, IsPanEnabled = true,
IsZoomEnabled = true, IsZoomEnabled = true,
// 格式化X轴标签为HH:MM:SS格式 // 格式化X轴标签为HH:MM:SS格式
@@ -87,68 +101,58 @@ namespace 全自动水压检测仪
_plotModel.Axes.Add(xAxis); _plotModel.Axes.Add(xAxis);
// 配置Y轴实时压力 // 配置Y轴统一显示实时压力和压力设定值
var yAxisLeft = new LinearAxis var yAxis = new LinearAxis
{ {
Position = AxisPosition.Left, Position = AxisPosition.Left,
Title = "压力 (PSI)", Title = "压力 (PSI)",
TitleFontSize = 12, TitleFontSize = 13,
FontSize = 10, TitleFontWeight = OxyPlot.FontWeights.Bold,
TitleColor = OxyColors.Blue, FontSize = 11,
TextColor = OxyColors.Blue,
MajorGridlineStyle = LineStyle.Solid, MajorGridlineStyle = LineStyle.Solid,
MajorGridlineColor = OxyColor.FromRgb(230, 230, 230), MajorGridlineColor = OxyColor.FromRgb(220, 220, 220),
MajorGridlineThickness = 1,
MinorGridlineStyle = LineStyle.Dot, MinorGridlineStyle = LineStyle.Dot,
MinorGridlineColor = OxyColor.FromRgb(240, 240, 240), MinorGridlineColor = OxyColor.FromRgb(240, 240, 240),
MinorGridlineThickness = 0.5,
StringFormat = "F2", StringFormat = "F2",
Minimum = 0, Minimum = 0,
Key = "LeftAxis", Maximum = 10, // 初始最大值,会动态调整
Key = "YAxis",
IsPanEnabled = true, IsPanEnabled = true,
IsZoomEnabled = true IsZoomEnabled = true
}; };
_plotModel.Axes.Add(yAxisLeft); _plotModel.Axes.Add(yAxis);
// 配置右Y轴压力设定值 // 添加实时压力曲线 - 使用蓝色实线
var yAxisRight = new LinearAxis
{
Position = AxisPosition.Right,
Title = "压力设定值 (PSI)",
TitleFontSize = 12,
FontSize = 10,
TitleColor = OxyColors.Red,
TextColor = OxyColors.Red,
MajorGridlineStyle = LineStyle.None,
StringFormat = "F2",
Minimum = 0,
Key = "RightAxis",
IsPanEnabled = true,
IsZoomEnabled = true
};
_plotModel.Axes.Add(yAxisRight);
// 添加实时压力曲线
_pressureSeries = new LineSeries _pressureSeries = new LineSeries
{ {
Title = "实时压力", Title = "实时压力",
Color = OxyColors.Blue, Color = OxyColor.FromRgb(30, 144, 255), // 道奇蓝
StrokeThickness = 2, StrokeThickness = 2.5,
MarkerType = MarkerType.Circle, MarkerType = MarkerType.Circle,
MarkerSize = 4, MarkerSize = 5,
MarkerFill = OxyColors.Blue, MarkerFill = OxyColor.FromRgb(30, 144, 255),
YAxisKey = "LeftAxis" MarkerStroke = OxyColors.White,
MarkerStrokeThickness = 1.5,
LineStyle = LineStyle.Solid,
YAxisKey = "YAxis"
}; };
_plotModel.Series.Add(_pressureSeries); _plotModel.Series.Add(_pressureSeries);
// 添加压力设定值曲线 // 添加压力设定值曲线 - 使用红色虚线
_pressureSetSeries = new LineSeries _pressureSetSeries = new LineSeries
{ {
Title = "压力设定值", Title = "压力设定值",
Color = OxyColors.Red, Color = OxyColor.FromRgb(220, 20, 60), // 深红色
StrokeThickness = 2, StrokeThickness = 2.5,
MarkerType = MarkerType.Diamond, MarkerType = MarkerType.Square,
MarkerSize = 4, MarkerSize = 5,
MarkerFill = OxyColors.Red, MarkerFill = OxyColor.FromRgb(220, 20, 60),
YAxisKey = "RightAxis" MarkerStroke = OxyColors.White,
MarkerStrokeThickness = 1.5,
LineStyle = LineStyle.Dash,
YAxisKey = "YAxis"
}; };
_plotModel.Series.Add(_pressureSetSeries); _plotModel.Series.Add(_pressureSetSeries);
@@ -163,13 +167,11 @@ namespace 全自动水压检测仪
// 监听鼠标事件,检测用户交互(缩放和平移) // 监听鼠标事件,检测用户交互(缩放和平移)
_plotView.MouseDown += (s, e) => _plotView.MouseDown += (s, e) =>
{ {
// 用户开始交互时标记为缩放状态
_isUserZooming = true; _isUserZooming = true;
}; };
_plotView.MouseWheel += (s, e) => _plotView.MouseWheel += (s, e) =>
{ {
// 鼠标滚轮缩放时标记
_isUserZooming = true; _isUserZooming = true;
}; };
@@ -178,7 +180,6 @@ namespace 全自动水压检测仪
{ {
if (e.Button == System.Windows.Forms.MouseButtons.Left) if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ {
// 双击左键重置缩放
ResetZoom(); ResetZoom();
} }
}; };
@@ -208,6 +209,13 @@ namespace 全自动水压检测仪
_initialMinX = totalSeconds; _initialMinX = totalSeconds;
} }
// 更新最大压力值用于动态调整Y轴
double currentMaxPressure = Math.Max(pressure, pressureSetValue);
if (currentMaxPressure > _maxPressureValue)
{
_maxPressureValue = currentMaxPressure;
}
// 限制数据点数量保持图表流畅保留最近3600个点 // 限制数据点数量保持图表流畅保留最近3600个点
if (_pressureData.Count > MAX_DATA_POINTS) if (_pressureData.Count > MAX_DATA_POINTS)
{ {
@@ -227,22 +235,43 @@ namespace 全自动水压检测仪
_pressureSetSeries.Points.Clear(); _pressureSetSeries.Points.Clear();
_pressureSetSeries.Points.AddRange(_pressureSetData); _pressureSetSeries.Points.AddRange(_pressureSetData);
// 只有在非用户缩放状态下才自动调整X轴范围 // 只有在非用户缩放状态下才自动调整轴范围
if (!_isUserZooming && _pressureData.Count > 0) if (!_isUserZooming)
{ {
var xAxis = _plotModel.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom); // 调整X轴范围
if (xAxis != null) if (_pressureData.Count > 0)
{ {
// 始终从初始时间开始显示 var xAxis = _plotModel.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
double minX = _initialMinX; if (xAxis != null)
double maxX = _pressureData.Max(p => p.X); {
// 始终从初始时间开始显示
double minX = _initialMinX;
double maxX = _pressureData.Max(p => p.X);
xAxis.Minimum = minX;
xAxis.Maximum = maxX + 5; // 留一点余量
xAxis.Reset();
}
}
// 动态调整Y轴范围 - 比实际最大值大20%,确保曲线美观
var yAxis = _plotModel.Axes.FirstOrDefault(a => a.Key == "YAxis");
if (yAxis != null && _maxPressureValue > 0)
{
// Y轴最大值设置为实际最大值的1.2倍最小为10
double yMax = Math.Max(_maxPressureValue * 1.2, 10);
// 设置X轴范围确保从起始时间开始 // 向上取整到合适的刻度
xAxis.Minimum = minX; if (yMax < 10)
xAxis.Maximum = maxX + 5; // 留一点余量 yMax = Math.Ceiling(yMax);
else if (yMax < 100)
yMax = Math.Ceiling(yMax / 5) * 5;
else
yMax = Math.Ceiling(yMax / 10) * 10;
// 重置轴以应用新的范围 yAxis.Minimum = 0;
xAxis.Reset(); yAxis.Maximum = yMax;
yAxis.Reset();
} }
} }
@@ -277,9 +306,19 @@ namespace 全自动水压检测仪
xAxis.Reset(); xAxis.Reset();
} }
// 重置初始最小X值和用户缩放状态 // 重置Y轴范围
var yAxis = _plotModel?.Axes.FirstOrDefault(a => a.Key == "YAxis");
if (yAxis != null)
{
yAxis.Minimum = 0;
yAxis.Maximum = 10;
yAxis.Reset();
}
// 重置初始最小X值、用户缩放状态和最大压力值
_initialMinX = 0; _initialMinX = 0;
_isUserZooming = false; _isUserZooming = false;
_maxPressureValue = 0;
_plotModel?.InvalidatePlot(true); _plotModel?.InvalidatePlot(true);
} }
@@ -293,6 +332,7 @@ namespace 全自动水压检测仪
if (_pressureData.Count > 0) if (_pressureData.Count > 0)
{ {
// 重置X轴
var xAxis = _plotModel?.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom); var xAxis = _plotModel?.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
if (xAxis != null) if (xAxis != null)
{ {
@@ -302,6 +342,24 @@ namespace 全自动水压检测仪
xAxis.Maximum = maxX + 5; xAxis.Maximum = maxX + 5;
xAxis.Reset(); xAxis.Reset();
} }
// 重置Y轴
var yAxis = _plotModel?.Axes.FirstOrDefault(a => a.Key == "YAxis");
if (yAxis != null && _maxPressureValue > 0)
{
double yMax = Math.Max(_maxPressureValue * 1.2, 10);
if (yMax < 10)
yMax = Math.Ceiling(yMax);
else if (yMax < 100)
yMax = Math.Ceiling(yMax / 5) * 5;
else
yMax = Math.Ceiling(yMax / 10) * 10;
yAxis.Minimum = 0;
yAxis.Maximum = yMax;
yAxis.Reset();
}
} }
_plotModel?.InvalidatePlot(true); _plotModel?.InvalidatePlot(true);
@@ -316,22 +374,7 @@ namespace 全自动水压检测仪
if (_plotModel == null || _plotModel.Series.Count < 2) if (_plotModel == null || _plotModel.Series.Count < 2)
return; return;
// 压力设定值曲线标题保持不变 // 可以根据模式调整显示样式(如果需要)
if (_pressureSetSeries != null)
{
_pressureSetSeries.Title = "压力设定值";
}
// 可以根据模式调整Y轴范围压力设定值范围
foreach (var axis in _plotModel.Axes)
{
if (axis.Key == "RightAxis")
{
axis.Maximum = 10; // 压力设定值通常不超过10MPa
break;
}
}
_plotModel.InvalidatePlot(true); _plotModel.InvalidatePlot(true);
} }