RunTimeStatisticViewModel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Windows.Input;
  7. using Abt.Controls.SciChart;
  8. using Abt.Controls.SciChart.Visuals;
  9. using Abt.Controls.SciChart.Model.DataSeries;
  10. namespace DataAnalysisControl.Charting.ViewModel
  11. {
  12. public class RunTimeStatisticViewModel : ChartingBaseViewModel
  13. {
  14. public RunTimeStatisticViewModel(SciChartSurface plot)
  15. {
  16. _plot = plot;
  17. #if false
  18. DataSet = new DataSeriesSet<DateTime, double>();
  19. Show1YearCommand = new Aitex.UI.Charting.Command.ChartingCommand((o) => true, (o) =>
  20. { UpdateData(DateTime.Now - new TimeSpan(365, 0, 0, 0), DateTime.Now); });
  21. ShowAllCommand = new Aitex.UI.Charting.Command.ChartingCommand((o) => true, (o) =>
  22. { UpdateData(DateTime.MinValue, DateTime.MaxValue); });
  23. Show1YearCommand.Execute(null);
  24. #endif
  25. }
  26. private SciChartSurface _plot;
  27. #if false
  28. public ICommand Show1YearCommand { get; set; }
  29. public ICommand ShowAllCommand { get; set; }
  30. public XyDataSeries<DateTime, double> DataSet { get; set; }
  31. /// <summary>
  32. /// 更新数据
  33. /// </summary>
  34. /// <param name="begin"></param>
  35. /// <param name="end"></param>
  36. void UpdateData(DateTime begin, DateTime end)
  37. {
  38. var dataset = new DataSeriesSet<DateTime, double>();
  39. string[] chamNames = new string[] { "ReactorA", "ReactorB", "ReactorC", "ReactorD" };
  40. foreach (var cham in chamNames)
  41. {
  42. var commonViewModel = Aitex.UI.Charting.ViewModel.CommonViewModel.Instance;
  43. var ret = commonViewModel.GetSusceptorList(begin, end, cham);
  44. Dictionary<DateTime, double> runtimeDic = new Dictionary<DateTime, double>();
  45. foreach (var recipe in ret)
  46. {
  47. string pStartTimeStr = recipe.ProcessBeginTime;
  48. string pEndTimeStr = recipe.ProcessEndTime;
  49. if (string.IsNullOrEmpty(pStartTimeStr) || string.IsNullOrEmpty(pEndTimeStr))
  50. continue;
  51. DateTime pStartTime = DateTime.Parse(pStartTimeStr);
  52. DateTime pEndTime = DateTime.Parse(pEndTimeStr);
  53. if (pStartTime >= pEndTime)
  54. continue;
  55. for (DateTime d = pStartTime.Date; d <= pEndTime.Date; d += new TimeSpan(1, 0, 0, 0))
  56. {
  57. if (d == pStartTime.Date)
  58. {
  59. if (!runtimeDic.ContainsKey(d.Date))
  60. {
  61. runtimeDic.Add(d.Date, 0);
  62. }
  63. if (d != pEndTime.Date)
  64. {
  65. runtimeDic[d.Date] += (new DateTime(d.Year, d.Month, d.Day, 23, 59, 59) - pStartTime).TotalHours;
  66. }
  67. else
  68. {
  69. runtimeDic[d.Date] += (pEndTime - pStartTime).TotalHours;
  70. }
  71. }
  72. else if (d == pEndTime.Date)
  73. {
  74. if (!runtimeDic.ContainsKey(d.Date))
  75. {
  76. runtimeDic.Add(d.Date, 0);
  77. }
  78. if (d != pStartTime.Date)
  79. {
  80. runtimeDic[d.Date] += (new TimeSpan(pEndTime.Hour, pEndTime.Minute, pEndTime.Second)).TotalHours;
  81. }
  82. else
  83. {
  84. System.Diagnostics.Debug.Assert(false);
  85. }
  86. }
  87. else
  88. {
  89. if (!runtimeDic.ContainsKey(d.Date))
  90. {
  91. runtimeDic.Add(d.Date, 0);
  92. }
  93. runtimeDic[d.Date] += 24;
  94. }
  95. }
  96. }
  97. var series = dataset.AddSeries<XyyDataSeries<DateTime, double>>();
  98. if (begin == DateTime.MinValue)
  99. series.SeriesName = string.Format("{0} 所有运行时间统计", cham);
  100. else
  101. series.SeriesName = string.Format("{0} 最近1年运行时间统计", cham);
  102. var timeList = runtimeDic.Keys.ToList();
  103. timeList.Sort();
  104. if (timeList.Count > 0)
  105. {
  106. var s1 = timeList[0];
  107. var s2 = timeList[timeList.Count - 1];
  108. for (var day = s1; day <= s2; day+= new TimeSpan(1, 0, 0, 0))
  109. {
  110. if (runtimeDic.ContainsKey(day))
  111. series.Append(day, 0, runtimeDic[day]);
  112. else
  113. series.Append(day, 0, 0);
  114. }
  115. }
  116. }
  117. DataSet = dataset;
  118. _plot.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  119. _plot.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  120. InvokePropertyChanged();
  121. _plot.ZoomExtents();
  122. }
  123. #endif
  124. }
  125. }