using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace 全自动水压检测仪
{
///
/// 图表管理器 - 负责实时曲线图的创建和数据更新
///
public class ChartManager
{
private ElementHost _chartHost;
private LiveCharts.Wpf.CartesianChart _chart;
private ChartValues _pressureValues;
private ChartValues _temperatureValues;
private ChartValues _timeLabels;
private const int MAX_DATA_POINTS = 60; // 最多显示60个数据点
///
/// 初始化图表并添加到指定面板
///
public void InitializeChart(Control targetPanel)
{
targetPanel.Invoke(new Action(() =>
{
if (targetPanel == null)
throw new ArgumentNullException(nameof(targetPanel));
// 清除占位文字
targetPanel.Text = null;
// 初始化数据集合
_pressureValues = new ChartValues();
_temperatureValues = new ChartValues();
_timeLabels = new ChartValues();
// 创建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);
}));
}
///
/// 添加新的数据点
///
/// 实时压力值
/// 压力设定值
/// 时间标签(可选,默认使用当前时间)
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);
}
}
///
/// 清除所有数据
///
public void ClearData()
{
_pressureValues?.Clear();
_temperatureValues?.Clear();
_timeLabels?.Clear();
}
///
/// 更新图表配置(例如切换温度模式时)
///
/// 是否为高温模式
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
}
}
///
/// 释放资源
///
public void Dispose()
{
_chartHost?.Dispose();
_chart = null;
_pressureValues = null;
_temperatureValues = null;
_timeLabels = null;
}
}
}