RealTimeLineChart.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using Microsoft.Win32;
  2. using ScottPlot;
  3. using ScottPlot.Plottables;
  4. using ScottPlot.WPF;
  5. using SWRIS.Core;
  6. using SWRIS.Models;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Threading;
  14. namespace SWRIS.Controls
  15. {
  16. /// <summary>
  17. /// 动态折线图用户控件(基于 ScottPlot.WPF 的高性能版本)
  18. /// </summary>
  19. public partial class RealTimeLineChart : UserControl
  20. {
  21. #region 依赖属性定义
  22. public static readonly DependencyProperty SensorCountProperty =
  23. DependencyProperty.Register(
  24. nameof(SensorCount),
  25. typeof(int),
  26. typeof(RealTimeLineChart),
  27. new PropertyMetadata(4, OnSensorCountChanged));
  28. public static readonly DependencyProperty RopeLengthProperty =
  29. DependencyProperty.Register(
  30. nameof(RopeLength),
  31. typeof(double),
  32. typeof(RealTimeLineChart),
  33. new PropertyMetadata(100.0, OnAxisXChanged));
  34. public static readonly DependencyProperty SamplingStepProperty =
  35. DependencyProperty.Register(
  36. nameof(SamplingStep),
  37. typeof(double),
  38. typeof(RealTimeLineChart),
  39. new PropertyMetadata(0.1275));
  40. #endregion
  41. #region CLR 包装属性
  42. public int SensorCount
  43. {
  44. get => (int)GetValue(SensorCountProperty);
  45. set => SetValue(SensorCountProperty, value);
  46. }
  47. public double RopeLength
  48. {
  49. get => (double)GetValue(RopeLengthProperty);
  50. set => SetValue(RopeLengthProperty, value);
  51. }
  52. public double SamplingStep
  53. {
  54. get => (double)GetValue(SamplingStepProperty);
  55. set => SetValue(SamplingStepProperty, value);
  56. }
  57. #endregion
  58. public double? _lastPosition = null;
  59. private ConcurrentQueue<ushort> _sensorDataQueues;
  60. private const double MINDAMAGE = 40; // 最小损伤值
  61. private const double MAXDAMAGE = 120; // 最大损伤值
  62. private int _displayPoints = 1000000; // 显示点数
  63. private DispatcherTimer UpdatePlotTimer;
  64. private DataLogger _dataLogger;
  65. public RealTimeLineChart()
  66. {
  67. InitializeComponent();
  68. InitializePlot();
  69. }
  70. private void OnLoaded(object sender, RoutedEventArgs e)
  71. {
  72. InitializePlot();
  73. }
  74. #region 初始化方法
  75. private void InitializePlot()
  76. {
  77. WpfPlot.Menu?.Clear();
  78. WpfPlot.Menu.Add("保存图片", pt =>
  79. {
  80. OpenSaveImageDialog(pt);
  81. });
  82. WpfPlot.Menu.Add("清除曲线", pt =>
  83. {
  84. ClearPoints();
  85. });
  86. WpfPlot.Menu.Add("重置视图", pt =>
  87. {
  88. SetLimits();
  89. });
  90. var plot = WpfPlot.Plot;
  91. plot.Clear();
  92. plot.DataBackground.Color = Colors.Transparent;
  93. plot.FigureBackground.Color = Colors.Transparent;
  94. plot.Grid.MajorLineColor = new Color(59, 59, 123).WithOpacity(0.6);
  95. plot.Grid.XAxis.TickLabelStyle.ForeColor = new Color(151, 166, 212);
  96. plot.Grid.YAxis.TickLabelStyle.ForeColor = new Color(151, 166, 212);
  97. plot.Grid.XAxis.FrameLineStyle.Color = new Color(53, 53, 112);
  98. plot.Grid.XAxis.FrameLineStyle.Width = 2;
  99. plot.Grid.YAxis.FrameLineStyle.Color = new Color(53, 53, 112);
  100. plot.Grid.YAxis.FrameLineStyle.Width = 2;
  101. plot.Grid.XAxis.MajorTickStyle.Color = new Color(53, 53, 112);
  102. plot.Grid.XAxis.MajorTickStyle.Width = 1;
  103. plot.Grid.YAxis.MajorTickStyle.Color = new Color(53, 53, 112);
  104. plot.Grid.YAxis.MajorTickStyle.Width = 1;
  105. plot.Grid.YAxis.MinorTickStyle.Color = new Color(53, 53, 112);
  106. plot.Grid.XAxis.MinorTickStyle.Color = new Color(53, 53, 112);
  107. plot.Axes.Hairline(false);
  108. _sensorDataQueues = new ConcurrentQueue<ushort>();
  109. _dataLogger = plot.Add.DataLogger();
  110. _dataLogger.LineWidth = 2;
  111. _dataLogger.Color = new Color("#f47c7c");
  112. _dataLogger.ManageAxisLimits = false;
  113. WpfPlot.Plot.Axes.SetLimits(0, RopeLength * 1.2, MINDAMAGE, MAXDAMAGE);
  114. WpfPlot.Refresh();
  115. UpdatePlotTimer?.Stop();
  116. UpdatePlotTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(200) };
  117. UpdatePlotTimer.Tick += (s, d) =>
  118. {
  119. if (_dataLogger.HasNewData)
  120. {
  121. SetLimits();
  122. }
  123. };
  124. UpdatePlotTimer.Start();
  125. }
  126. #endregion
  127. #region 数据操作方法
  128. private void SetLimits()
  129. {
  130. if (_lastPosition == null || _lastPosition.Value < RopeLength)
  131. {
  132. WpfPlot.Plot.Axes.SetLimits(0, RopeLength * 1.2, MINDAMAGE, MAXDAMAGE);
  133. }
  134. else
  135. {
  136. WpfPlot.Plot.Axes.SetLimits(0, _lastPosition.Value + (RopeLength * 0.12), MINDAMAGE, MAXDAMAGE);
  137. }
  138. WpfPlot.Refresh();
  139. }
  140. public void ClearPoints()
  141. {
  142. lock (_sensorDataQueues)
  143. {
  144. while (_sensorDataQueues.TryDequeue(out _)) ;
  145. }
  146. _dataLogger.Clear();
  147. WpfPlot.Refresh();
  148. }
  149. public void AddDataPoints(Coordinates[] chartData)
  150. {
  151. _dataLogger.Add(chartData);
  152. _lastPosition = chartData.LastOrDefault().X;
  153. WpfPlot.Refresh();
  154. }
  155. public Coordinates[] GetChartSource()
  156. {
  157. return _dataLogger.Data.Coordinates.ToArray();
  158. }
  159. public void AddDataPoints((double Position, LiveStreamDataModel ListStream) dataPoints, int[] inUseSensors)
  160. {
  161. if (_lastPosition == null)
  162. {
  163. _lastPosition = dataPoints.Position;
  164. return;
  165. }
  166. if (dataPoints.Position < _lastPosition)
  167. {
  168. lock (_sensorDataQueues)
  169. {
  170. while (_sensorDataQueues.TryDequeue(out _)) ;
  171. _lastPosition = dataPoints.Position;
  172. }
  173. _dataLogger.Data.Clear();
  174. }
  175. else
  176. {
  177. for (int i = 0; i < dataPoints.ListStream.SampleCount; i++)
  178. {
  179. ushort totalValue = 0;
  180. ushort totalCount = 0;
  181. ushort[] values = dataPoints.ListStream.Data[i];
  182. foreach (var j in inUseSensors)
  183. {
  184. totalCount++;
  185. totalValue += values[j - 1];
  186. }
  187. _sensorDataQueues.Enqueue((ushort)(totalValue / totalCount));
  188. }
  189. }
  190. if (dataPoints.Position > _lastPosition)
  191. {
  192. UpdateLoop(_lastPosition.Value, dataPoints.Position);
  193. _lastPosition = dataPoints.Position;
  194. }
  195. }
  196. public void OpenSaveImageDialog(Plot plot)
  197. {
  198. SaveFileDialog saveFileDialog = new SaveFileDialog
  199. {
  200. Filter = "PNG Files (*.png)|*.png|JPEG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg|BMP Files (*.bmp)|*.bmp|WebP Files (*.webp)|*.webp|SVG Files (*.svg)|*.svg|All files (*.*)|*.*"
  201. };
  202. bool? flag = saveFileDialog.ShowDialog();
  203. if (flag.HasValue && flag == true && !string.IsNullOrEmpty(saveFileDialog.FileName))
  204. {
  205. ImageFormat format;
  206. try
  207. {
  208. format = ImageFormats.FromFilename(saveFileDialog.FileName);
  209. }
  210. catch (ArgumentException)
  211. {
  212. MessageBox.Show("Unsupported image file format", "ERROR", MessageBoxButton.OK, MessageBoxImage.Hand);
  213. return;
  214. }
  215. try
  216. {
  217. PixelSize size = plot.RenderManager.LastRender.FigureRect.Size;
  218. plot.Save(saveFileDialog.FileName, (int)size.Width, (int)size.Height, format);
  219. }
  220. catch (Exception)
  221. {
  222. MessageBox.Show("Image save failed", "ERROR", MessageBoxButton.OK, MessageBoxImage.Hand);
  223. }
  224. }
  225. }
  226. private void UpdateLoop(double lastPosition, double position)
  227. {
  228. try
  229. {
  230. // 在后台线程准备数据
  231. List<(double x, ushort y)> dataToAdd = new List<(double x, ushort y)>();
  232. var step = (position - lastPosition) / Math.Max(1, _sensorDataQueues.Count);
  233. double currentX = lastPosition;
  234. while (_sensorDataQueues.TryDequeue(out ushort data))
  235. {
  236. currentX += step;
  237. // 确保 x 值严格递增
  238. if (dataToAdd.Count > 0 && currentX <= dataToAdd[dataToAdd.Count - 1].x)
  239. {
  240. currentX = dataToAdd[dataToAdd.Count - 1].x + Math.Abs(step) * 0.001;
  241. }
  242. dataToAdd.Add((currentX, data));
  243. }
  244. // 在UI线程批量添加
  245. Dispatcher.Invoke(() =>
  246. {
  247. lock (_dataLogger)
  248. {
  249. // 获取最后一个点的 x 值
  250. double lastX = _dataLogger.Data.Coordinates.Count > 0
  251. ? _dataLogger.Data.Coordinates[_dataLogger.Data.Coordinates.Count - 1].X
  252. : double.MinValue;
  253. foreach (var point in dataToAdd)
  254. {
  255. double safeX = point.x;
  256. if (safeX <= lastX)
  257. {
  258. safeX = lastX + Math.Abs(position - lastPosition) * 0.0001;
  259. }
  260. _dataLogger.Add(safeX, point.y);
  261. lastX = safeX;
  262. }
  263. }
  264. // 清理操作
  265. if (_dataLogger.Data.Coordinates.Count > _displayPoints)
  266. {
  267. int removeCount = (int)(_displayPoints * 0.1d);
  268. removeCount = Math.Min(removeCount, _dataLogger.Data.Coordinates.Count - 1);
  269. if (removeCount > 0)
  270. {
  271. _dataLogger.Data.Coordinates.RemoveRange(0, removeCount);
  272. }
  273. }
  274. });
  275. }
  276. catch (Exception ex)
  277. {
  278. LogHelper.Error($"绘制曲线时发生错误:{ex.Message}", ex);
  279. }
  280. }
  281. #endregion
  282. #region 静态回调
  283. private static void OnSensorCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  284. {
  285. if (d is RealTimeLineChart control)
  286. control.OnSensorCountChanged((int)e.OldValue, (int)e.NewValue);
  287. }
  288. private static void OnAxisXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  289. {
  290. if (d is RealTimeLineChart control)
  291. control.UpdateAxisX();
  292. }
  293. #endregion
  294. #region 坐标轴更新逻辑
  295. private void UpdateAxisX()
  296. {
  297. if (WpfPlot?.Plot == null) return;
  298. WpfPlot.Plot.Axes.SetLimitsX(0, RopeLength * 1.2);
  299. WpfPlot.Refresh();
  300. }
  301. #endregion
  302. #region 传感器数量变化处理
  303. private void OnSensorCountChanged(int oldCount, int newCount)
  304. {
  305. Dispatcher.Invoke(() =>
  306. {
  307. InitializePlot();
  308. });
  309. }
  310. #endregion
  311. }
  312. }