SineMainPageViewModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using OxyPlot;
  4. using OxyPlot.Series;
  5. using Shaker.Model;
  6. using Shaker.Model.Tools;
  7. using Shaker.Models;
  8. using ShakerApp.Tools;
  9. using ShakerApp.Views;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace ShakerApp.ViewModels
  16. {
  17. internal class SineMainPageViewModel : ViewModelBase<SineDataModel>, IMainPageViewModel
  18. {
  19. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  20. List<LineStyle> lineStyles = new List<LineStyle>() {LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  21. List<string> properties = new List<string>() {nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  22. List<SweepData> datas = new List<SweepData>();
  23. OxyPlot.Annotations.ArrowAnnotation arrowAnnotation = new OxyPlot.Annotations.ArrowAnnotation();
  24. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  25. private SineMainPageViewModel()
  26. {
  27. #region 加速度谱曲线
  28. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  29. {
  30. MaximumPadding = 0,
  31. MinimumPadding = 0,
  32. Title = App.Current?.FindResource(Shaker.Model.ShakerConstant.FrequencyKey) + "",
  33. Unit = "Hz",
  34. MajorGridlineStyle = LineStyle.Solid,
  35. Position = OxyPlot.Axes.AxisPosition.Bottom,
  36. Key = "Freq"
  37. });
  38. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  39. {
  40. Key = "Ampt",
  41. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  42. Unit = "g",
  43. MajorGridlineStyle = LineStyle.Solid,
  44. Position = OxyPlot.Axes.AxisPosition.Left,
  45. });
  46. PlotModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  47. for (int i = 0; i < oxyColors.Count; i++)
  48. {
  49. LineSeries line = new LineSeries();
  50. line.Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  51. line.Color = oxyColors[i];
  52. line.StrokeThickness = 1;
  53. line.DataFieldX = nameof(SweepData.Frequency);
  54. line.DataFieldY = properties[i];
  55. line.LineStyle = lineStyles[i];
  56. line.XAxisKey = "Freq";
  57. line.YAxisKey = "Ampt";
  58. LineSeries.Add(line);
  59. PlotModel.Series.Add(line);
  60. }
  61. PlotModel.Annotations.Add(arrowAnnotation);
  62. arrowAnnotation.Selectable = false;
  63. arrowAnnotation.SelectionMode = OxyPlot.SelectionMode.Single;
  64. arrowAnnotation.Text = "Test";
  65. arrowAnnotation.TextColor = OxyColors.Transparent;
  66. arrowAnnotation.StartPoint = new DataPoint(8, 4);
  67. arrowAnnotation.EndPoint = new DataPoint();
  68. arrowAnnotation.Color = OxyColors.Transparent;
  69. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  70. {
  71. ShowInvisibleSeries = true,
  72. });
  73. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  74. #endregion
  75. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  76. {
  77. PlotModel.InvalidatePlot(false);
  78. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  79. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  80. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  81. for(int i=0;i<LineSeries.Count;i++)
  82. {
  83. LineSeries[i].Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  84. }
  85. PlotModel.InvalidatePlot(true);
  86. });
  87. }
  88. static SineMainPageViewModel()
  89. {
  90. }
  91. public void SetRefSpectrum(List<SweepData> data)
  92. {
  93. datas.Clear();
  94. datas.AddRange(data);
  95. PlotModel.InvalidatePlot(false);
  96. //LineSeries[0].IsVisible = false;
  97. for(int i=0;i<LineSeries.Count;i++)
  98. {
  99. LineSeries[i].ItemsSource = datas;
  100. }
  101. PlotModel.InvalidatePlot(true);
  102. }
  103. public void Start()
  104. {
  105. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  106. }
  107. public void Stop()
  108. {
  109. }
  110. public void UpdateData(IResultDataModel model)
  111. {
  112. if (model is SineDataModel sine)
  113. {
  114. if(sine.SweepDirection != Model.SweepDirection)
  115. {
  116. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  117. }
  118. UpDateModel(sine);
  119. uint targetacc = (uint)sine.CurrentAcceleration * 1000_1000;
  120. int index = -1;
  121. for (int i = 0; i < datas.Count; i++)
  122. {
  123. uint tempacc = (uint)(datas[i].Acceleration * 1000_000);
  124. if (tempacc == targetacc)
  125. {
  126. index = i;
  127. datas[i].Acceleration = sine.CurrentAcceleration;
  128. UpPlot();
  129. return;
  130. }
  131. else if (tempacc > targetacc)
  132. {
  133. if (i > 0)
  134. {
  135. index = i - 1;
  136. }
  137. else index = 0;
  138. break;
  139. }
  140. }
  141. if (index == -1) index = datas.Count - 1;
  142. float value = 0; float upstop = 0; float upwarn = 0; float downstop = 0; float downwarn = 0;
  143. SweepConfigViewModel.Instance.Model.CalcAmpt(sine.CurrentFrequency, ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  144. datas.Insert(index + 1, new SweepData()
  145. {
  146. Acceleration = sine.CurrentAcceleration,
  147. Frequency = sine.CurrentFrequency,
  148. TargetAcceleration = value,
  149. UpStopAcceleration = upstop,
  150. UpWarnAcceleration = upwarn,
  151. DownStopAcceleration = downstop,
  152. DownWarnAcceleration = downwarn,
  153. });
  154. UpPlot();
  155. }
  156. }
  157. private void UpPlot()
  158. {
  159. PlotModel.InvalidatePlot(false);
  160. arrowAnnotation.TextColor = OxyColors.Black;
  161. arrowAnnotation.Color = OxyColors.Green;
  162. arrowAnnotation.EndPoint = new DataPoint(CurrentFrequency, CurrentAcceleration);
  163. arrowAnnotation.StartPoint = new DataPoint(CurrentFrequency, 1E-30);
  164. arrowAnnotation.Text = $"{App.Current?.FindResource("Frequency")}:{CurrentFrequency:F2}Hz\r\n{App.Current?.FindResource("Acceleration")}:{CurrentAcceleration:F3}g";
  165. arrowAnnotation.TextPosition = arrowAnnotation.EndPoint;
  166. arrowAnnotation.TextHorizontalAlignment = HorizontalAlignment.Left;
  167. arrowAnnotation.TextVerticalAlignment = VerticalAlignment.Top;
  168. for(int i=0;i<LineSeries.Count;i++)
  169. {
  170. LineSeries[i].ItemsSource = datas;
  171. }
  172. PlotModel.InvalidatePlot(true);
  173. }
  174. public float TotalTime { get => Model.TotalTime; set => SetProperty(ref Model.TotalTime, value); }
  175. public float RunTime { get => Model.RunTime; set => SetProperty(ref Model.RunTime, value); }
  176. public float CurrentFrequency { get => Model.CurrentFrequency; set => SetProperty(ref Model.CurrentFrequency, value); }
  177. public float CurrentAcceleration { get => Model.CurrentAcceleration; set => SetProperty(ref Model.CurrentAcceleration, value); }
  178. public SweepDirection SweepDirection { get => Model.SweepDirection; set => SetProperty(ref Model.SweepDirection, value); }
  179. public uint SweepIndex { get => Model.SweepIndex; set => SetProperty(ref Model.SweepIndex, value); }
  180. public MainPageType PageType => Model.MainPageType;
  181. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  182. public static SineMainPageViewModel Instance { get; } = new SineMainPageViewModel();
  183. public OxyPlot.PlotModel DriverModel { get; private set; } = new PlotModel();
  184. }
  185. }