实现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,31 +248,55 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
return; return;
} }
// 创建数值点集合 // 如果系列集合为空或没有有效的系列,则重新创建
var values = new List<DateTimePoint>(); if (LineSeriesCollection == null || LineSeriesCollection.Length == 0)
foreach (var history in _variableHistoryList)
{ {
// 尝试将值转换为double // 创建数值点集合
if (double.TryParse(history.Value, out double value)) var values = new List<DateTimePoint>();
foreach (var history in _variableHistoryList)
{ {
values.Add(new DateTimePoint(history.Timestamp, value)); // 尝试将值转换为double
if (double.TryParse(history.Value, out double value))
{
values.Add(new DateTimePoint(history.Timestamp, value));
}
} }
// 创建线性序列
var series = new LineSeries<DateTimePoint>
{
Name = CurrentVariable?.Name ?? "变量值",
Values = values,
Fill = null,
Stroke = new SolidColorPaint(new SKColor(41, 128, 185)) { StrokeThickness = 2 },
GeometrySize = 6, // 显示数据点圆点大小为6
LineSmoothness = 0 // 使用直线连接点,也可以设为其他值实现曲线
};
// 更新序列集合
LineSeriesCollection = new ISeries[] { series };
} }
else
// 创建线性序列
var series = new LineSeries<DateTimePoint>
{ {
Name = CurrentVariable?.Name ?? "变量值", // 对于实时更新,保持原有完整的更新逻辑以确保数据一致性
Values = values, // 创建数值点集合
Fill = null, var values = new List<DateTimePoint>();
Stroke = new SolidColorPaint(new SKColor(41, 128, 185)) { StrokeThickness = 2 },
GeometrySize = 6, // 显示数据点圆点大小为6
LineSmoothness = 0 // 使用直线连接点,也可以设为其他值实现曲线
};
// 更新序列集合 foreach (var history in _variableHistoryList)
LineSeriesCollection = new ISeries[] { series }; {
// 尝试将值转换为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[]