122 lines
3.3 KiB
C#
122 lines
3.3 KiB
C#
using OxyPlot;
|
|
using OxyPlot.Series;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace EmptyLoadTest
|
|
{
|
|
public partial class CurveWindow : Window
|
|
{
|
|
private PlotModel _plotModel;
|
|
private LineSeries _torqueSpeedSeries;
|
|
private DispatcherTimer _updateTimer;
|
|
private static CurveWindow _instance;
|
|
|
|
// 用于存储数据的队列(支持后台记录)
|
|
private readonly Queue<DataPoint> _dataQueue = new Queue<DataPoint>();
|
|
private readonly object _lockObject = new object();
|
|
|
|
// 最大缓存数据点数
|
|
private const int MAX_CACHE_POINTS = 1000;
|
|
|
|
public static CurveWindow Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null || !_instance.IsLoaded)
|
|
{
|
|
_instance = new CurveWindow();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public CurveWindow()
|
|
{
|
|
InitializeComponent();
|
|
InitializeChart();
|
|
InitializeUpdateTimer();
|
|
}
|
|
|
|
// 外部调用此方法来更新数据(即使窗口未打开也会缓存)
|
|
public void UpdateData(float torque, float speed)
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
// 添加到队列
|
|
_dataQueue.Enqueue(new DataPoint(torque, speed));
|
|
|
|
// 限制队列大小
|
|
if (_dataQueue.Count > MAX_CACHE_POINTS)
|
|
{
|
|
_dataQueue.Dequeue();
|
|
}
|
|
}
|
|
|
|
// 如果窗口已打开,立即更新显示
|
|
if (this.IsLoaded && IsVisible)
|
|
{
|
|
ProcessQueuedData();
|
|
}
|
|
}
|
|
|
|
private void ProcessQueuedData()
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
while (_dataQueue.Count > 0)
|
|
{
|
|
var point = _dataQueue.Dequeue();
|
|
_torqueSpeedSeries.Points.Add(point);
|
|
|
|
// 限制显示的数据点数
|
|
if (_torqueSpeedSeries.Points.Count > 500)
|
|
{
|
|
_torqueSpeedSeries.Points.RemoveAt(0);
|
|
}
|
|
}
|
|
|
|
_plotModel.InvalidatePlot(true);
|
|
UpdateStatusDisplay();
|
|
}
|
|
}
|
|
|
|
private void UpdateTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// 处理队列中的数据
|
|
ProcessQueuedData();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"曲线更新失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
// 新增:获取当前数据点数
|
|
public int GetDataCount()
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
return _dataQueue.Count + _torqueSpeedSeries.Points.Count;
|
|
}
|
|
}
|
|
|
|
// 清空数据
|
|
public void ClearData()
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
_dataQueue.Clear();
|
|
_torqueSpeedSeries.Points.Clear();
|
|
}
|
|
|
|
if (this.IsLoaded)
|
|
{
|
|
_plotModel.InvalidatePlot(true);
|
|
UpdateStatusDisplay();
|
|
}
|
|
}
|
|
}
|
|
} |