ProcessDataChartDataItem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Runtime.Serialization;
  7. using SciChart.Charting.Model.DataSeries;
  8. using SciChart.Charting.Visuals.RenderableSeries;
  9. using Aitex.Core.UI.MVVM;
  10. using Aitex.Core.UI.View.Smart;
  11. using SciChart.Charting.Visuals.Annotations;
  12. using SciChart.Data.Model;
  13. namespace Aitex.Core.UI.ControlDataContext
  14. {
  15. [DataContract]
  16. [Serializable]
  17. public class HistoryDataItem
  18. {
  19. [DataMember]
  20. public DateTime dateTime
  21. {
  22. get;
  23. set;
  24. }
  25. [DataMember]
  26. public string dbName
  27. {
  28. get;
  29. set;
  30. }
  31. [DataMember]
  32. public double value
  33. {
  34. get;
  35. set;
  36. }
  37. }
  38. public class ProcessDataChartDataItem : INotifyPropertyChanged
  39. {
  40. public event PropertyChangedEventHandler PropertyChanged;
  41. public void InvokePropertyChanged(string propertyName)
  42. {
  43. if (PropertyChanged != null)
  44. {
  45. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  46. }
  47. }
  48. public DelegateCommand<object> SeriesSelectAllCommand
  49. {
  50. get;
  51. private set;
  52. }
  53. public DelegateCommand<object> SeriesSelectNoneCommand
  54. {
  55. get;
  56. private set;
  57. }
  58. public DelegateCommand<object> SeriesSelectDefaultCommand
  59. {
  60. get;
  61. private set;
  62. }
  63. public ObservableCollection<IRenderableSeries> RenderableSeries
  64. {
  65. get;
  66. set;
  67. }
  68. public string ProcessInfo
  69. {
  70. get;
  71. set;
  72. }
  73. public int Count
  74. {
  75. get
  76. {
  77. return RenderableSeries.Count;
  78. }
  79. }
  80. private int _capacity = 10000;
  81. private AnnotationCollection _annotations;
  82. public AnnotationCollection Annotations
  83. {
  84. get => _annotations;
  85. set
  86. {
  87. _annotations = value;
  88. InvokePropertyChanged(nameof(Annotations));
  89. }
  90. }
  91. public ProcessDataChartDataItem(int capacity = 60 * 5)
  92. {
  93. SeriesSelectAllCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectAll), null);
  94. SeriesSelectNoneCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectNone), null);
  95. SeriesSelectDefaultCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectDefault), null);
  96. RenderableSeries = new ObservableCollection<IRenderableSeries>();
  97. _capacity = capacity;
  98. }
  99. public void SetInfo(string info)
  100. {
  101. ProcessInfo = info;
  102. InvokePropertyChanged("ProcessInfo");
  103. }
  104. public void AppendData(Dictionary<string, object> data)
  105. {
  106. if (data == null)
  107. return;
  108. foreach (var item in RenderableSeries)
  109. {
  110. var series = item as SmartDataLine;
  111. if (series == null)
  112. continue;
  113. if (!data.ContainsKey(series.DbDataName))
  114. continue;
  115. var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
  116. while (dataSeries.Count > _capacity)
  117. dataSeries.RemoveAt(0);
  118. dataSeries.Append(DateTime.Now, (float)(Convert.ToDouble(data[series.DbDataName])));
  119. }
  120. }
  121. public void UpdateCultureString(Dictionary<string, string> data)
  122. {
  123. if (data == null)
  124. return;
  125. foreach (var item in RenderableSeries)
  126. {
  127. var series = item as SmartDataLine;
  128. if (series == null)
  129. continue;
  130. if (!data.ContainsKey(series.DbDataName))
  131. continue;
  132. series.DisplayName = data[series.DbDataName];
  133. }
  134. }
  135. public void UpdateData(List<HistoryDataItem> data)
  136. {
  137. ClearData();
  138. if (data == null || data.Count == 0)
  139. return;
  140. foreach (HistoryDataItem dataItem in data)
  141. {
  142. foreach (var item in RenderableSeries)
  143. {
  144. var series = item as SmartDataLine;
  145. if (series == null)
  146. continue;
  147. var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
  148. while (dataSeries.Count > _capacity)
  149. dataSeries.RemoveAt(0);
  150. if (series.DbDataName != dataItem.dbName)
  151. continue;
  152. dataSeries.Append(dataItem.dateTime, (float)(dataItem.value));
  153. }
  154. }
  155. }
  156. public void ClearData()
  157. {
  158. foreach (var item in RenderableSeries)
  159. {
  160. var series = item as SmartDataLine;
  161. if (series == null)
  162. continue;
  163. var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
  164. dataSeries.Clear();
  165. }
  166. }
  167. /// <summary>
  168. /// 如果已经有了key,更新名字和做标注
  169. /// 如果不存在,创建新的
  170. /// </summary>
  171. /// <param name="items"></param>
  172. public void UpdateColumns(Dictionary<string/*key value=database column name*/, Tuple<string, string, bool>> dicItems)
  173. {
  174. List<IRenderableSeries> lstRemovedSeries = new List<IRenderableSeries>();
  175. foreach (var seriesItem in RenderableSeries)
  176. {
  177. var series = seriesItem as SmartDataLine;
  178. if (series == null)
  179. continue;
  180. if (!dicItems.ContainsKey(series.DbDataName))
  181. {
  182. lstRemovedSeries.Add(seriesItem);
  183. continue;
  184. }
  185. UpdateColumn(dicItems[series.DbDataName]);
  186. dicItems.Remove(series.DbDataName);
  187. }
  188. foreach (var newItem in dicItems)
  189. {
  190. RenderableSeries.Add(new SmartDataLine(newItem.Value.Item2, newItem.Value.Item3 ? System.Windows.Media.Colors.Blue : System.Windows.Media.Colors.Red,
  191. newItem.Value.Item1, true)
  192. {
  193. YAxisId = newItem.Value.Item3 ? "PressureYAxisId" : "GeneralYAxisId"
  194. });
  195. }
  196. foreach (var removeItem in lstRemovedSeries)
  197. {
  198. RenderableSeries.Remove(removeItem);
  199. }
  200. }
  201. public void UpdateColumn(Tuple<string, string, bool> columnInfo)
  202. {
  203. foreach (var item in RenderableSeries)
  204. {
  205. var series = item as SmartDataLine;
  206. if (series != null && series.DbDataName == columnInfo.Item1)
  207. {
  208. series.DisplayName = columnInfo.Item2;
  209. series.YAxisId = columnInfo.Item3 ? "PressureYAxisId" : "GeneralYAxisId";
  210. break;
  211. }
  212. }
  213. }
  214. public void UpdateColumn(string dbName, string displayName, bool isPressure)
  215. {
  216. foreach (var item in RenderableSeries)
  217. {
  218. var series = item as SmartDataLine;
  219. if (series != null && series.DbDataName == dbName)
  220. {
  221. series.DisplayName = displayName;
  222. break;
  223. }
  224. }
  225. }
  226. public void UpdateColumnDisplayName(string dbName, string displayName)
  227. {
  228. foreach (var item in RenderableSeries)
  229. {
  230. var series = item as SmartDataLine;
  231. if (series != null && series.DbDataName == dbName)
  232. {
  233. series.DisplayName = displayName;
  234. break;
  235. }
  236. }
  237. }
  238. public void RemoveColumn(string dbName)
  239. {
  240. foreach (var item in RenderableSeries)
  241. {
  242. var series = item as SmartDataLine;
  243. if (series != null && series.DbDataName == dbName)
  244. {
  245. RenderableSeries.Remove(series);
  246. break;
  247. }
  248. }
  249. }
  250. public void RemoveAllColumn()
  251. {
  252. ClearData();
  253. RenderableSeries.Clear();
  254. }
  255. public void InitColumns(Dictionary<string, string> items)
  256. {
  257. ClearData();
  258. RenderableSeries.Clear();
  259. foreach (KeyValuePair<string, string> item in items)
  260. {
  261. RenderableSeries.Add(new SmartDataLine(item.Key, System.Windows.Media.Colors.Blue,
  262. item.Value, true)
  263. {
  264. YAxisId = item.Key.Contains("Pressure") ? "PressureYAxisId" : "GeneralYAxisId"
  265. });
  266. }
  267. }
  268. public void SetColor(string key, System.Windows.Media.Color color)
  269. {
  270. foreach (var item in RenderableSeries)
  271. {
  272. var series = item as SmartDataLine;
  273. if (series == null)
  274. continue;
  275. if (series.DisplayName == key)
  276. series.Stroke = color;
  277. }
  278. }
  279. public void OnSeriesSelectAll(object param)
  280. {
  281. foreach (var item in RenderableSeries)
  282. {
  283. var series = item as SmartDataLine;
  284. if (series == null)
  285. continue;
  286. series.IsVisible = true;
  287. }
  288. }
  289. public void OnSeriesSelectNone(object param)
  290. {
  291. foreach (var item in RenderableSeries)
  292. {
  293. var series = item as SmartDataLine;
  294. if (series == null)
  295. continue;
  296. series.IsVisible = false;
  297. }
  298. }
  299. public void OnSeriesSelectDefault(object param)
  300. {
  301. foreach (var item in RenderableSeries)
  302. {
  303. var series = item as SmartDataLine;
  304. if (series == null)
  305. continue;
  306. series.IsVisible = series.IsDefaultVisable;
  307. series.Stroke = series.DefaultSeriesColor;
  308. series.LineThickness = 1;
  309. }
  310. }
  311. }
  312. }