123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Avalonia.Controls;
- using CommunityToolkit.Mvvm.Input;
- using OxyPlot;
- using OxyPlot.Legends;
- using OxyPlot.Series;
- using Shaker.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace ShakerApp.ViewModels
- {
- internal class RandomTransferFunctionViewModel:DisplayViewModelBase<IModel>
- {
- public void InitTransferFunctionData(double[] transferFunction)
- {
- int start = (int)(RandomConfigViewModel.Instance.MinFrequency / RandomConfigViewModel.Instance.FrequencyResolution);
- int len = (int)(((double)RandomConfigViewModel.Instance.MaxFrequency - RandomConfigViewModel.Instance.MinFrequency) / RandomConfigViewModel.Instance.FrequencyResolution);
- List<DataPoint> dataPoints = new List<DataPoint>();
- for(int i=0;i<len;i++)
- {
- dataPoints.Add(new DataPoint((i+start)*RandomConfigViewModel.Instance.FrequencyResolution,transferFunction[ i+start]));
- }
- PlotModel.InvalidatePlot(false);
- (PlotModel.Series[0] as LineSeries)!.ItemsSource = dataPoints;
- PlotModel.InvalidatePlot(true);
- }
- private RandomTransferFunctionViewModel()
- {
- Content = typeof(Views.RandomTransferFunctionView);
- ButtonVisibily = false;
- PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
- {
- Title = App.Current?.FindResource("Frequency")+"",
- Unit = "Hz",
- Position = OxyPlot.Axes.AxisPosition.Bottom,
- MaximumPadding =0,
- MinimumPadding =0,
- MajorGridlineStyle = LineStyle.Solid,
- Key = "X",
- });
- PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
- {
- Title = App.Current?.FindResource("Ampt") + "",
- Position = OxyPlot.Axes.AxisPosition.Left,
- MajorGridlineStyle = LineStyle.Solid,
- MaximumPadding =0,
- MinimumPadding =0,
- Key = "Y",
- });
- PlotModel.Series.Add(new OxyPlot.Series.LineSeries()
- {
- Title = App.Current?.FindResource("TransferFunction") + "",
- XAxisKey = "X",
- YAxisKey = "Y",
- DataFieldX = nameof(DataPoint.X),
- DataFieldY = nameof(DataPoint.Y),
- });
- PlotModel.Title = App.Current?.FindResource("TransferFunction") + "";
- PlotModel.Legends.Add(new Legend()
- {
- ShowInvisibleSeries = true,
- });
- GetEvent(ViewModels.ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
- {
- PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
- PlotModel.Axes[1].Title = App.Current?.FindResource("Ampt") + "";
- PlotModel.Series[0].Title = App.Current?.FindResource("TransferFunction") + "";
- PlotModel.Title = App.Current?.FindResource("TransferFunction") + "";
- });
- }
- static RandomTransferFunctionViewModel()
- {
- }
- public ICommand StartCommand=>new RelayCommand(Start);
- private void Start()
- {
- if (RandomConfigViewModel.Instance.PlanItems.Count == 0)
- {
- ShowError(App.Current?.FindResource("RandomPlanCountIsZero") + "");
- return;
- }
- if (RandomConfigViewModel.Instance.PlanItems.Sum(x => x.Value.Time) <= 0)
- {
- ShowError(App.Current?.FindResource("RandomPlanTimeIsZero") + "");
- return;
- }
- CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STARTRANDOMTEST)?.Publish(this, null);
- CloseWindowAction?.Invoke();
- }
- protected override void Cancel()
- {
- CloseWindowAction?.Invoke();
- }
- public PlotModel PlotModel { get; } = new PlotModel();
- public static RandomTransferFunctionViewModel Instance { get; } = new RandomTransferFunctionViewModel();
- }
- }
|