Files
FullAutoWaterCheck/全自动水压检测仪/ChartManager.cs
2026-02-03 17:47:32 +08:00

197 lines
6.4 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.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)
{
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 = "温度 (°C)",
FontSize = 12,
Foreground = System.Windows.Media.Brushes.Red,
LabelFormatter = value => value.ToString("F1"),
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 tempSeries = _chart.Series[1] as LineSeries;
if (tempSeries != null)
{
tempSeries.Title = isHighTemperatureMode ? "箱体温度(高温)" : "箱体温度(常温)";
}
// 可以根据模式调整Y轴范围
if (_chart.AxisY.Count > 1)
{
_chart.AxisY[1].MaxValue = isHighTemperatureMode ? 200 : 100;
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
_chartHost?.Dispose();
_chart = null;
_pressureValues = null;
_temperatureValues = null;
_timeLabels = null;
}
}
}