ChartDataLine.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows.Media;
  5. using Aitex.Core.Util;
  6. using SciChart.Charting.Model.DataSeries;
  7. using SciChart.Charting.Visuals.RenderableSeries;
  8. namespace MECF.Framework.Common.ControlDataContext
  9. {
  10. public class ChartDataLine<T> : FastLineRenderableSeries, INotifyPropertyChanged where T : IComparable
  11. {
  12. #region PropertyChanged
  13. public event PropertyChangedEventHandler PropertyChanged;
  14. public void InvokePropertyChanged(string propertyName)
  15. {
  16. PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(propertyName);
  17. PropertyChangedEventHandler changed = PropertyChanged;
  18. if (changed != null)
  19. {
  20. changed(this, eventArgs);
  21. }
  22. }
  23. public void InvokePropertyChanged()
  24. {
  25. Type t = this.GetType();
  26. var ps = t.GetProperties();
  27. foreach (var p in ps)
  28. {
  29. InvokePropertyChanged(p.Name);
  30. }
  31. }
  32. #endregion
  33. public string UniqueId { get; set; }
  34. public List<Tuple<T, double>> Points
  35. {
  36. get
  37. {
  38. lock (_lockData)
  39. {
  40. return _queueRawData.ToList();
  41. }
  42. }
  43. }
  44. public string DataSource { get; set; }
  45. public string DataName { get; set; }
  46. public string DisplayName
  47. {
  48. get
  49. {
  50. return DataSeries.SeriesName;
  51. }
  52. set
  53. {
  54. DataSeries.SeriesName = value;
  55. }
  56. }
  57. public double LineThickness
  58. {
  59. get
  60. {
  61. return StrokeThickness;
  62. }
  63. set
  64. {
  65. var i = Convert.ToInt32(value);
  66. if (i < 1) i = 1;
  67. if (i > 100) i = 100;
  68. StrokeThickness = i;
  69. InvokePropertyChanged("LineThickness");
  70. }
  71. }
  72. private double _dataFactor = 1.0;
  73. public double DataFactor
  74. {
  75. get => _dataFactor;
  76. set
  77. {
  78. if (Math.Abs(_dataFactor - value) > 0.001)
  79. {
  80. _dataFactor = value;
  81. InvokePropertyChanged(nameof(DataFactor));
  82. UpdateChartSeriesValue();
  83. }
  84. }
  85. }
  86. private double _dataOffset = 0;
  87. public double DataOffset
  88. {
  89. get => _dataOffset;
  90. set
  91. {
  92. if (Math.Abs(_dataOffset - value) > 0.001)
  93. {
  94. _dataOffset = value;
  95. InvokePropertyChanged(nameof(DataFactor));
  96. UpdateChartSeriesValue();
  97. }
  98. }
  99. }
  100. private int _capacity = 10000;
  101. public int Capacity
  102. {
  103. get { return _capacity; }
  104. set
  105. {
  106. _capacity = value;
  107. //DataSeries.FifoCapacity = value;
  108. if (_queueRawData != null)
  109. {
  110. _queueRawData.FixedSize = value;
  111. }
  112. }
  113. }
  114. private object _lockData = new object();
  115. private FixSizeQueue<Tuple<T, double>> _queueRawData;
  116. public ChartDataLine(string dataName)
  117. : this(dataName, dataName, Colors.Blue)
  118. {
  119. }
  120. public ChartDataLine(string dataName, string displayName, Color seriesColor)
  121. {
  122. UniqueId = Guid.NewGuid().ToString();
  123. _queueRawData = new FixSizeQueue<Tuple<T, double>>(_capacity);
  124. XAxisId = "DefaultAxisId";
  125. YAxisId = "DefaultAxisId";
  126. DataSeries = new XyDataSeries<T, double>(0);
  127. DataSeries.AcceptsUnsortedData = true;
  128. DisplayName = displayName;
  129. DataName = dataName;
  130. Stroke = seriesColor;
  131. IsVisible = true;
  132. DataOffset = 0;
  133. LineThickness = 1;
  134. DataFactor = 1;
  135. }
  136. public void Append(T dt, double value)
  137. {
  138. lock (_lockData)
  139. {
  140. _queueRawData.Enqueue(Tuple.Create(dt, value));
  141. var series = DataSeries as XyDataSeries<T, double>;
  142. if (series != null)
  143. {
  144. series.Append(dt, value * _dataFactor + _dataOffset);
  145. while (series.Count > _capacity)
  146. {
  147. series.RemoveRange(0, series.Count - _capacity);
  148. }
  149. }
  150. //(DataSeries as XyDataSeries<DateTime, double>)?.Append(dt, value*_dataFactor + _dataOffset);
  151. }
  152. }
  153. public void UpdateChartSeriesValue()
  154. {
  155. lock (_lockData)
  156. {
  157. using (DataSeries.SuspendUpdates())
  158. {
  159. XyDataSeries<DateTime, double> s = DataSeries as XyDataSeries<DateTime, double>;
  160. for (int i = 0; i < s.Count; i++)
  161. {
  162. s.Update(i, _queueRawData.ElementAt(i).Item2 * _dataFactor + _dataOffset);
  163. }
  164. }
  165. }
  166. }
  167. public void ClearData()
  168. {
  169. lock (_lockData)
  170. {
  171. _queueRawData.Clear();
  172. (DataSeries as XyDataSeries<DateTime, double>)?.Clear();
  173. }
  174. }
  175. }
  176. }