ChartDataLine.cs 5.5 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 : FastLineRenderableSeries, INotifyPropertyChanged
  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<DateTime, 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<DateTime, 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<DateTime, double>>(_capacity);
  124. XAxisId = "DefaultAxisId";
  125. YAxisId = "DefaultAxisId";
  126. DataSeries = new XyDataSeries<DateTime, double>(0);
  127. DisplayName = displayName;
  128. DataName = dataName;
  129. Stroke = seriesColor;
  130. IsVisible = true;
  131. DataOffset = 0;
  132. LineThickness = 1;
  133. DataFactor = 1;
  134. }
  135. public void Append(DateTime dt, double value)
  136. {
  137. lock (_lockData)
  138. {
  139. _queueRawData.Enqueue(Tuple.Create(dt, value));
  140. var series = DataSeries as XyDataSeries<DateTime, double>;
  141. if (series != null)
  142. {
  143. series.Append(dt, value * _dataFactor + _dataOffset);
  144. while (series.Count > _capacity)
  145. {
  146. series.RemoveRange(0, series.Count - _capacity);
  147. }
  148. }
  149. //(DataSeries as XyDataSeries<DateTime, double>)?.Append(dt, value*_dataFactor + _dataOffset);
  150. }
  151. }
  152. public void UpdateChartSeriesValue()
  153. {
  154. lock (_lockData)
  155. {
  156. using (DataSeries.SuspendUpdates())
  157. {
  158. XyDataSeries<DateTime, double> s = DataSeries as XyDataSeries<DateTime, double>;
  159. for (int i = 0; i < s.Count; i++)
  160. {
  161. s.Update(i, _queueRawData.ElementAt(i).Item2 * _dataFactor + _dataOffset);
  162. }
  163. }
  164. }
  165. }
  166. public void ClearData()
  167. {
  168. lock (_lockData)
  169. {
  170. _queueRawData.Clear();
  171. (DataSeries as XyDataSeries<DateTime, double>)?.Clear();
  172. }
  173. }
  174. }
  175. }