添加LiveCharts
This commit is contained in:
196
全自动水压检测仪/ChartManager.cs
Normal file
196
全自动水压检测仪/ChartManager.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ namespace 全自动水压检测仪
|
||||
private Coeffiicientsetting _coeffiicientsetting;
|
||||
private ScanImport _scanImport;
|
||||
private StatusSettingsForm _statusSettingsForm; // 新增状态设置窗体
|
||||
private ChartManager _chartManager; // 图表管理器
|
||||
|
||||
public List<ConductivityTestData> CurrentReport;
|
||||
private Report _report;
|
||||
@@ -100,6 +101,9 @@ namespace 全自动水压检测仪
|
||||
|
||||
_repository = new ConductivityRepository();
|
||||
CurrentReport = new List<ConductivityTestData>();
|
||||
|
||||
// 初始化图表管理器
|
||||
_chartManager = new ChartManager();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Timer InitAlarmMonitorTimer()
|
||||
@@ -501,6 +505,22 @@ namespace 全自动水压检测仪
|
||||
var value10 = c.UshortToFloat(modbusData.Real11[1], modbusData.Real11[0]);
|
||||
uiLabel38.Text = value10.ToString("F1");
|
||||
}
|
||||
|
||||
// 更新图表数据(实时压力和箱体温度)
|
||||
if (modbusData.Real1 != null && modbusData.Real1.Length >= 2 &&
|
||||
modbusData.Real10 != null && modbusData.Real10.Length >= 2)
|
||||
{
|
||||
try
|
||||
{
|
||||
var pressure = c.UshortToFloat(modbusData.Real1[1], modbusData.Real1[0]);
|
||||
var temperature = c.UshortToFloat(modbusData.Real10[1], modbusData.Real10[0]);
|
||||
_chartManager?.AddDataPoint(pressure, temperature);
|
||||
}
|
||||
catch (Exception chartEx)
|
||||
{
|
||||
Debug.WriteLine($"图表更新异常: {chartEx.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception uiEx)
|
||||
{
|
||||
@@ -937,6 +957,17 @@ namespace 全自动水压检测仪
|
||||
// _modbusMaster?.WriteSingleCoil(1, 30, true);
|
||||
boolSignal1.OnRisingEdge += BoolSignal1_OnRisingEdge;
|
||||
|
||||
// 初始化图表
|
||||
try
|
||||
{
|
||||
_chartManager.InitializeChart(uiPanel_ChartPlaceholder);
|
||||
Debug.WriteLine("[NormalTemperatureMode] 图表初始化成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"[NormalTemperatureMode] 图表初始化失败: {ex.Message}");
|
||||
}
|
||||
|
||||
// 在 Load 完成初始化后再启动定时器,避免定时器在 c / ma 未就绪时触发访问导致异常
|
||||
//_readTimer?.Start();
|
||||
//_readTimerTwo?.Start();
|
||||
@@ -1119,6 +1150,9 @@ namespace 全自动水压检测仪
|
||||
_alarmMonitorTimer?.Stop();
|
||||
_alarmMonitorTimer?.Dispose();
|
||||
|
||||
// 释放图表资源
|
||||
_chartManager?.Dispose();
|
||||
|
||||
// 仅用户主动关闭时退出应用
|
||||
if (e.CloseReason == CloseReason.UserClosing)
|
||||
{
|
||||
@@ -1209,6 +1243,10 @@ namespace 全自动水压检测仪
|
||||
{
|
||||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 10080);
|
||||
isAddTag = false;
|
||||
|
||||
// 清除图表数据,开始新的测试
|
||||
_chartManager?.ClearData();
|
||||
Debug.WriteLine("[NormalTemperatureMode] 启动测试,清除图表数据");
|
||||
}
|
||||
|
||||
//停止测试
|
||||
@@ -1226,7 +1264,14 @@ namespace 全自动水压检测仪
|
||||
// 延迟一小段时间等待PLC状态更新
|
||||
System.Threading.Tasks.Task.Delay(500).ContinueWith(_ =>
|
||||
{
|
||||
SafeInvoke(() => UpdateControlsVisibilityByMode());
|
||||
SafeInvoke(() =>
|
||||
{
|
||||
UpdateControlsVisibilityByMode();
|
||||
|
||||
// 更新图表模式
|
||||
bool isHighTempMode = uiLight2 != null && uiLight2.State == UILightState.On;
|
||||
_chartManager?.UpdateChartMode(isHighTempMode);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<package id="K4os.Compression.LZ4.Streams" version="1.3.8" targetFramework="net472" />
|
||||
<package id="K4os.Hash.xxHash" version="1.0.8" targetFramework="net472" />
|
||||
<package id="LiveCharts" version="0.9.7" targetFramework="net472" />
|
||||
<package id="LiveCharts.Wpf" version="0.9.7" targetFramework="net472" />
|
||||
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.1" targetFramework="net472" />
|
||||
<package id="MySql.Data" version="9.5.0" targetFramework="net472" />
|
||||
<package id="NModbus4" version="2.1.0" targetFramework="net472" />
|
||||
|
||||
@@ -62,6 +62,9 @@
|
||||
<Reference Include="LiveCharts, Version=0.9.7.0, Culture=neutral, PublicKeyToken=0bc1f845d1ebb8df, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\LiveCharts.0.9.7\lib\net45\LiveCharts.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="LiveCharts.Wpf, Version=0.9.7.0, Culture=neutral, PublicKeyToken=0bc1f845d1ebb8df, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\LiveCharts.Wpf.0.9.7\lib\net45\LiveCharts.Wpf.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=9.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.9.0.1\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -118,7 +121,9 @@
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xaml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
<Reference Include="WindowsFormsIntegration" />
|
||||
<Reference Include="ZstdSharp, Version=0.8.6.0, Culture=neutral, PublicKeyToken=8d151af33a4ad5cf, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\ZstdSharp.Port.0.8.6\lib\net462\ZstdSharp.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -130,6 +135,7 @@
|
||||
<Compile Include="ChangPassword.Designer.cs">
|
||||
<DependentUpon>ChangPassword.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ChartManager.cs" />
|
||||
<Compile Include="Coeffiicientsetting.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user