实现VariableHistoryView中图表的动态更新

This commit is contained in:
2025-10-03 12:10:37 +08:00
parent 65c7232efa
commit 1858238927

View File

@@ -108,21 +108,28 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
{ {
// if (e.Variable.Id != CurrentVariable.Id) if (e.Variable.Id != CurrentVariable.Id)
// { {
// return; return;
// } }
//
// var variableHistory = new VariableHistoryDto() var variableHistory = new VariableHistoryDto()
// { {
// VariableId = CurrentVariable.Id, VariableId = CurrentVariable.Id,
// Timestamp = DateTime.Now, Timestamp = DateTime.Now,
// Value = e.Variable.DataValue Value = e.Variable.DisplayValue
// }; };
// _variableHistoryList.Add(variableHistory); _variableHistoryList.Add(variableHistory);
//
// // 更新图表数据 // 限制历史记录数量以防止内存溢出
// UpdateChartData(); if (HistoryLimit.HasValue && _variableHistoryList.Count > HistoryLimit.Value)
{
// 移除最旧的记录
_variableHistoryList.RemoveAt(0);
}
// 更新图表数据
UpdateChartData();
} }
/// <summary> /// <summary>
@@ -241,6 +248,9 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
return; return;
} }
// 如果系列集合为空或没有有效的系列,则重新创建
if (LineSeriesCollection == null || LineSeriesCollection.Length == 0)
{
// 创建数值点集合 // 创建数值点集合
var values = new List<DateTimePoint>(); var values = new List<DateTimePoint>();
@@ -266,6 +276,27 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
// 更新序列集合 // 更新序列集合
LineSeriesCollection = new ISeries[] { series }; LineSeriesCollection = new ISeries[] { series };
}
else
{
// 对于实时更新,保持原有完整的更新逻辑以确保数据一致性
// 创建数值点集合
var values = new List<DateTimePoint>();
foreach (var history in _variableHistoryList)
{
// 尝试将值转换为double
if (double.TryParse(history.Value, out double value))
{
values.Add(new DateTimePoint(history.Timestamp, value));
}
}
// 更新当前系列
var currentSeries = (LineSeries<DateTimePoint>)LineSeriesCollection[0];
currentSeries.Values = values;
currentSeries.Name = CurrentVariable?.Name ?? "变量值";
}
// 更新坐标轴 // 更新坐标轴
LineAxisX = new Axis[] LineAxisX = new Axis[]