ProcessDataChartDataItem.cs 10 KB

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