TabStatisticViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.UI.Charting.ViewModel;
  6. using System.Collections.ObjectModel;
  7. using Aitex.UI.Charting.Command;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. using System.Windows.Threading;
  11. using Aitex.UI.Charting.View;
  12. using DataAnalysisControl.Core;
  13. using Aitex.UI.Charting.Model;
  14. namespace Aitex.UI.Charting.ViewModel
  15. {
  16. public class TabStatisticViewModel : ChartingBaseViewModel
  17. {
  18. public TabStatisticViewModel()
  19. {
  20. StatisticDataList = new ObservableCollection<ChartingDataStatisticResult>();
  21. StatisticDataBeginTime = DateTime.MinValue.ToString("yyyy/MM/dd HH:mm:ss");
  22. StatisticDataEndTime = DateTime.MinValue.ToString("yyyy/MM/dd HH:mm:ss");
  23. StatisticDataTimeSpan = "00:00:00";
  24. Export2File = new ChartingCommand((o) => true, (o) => Export2CsvFile(o));
  25. //初始化静态定时器
  26. _timer.Interval = new TimeSpan(0, 0, 1);
  27. _timer.Tick += new EventHandler(_timer_Tick);
  28. }
  29. public void Start()
  30. {
  31. PlotViewModel.ShowVerticalTimeLines(true);
  32. DoDataStatistic();
  33. _timer.Start();
  34. }
  35. public void Stop()
  36. {
  37. _timer.Stop();
  38. }
  39. /// <summary>
  40. /// 定时检查时间轴是否修改
  41. /// 如果检查到时间轴被修改,则更新统计数据并刷新界面
  42. /// </summary>
  43. /// <param name="sender"></param>
  44. /// <param name="e"></param>
  45. void _timer_Tick(object sender, EventArgs e)
  46. {
  47. try
  48. {
  49. if ((PlotViewModel.PlotView.toggleVerticalLine.IsChecked.HasValue && PlotViewModel.PlotView.toggleVerticalLine.IsChecked.Value)
  50. && (_timer1 != CommonViewModel.Instance.Time1 || _timer2 != CommonViewModel.Instance.Time2))
  51. {
  52. _timer1 = CommonViewModel.Instance.Time1;
  53. _timer2 = CommonViewModel.Instance.Time2;
  54. DoDataStatistic();
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. CONTEXT.WriteLog(ex);
  60. }
  61. }
  62. /// <summary>
  63. /// 用于定时检查时间轴是否已经被修改,如果修改计算新的统计值并更新界面
  64. /// 此处之所以借助定时器实现,而非通过时间轴的位置移动事件触发计算的原因是:
  65. /// sciChart控件的VerticalAnnotationLine控制的事件响应似乎不能正常工作
  66. /// </summary>
  67. private DispatcherTimer _timer = new DispatcherTimer();
  68. private DateTime _timer1 = DateTime.MinValue;
  69. private DateTime _timer2 = DateTime.MaxValue;
  70. //private static TabStatisticViewModel Instance { get; set; }
  71. /// <summary>
  72. /// 数据导出至CSV文件
  73. /// </summary>
  74. /// <param name="obj"></param>
  75. private void Export2CsvFile(object obj)
  76. {
  77. try
  78. {
  79. var dlg = new Microsoft.Win32.SaveFileDialog();
  80. dlg.FileName = "数据统计";
  81. dlg.DefaultExt = ".csv"; // Default file extension
  82. dlg.Filter = "Excel (.csv)|*.csv"; // Filter files by extension
  83. // Show open file dialog box
  84. Nullable<bool> result = dlg.ShowDialog();
  85. if (result.HasValue && result.Value)
  86. {
  87. using (System.IO.StreamWriter fs = new System.IO.StreamWriter(dlg.FileName, false, Encoding.UTF8))
  88. {
  89. fs.Write("【数据源名称】,【变量名】,【用户定义名称】,【总数量】,【最小值】,【最大值】,【平均值】,【方差】");
  90. fs.WriteLine();
  91. foreach (var item in StatisticDataList)
  92. {
  93. fs.Write(item.SourceName);
  94. fs.Write(",");
  95. fs.Write(item.DataName);
  96. fs.Write(",");
  97. fs.Write(item.UserDefinedName);
  98. fs.Write(",");
  99. fs.Write(item.TotalNum);
  100. fs.Write(",");
  101. fs.Write(item.Min);
  102. fs.Write(",");
  103. fs.Write(item.Max);
  104. fs.Write(",");
  105. fs.Write(item.Average);
  106. fs.Write(",");
  107. fs.Write(item.Deviation);
  108. fs.Write("\n");
  109. }
  110. fs.Close();
  111. }
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. CONTEXT.WriteLog(ex);
  117. MessageBox.Show("数据导出失败:\n" + ex.Message, "数据导出", MessageBoxButton.OK, MessageBoxImage.Warning);
  118. }
  119. }
  120. /// <summary>
  121. /// 数学统计计算
  122. /// </summary>
  123. private void DoDataStatistic()
  124. {
  125. if (PlotViewModel.PlotView.vertical_Line1.X1 == null || PlotViewModel.PlotView.vertical_Line2.X1 == null) return;
  126. //defaut show the vertical lines
  127. System.Diagnostics.Debug.WriteLine("Calc Statistic Data");
  128. StatisticDataList.Clear();
  129. StatisticDataBeginTime = DateTime.MinValue.ToString("yyyy/MM/dd HH:mm:ss");
  130. StatisticDataEndTime = DateTime.MinValue.ToString("yyyy/MM/dd HH:mm:ss");
  131. StatisticDataTimeSpan = "00:00:00";
  132. var commonViewModel = CommonViewModel.Instance;
  133. var minT1 = PlotViewModel.PlotView.vertical_Line1.X1 == null ? DateTime.Now : (DateTime)PlotViewModel.PlotView.vertical_Line1.X1;
  134. var maxT1 = PlotViewModel.PlotView.vertical_Line2.X1 == null ? DateTime.Now : (DateTime)PlotViewModel.PlotView.vertical_Line2.X1;
  135. if (minT1 >= maxT1)
  136. {
  137. var temp = minT1;
  138. minT1 = maxT1;
  139. maxT1 = temp;
  140. }
  141. foreach (MyLineSeries series in commonViewModel.RenderableSeries)
  142. {
  143. if (series == null || !series.IsVisible || series.DataSource == null)
  144. continue;
  145. var minT = minT1 - series.DataSource.TimeMove;
  146. var maxT = maxT1 - series.DataSource.TimeMove;
  147. double factor = series.Factor;
  148. double offset = series.Offset;
  149. DataItem datas = series.Points;
  150. //do statistic calculation here
  151. List<double> tempList = new List<double>();
  152. tempList.Clear();
  153. double min = double.MaxValue;
  154. double max = double.MinValue;
  155. double sum = 0;
  156. double ave = 0;
  157. double dev = 0;
  158. int totalNum = 0;
  159. DateTime curX = DateTime.MinValue;
  160. double curY = 0;
  161. if (factor == 0) factor = 1;
  162. for (int i = 0; i < datas.RawData.Count; i++)
  163. {
  164. curX = datas.TimeStamp[i];
  165. curY = datas.RawData[i];
  166. if (curX > minT && curX < maxT)
  167. {
  168. if (curY < min) min = curY;
  169. if (curY > max) max = curY;
  170. sum += curY;
  171. totalNum++;
  172. tempList.Add(curY);
  173. }
  174. }
  175. if (totalNum > 0)
  176. {
  177. ave = sum / totalNum;
  178. for (int i = 0; i < tempList.Count; i++)
  179. {
  180. var temp1 = tempList[i] - ave;
  181. dev += temp1 * temp1;
  182. }
  183. dev = dev / totalNum;
  184. }
  185. if (min == double.MaxValue)
  186. min = 0;
  187. if (max == double.MinValue)
  188. max = 0;
  189. StatisticDataList.Add(new ChartingDataStatisticResult()
  190. {
  191. SourceName = series.DataSourceName,
  192. DataName = series.SeriesDisplayName,//.SeriesName,
  193. UserDefinedName = series.DisplayName,
  194. Average = Math.Round(ave, 3),
  195. Deviation = Math.Round(dev, 3),
  196. Max = Math.Round(max, 3),
  197. Min = Math.Round(min, 3),
  198. TotalNum = totalNum
  199. });
  200. }
  201. StatisticDataBeginTime = minT1.ToString("yyyy/MM/dd HH:mm:ss");
  202. StatisticDataEndTime = maxT1.ToString("yyyy/MM/dd HH:mm:ss");
  203. var tSpan = maxT1 - minT1;
  204. StatisticDataTimeSpan = string.Format("{0}小时{1}分{2}秒", ((int)tSpan.TotalHours).ToString("00"),
  205. tSpan.Minutes, tSpan.Seconds);
  206. InvokePropertyChanged("StatisticDataBeginTime");
  207. InvokePropertyChanged("StatisticDataEndTime");
  208. InvokePropertyChanged("StatisticDataTimeSpan");
  209. InvokePropertyChanged("StatisticDataList");
  210. }
  211. //public ICommand DoDataStatistic { get; set; }
  212. public ICommand Export2File { get; set; }
  213. public ObservableCollection<ChartingDataStatisticResult> StatisticDataList { get; set; }
  214. public string StatisticDataBeginTime { get; set; }
  215. public string StatisticDataEndTime { get; set; }
  216. public string StatisticDataTimeSpan { get; set; }
  217. /// <summary>
  218. /// Charting中变量的统计值
  219. /// </summary>
  220. public class ChartingDataStatisticResult
  221. {
  222. public string SourceName { get; set; }
  223. public string DataName { get; set; }
  224. public string UserDefinedName { get; set; }
  225. public double Average { get; set; }
  226. public double Min { get; set; }
  227. public double Max { get; set; }
  228. public double Deviation { get; set; }
  229. public double TotalNum { get; set; }
  230. public string UniqueName { get; set; }
  231. }
  232. }
  233. }