Files
FullAutoWaterCheck/全自动水压检测仪/ChartManager.cs
2026-02-04 17:19:49 +08:00

210 lines
6.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace
{
/// <summary>
/// 图表管理器 - 负责实时曲线图的创建和数据更新
/// </summary>
public class ChartManager
{
private ElementHost _chartHost;
private LiveCharts.Wpf.CartesianChart _chart;
private ChartValues<double> _pressureValues;
private ChartValues<double> _temperatureValues;
private ChartValues<string> _timeLabels;
private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点
/// <summary>
/// 初始化图表并添加到指定面板
/// </summary>
public void InitializeChart(Control targetPanel)
{
targetPanel.Invoke(new Action(() =>
{
if (targetPanel == null)
throw new ArgumentNullException(nameof(targetPanel));
// 清除占位文字
targetPanel.Text = null;
// 初始化数据集合
_pressureValues = new ChartValues<double>();
_temperatureValues = new ChartValues<double>();
_timeLabels = new ChartValues<string>();
// 创建WPF图表控件
_chart = new LiveCharts.Wpf.CartesianChart
{
Background = System.Windows.Media.Brushes.White,
DisableAnimations = false,
Hoverable = true,
DataTooltip = null,
LegendLocation = LegendLocation.Top
};
// 配置X轴时间轴
_chart.AxisX.Add(new Axis
{
Title = "时间",
Labels = _timeLabels,
FontSize = 12,
Foreground = System.Windows.Media.Brushes.Black,
Separator = new LiveCharts.Wpf.Separator
{
Step = 5,
IsEnabled = true
}
});
// 配置Y轴压力
_chart.AxisY.Add(new Axis
{
Title = "压力 (MPa)",
FontSize = 12,
Foreground = System.Windows.Media.Brushes.Blue,
LabelFormatter = value => value.ToString("F2"),
MinValue = 0
});
// 配置第二Y轴压力设定值
_chart.AxisY.Add(new Axis
{
Title = "压力设定值 (MPa)",
FontSize = 12,
Foreground = System.Windows.Media.Brushes.Red,
LabelFormatter = value => value.ToString("F2"),
Position = AxisPosition.RightTop,
MinValue = 0
});
// 添加压力曲线
_chart.Series.Add(new LineSeries
{
Title = "实时压力",
Values = _pressureValues,
Stroke = System.Windows.Media.Brushes.Blue,
Fill = System.Windows.Media.Brushes.Transparent,
PointGeometry = DefaultGeometries.Circle,
PointGeometrySize = 5,
StrokeThickness = 2,
LineSmoothness = 0.3,
ScalesYAt = 0
});
// 添加压力设定值曲线
_chart.Series.Add(new LineSeries
{
Title = "压力设定值",
Values = _temperatureValues,
Stroke = System.Windows.Media.Brushes.Red,
Fill = System.Windows.Media.Brushes.Transparent,
PointGeometry = DefaultGeometries.Diamond,
PointGeometrySize = 5,
StrokeThickness = 2,
LineSmoothness = 0.3,
ScalesYAt = 1
});
// 创建ElementHost以承载WPF控件
_chartHost = new ElementHost
{
Dock = DockStyle.Fill,
Child = _chart
};
// 添加到目标面板
targetPanel.Controls.Add(_chartHost);
}));
}
/// <summary>
/// 添加新的数据点
/// </summary>
/// <param name="pressure">实时压力值</param>
/// <param name="temperature">压力设定值</param>
/// <param name="time">时间标签(可选,默认使用当前时间)</param>
public void AddDataPoint(double pressure, double temperature, string time = null)
{
if (_pressureValues == null || _temperatureValues == null || _timeLabels == null)
return;
// 使用当前时间作为默认标签
if (string.IsNullOrEmpty(time))
time = DateTime.Now.ToString("HH:mm:ss");
// 添加数据点
_pressureValues.Add(pressure);
_temperatureValues.Add(temperature);
_timeLabels.Add(time);
// 限制数据点数量,保持图表流畅
if (_pressureValues.Count > MAX_DATA_POINTS)
{
_pressureValues.RemoveAt(0);
_temperatureValues.RemoveAt(0);
_timeLabels.RemoveAt(0);
}
}
/// <summary>
/// 清除所有数据
/// </summary>
public void ClearData()
{
_pressureValues?.Clear();
_temperatureValues?.Clear();
_timeLabels?.Clear();
}
/// <summary>
/// 更新图表配置(例如切换温度模式时)
/// </summary>
/// <param name="isHighTemperatureMode">是否为高温模式</param>
public void UpdateChartMode(bool isHighTemperatureMode)
{
if (_chart == null || _chart.Series.Count < 2)
return;
// 压力设定值曲线标题保持不变
var pressureSetSeries = _chart.Series[1] as LineSeries;
if (pressureSetSeries != null)
{
pressureSetSeries.Title = "压力设定值";
}
// 可以根据模式调整Y轴范围压力设定值范围
if (_chart.AxisY.Count > 1)
{
_chart.AxisY[1].MaxValue = 10; // 压力设定值通常不超过10MPa
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
_chartHost?.Dispose();
_chart = null;
_pressureValues = null;
_temperatureValues = null;
_timeLabels = null;
}
}
}