RandomTransferFunctionViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Avalonia.Controls;
  2. using CommunityToolkit.Mvvm.Input;
  3. using OxyPlot;
  4. using OxyPlot.Legends;
  5. using OxyPlot.Series;
  6. using Shaker.Models;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. namespace ShakerApp.ViewModels
  14. {
  15. internal class RandomTransferFunctionViewModel:DisplayViewModelBase<IModel>
  16. {
  17. public void InitTransferFunctionData(double[] transferFunction)
  18. {
  19. int start = (int)(RandomConfigViewModel.Instance.MinFrequency / RandomConfigViewModel.Instance.FrequencyResolution);
  20. int len = (int)(((double)RandomConfigViewModel.Instance.MaxFrequency - RandomConfigViewModel.Instance.MinFrequency) / RandomConfigViewModel.Instance.FrequencyResolution);
  21. List<DataPoint> dataPoints = new List<DataPoint>();
  22. for(int i=0;i<len;i++)
  23. {
  24. dataPoints.Add(new DataPoint((i+start)*RandomConfigViewModel.Instance.FrequencyResolution,transferFunction[ i+start]));
  25. }
  26. PlotModel.InvalidatePlot(false);
  27. (PlotModel.Series[0] as LineSeries)!.ItemsSource = dataPoints;
  28. PlotModel.InvalidatePlot(true);
  29. }
  30. private RandomTransferFunctionViewModel()
  31. {
  32. Content = typeof(Views.RandomTransferFunctionView);
  33. ButtonVisibily = false;
  34. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  35. {
  36. Title = App.Current?.FindResource("Frequency")+"",
  37. Unit = "Hz",
  38. Position = OxyPlot.Axes.AxisPosition.Bottom,
  39. MaximumPadding =0,
  40. MinimumPadding =0,
  41. MajorGridlineStyle = LineStyle.Solid,
  42. Key = "X",
  43. });
  44. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  45. {
  46. Title = App.Current?.FindResource("Ampt") + "",
  47. Position = OxyPlot.Axes.AxisPosition.Left,
  48. MajorGridlineStyle = LineStyle.Solid,
  49. MaximumPadding =0,
  50. MinimumPadding =0,
  51. Key = "Y",
  52. });
  53. PlotModel.Series.Add(new OxyPlot.Series.LineSeries()
  54. {
  55. Title = App.Current?.FindResource("TransferFunction") + "",
  56. XAxisKey = "X",
  57. YAxisKey = "Y",
  58. DataFieldX = nameof(DataPoint.X),
  59. DataFieldY = nameof(DataPoint.Y),
  60. });
  61. PlotModel.Title = App.Current?.FindResource("TransferFunction") + "";
  62. PlotModel.Legends.Add(new Legend()
  63. {
  64. ShowInvisibleSeries = true,
  65. });
  66. GetEvent(ViewModels.ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  67. {
  68. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  69. PlotModel.Axes[1].Title = App.Current?.FindResource("Ampt") + "";
  70. PlotModel.Series[0].Title = App.Current?.FindResource("TransferFunction") + "";
  71. PlotModel.Title = App.Current?.FindResource("TransferFunction") + "";
  72. });
  73. }
  74. static RandomTransferFunctionViewModel()
  75. {
  76. }
  77. public ICommand StartCommand=>new RelayCommand(Start);
  78. private void Start()
  79. {
  80. if (RandomConfigViewModel.Instance.PlanItems.Count == 0)
  81. {
  82. ShowError(App.Current?.FindResource("RandomPlanCountIsZero") + "");
  83. return;
  84. }
  85. if (RandomConfigViewModel.Instance.PlanItems.Sum(x => x.Value.Time) <= 0)
  86. {
  87. ShowError(App.Current?.FindResource("RandomPlanTimeIsZero") + "");
  88. return;
  89. }
  90. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STARTRANDOMTEST)?.Publish(this, null);
  91. CloseWindowAction?.Invoke();
  92. }
  93. protected override void Cancel()
  94. {
  95. CloseWindowAction?.Invoke();
  96. }
  97. public PlotModel PlotModel { get; } = new PlotModel();
  98. public static RandomTransferFunctionViewModel Instance { get; } = new RandomTransferFunctionViewModel();
  99. }
  100. }