SingleTracePlotViewModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using ScottPlot;
  2. using ScottPlot.WPF;
  3. namespace ProximaAnalizer.ViewModels.Dialog;
  4. internal partial class SingleTracePlotViewModel : ObservableObject, IDialogAware
  5. {
  6. public SingleTracePlotViewModel(DBDataHelper dBDataHelper)
  7. {
  8. this._dBDataHelper = dBDataHelper;
  9. this.PlotControl = new();
  10. this._plotHepler = new(this.PlotControl);
  11. this._plotHepler.InitPlot();
  12. }
  13. [ObservableProperty]
  14. private string _Title = string.Empty;
  15. private string _BaseTitle = string.Empty;
  16. private readonly PlotHepler _plotHepler;
  17. private readonly DBDataHelper _dBDataHelper;
  18. public DialogCloseListener RequestClose { get; set; }
  19. [ObservableProperty]
  20. private WpfPlot _PlotControl;
  21. [ObservableProperty]
  22. private ObservableCollection<EventData> _events = [];
  23. [ObservableProperty]
  24. private string? _Hint;
  25. [ObservableProperty]
  26. private Visibility _alarmVis = Visibility.Visible;
  27. [ObservableProperty]
  28. private Visibility _tabVis = Visibility.Visible;
  29. public bool CanCloseDialog()
  30. {
  31. return true;
  32. }
  33. public void OnDialogClosed()
  34. {
  35. }
  36. [ObservableProperty]
  37. private ObservableCollection<string> _Tabs = [];
  38. [RelayCommand]
  39. private void SelectTab(string para)
  40. {
  41. if (!string.Equals(para, this._BaseTitle))
  42. this.Title = $"{_BaseTitle} - {para}";
  43. else
  44. this.Title = _BaseTitle;
  45. this.PlotControl.Plot.Clear();
  46. if (this._DbKeys is null)
  47. return;
  48. List<DateTime> time = [];
  49. List<float> data = [];
  50. string? key;
  51. switch (this._DbKeys)
  52. {
  53. case Dictionary<string, string> shortKey:
  54. if (!shortKey.TryGetValue(this._BaseTitle, out key) || string.IsNullOrEmpty(key))
  55. return;
  56. break;
  57. case Dictionary<string, Dictionary<string, string>> longKey:
  58. if (!longKey.TryGetValue(this._BaseTitle, out Dictionary<string, string>? lists) || lists is null)
  59. return;
  60. if (!lists.TryGetValue(para, out key) || string.IsNullOrEmpty(key))
  61. return;
  62. break;
  63. default:
  64. return;
  65. }
  66. if (!_dBDataHelper.GetPMData(["time", key], out List<dynamic>? rawData) || rawData is null || rawData.Count == 0)
  67. {
  68. if (!_dBDataHelper.GetSystemData(["time", key], out rawData) || rawData is null || rawData.Count == 0)
  69. return;
  70. }
  71. this.Hint = string.Empty;
  72. foreach (var item in rawData)
  73. {
  74. if (item is not IDictionary<string, object> contents)
  75. continue;
  76. if (string.IsNullOrEmpty(this.Hint) && contents[key] is bool b)
  77. this.Hint = $"True: {Convert.ToSingle(true):0} False: {Convert.ToSingle(false):0}";
  78. time.Add(new DateTime((long)contents["time"]));
  79. data.Add(Convert.ToSingle(contents[key]));
  80. }
  81. this._plotHepler.AddLeftLine(time, data, LinePattern.Solid, MarkerStyle.Default, 1, "1E90FF");
  82. if (Events is not null)
  83. foreach (var item in Events)
  84. {
  85. switch (item.Level)
  86. {
  87. case "Alarm":
  88. this._plotHepler.AddAlarmLine(item.Occur_Time, item.Source!);
  89. break;
  90. case "Warning":
  91. this._plotHepler.AddWarningLine(item.Occur_Time, item.Source!);
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. this.PlotControl.Plot.Axes.AutoScale();
  98. this.PlotControl.Refresh();
  99. }
  100. private object? _DbKeys;
  101. private DateTime _start;
  102. private DateTime _end;
  103. public void OnDialogOpened(IDialogParameters parameters)
  104. {
  105. if (!parameters.TryGetValue("point", out object? obj) || obj is null)
  106. return;
  107. if (!parameters.TryGetValue("time", out DateTime? time) || time is null)
  108. return;
  109. if (!parameters.TryGetValue("keys", out object? keys) || keys is null)
  110. return;
  111. this.Tabs.Clear();
  112. this._DbKeys = keys;
  113. switch (obj)
  114. {
  115. case KeyValuePair<string, object> singleType:
  116. this.Title = singleType.Key;
  117. this._BaseTitle = singleType.Key;
  118. this.Tabs.Add(singleType.Key);
  119. break;
  120. case KeyValuePair<string, ObservableDictionary<string, object>> doubleType:
  121. this.Title = doubleType.Key;
  122. this._BaseTitle = doubleType.Key;
  123. foreach (var item in doubleType.Value)
  124. this.Tabs.Add(item.Key);
  125. break;
  126. default:
  127. break;
  128. }
  129. _start = time.Value.AddMinutes(-1);
  130. _end = time.Value.AddMinutes(1);
  131. this._dBDataHelper.SetTimeRange(_start, _end);
  132. if (!this._dBDataHelper.GetAlarmData(out List<EventData>? alarms) || alarms is null || alarms.Count == 0)
  133. this.AlarmVis = Visibility.Collapsed;
  134. else
  135. this.Events.AddRange(alarms);
  136. if (this.Tabs.Count == 1)
  137. {
  138. this.SelectTab(this.Tabs.First());
  139. this.TabVis = Visibility.Collapsed;
  140. }
  141. }
  142. }