This commit is contained in:
wxt
2026-02-05 16:13:23 +08:00

View File

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