123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using Avalonia.Collections;
- using Avalonia.Controls;
- using OxyPlot;
- using OxyPlot.Axes;
- using Shaker.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ShakerApp.Tools;
- using OxyPlot.Series;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- namespace ShakerApp.ViewModels
- {
- public class AnalogSignalPreviewViewModel:DisplayViewModelBase<IModel>,IDataPreview
- {
- public string AttachTitle
- {
- get => attachTitle;
- set
- {
- if (attachTitle == value) return;
- attachTitle = value;
- UpdatePlotTitle(SelectedAnalog);
- }
- }
- List<OxyPlot.Series.LineSeries> lineSeries = new List<OxyPlot.Series.LineSeries>();
- private object locker = new object();
- public AvaloniaList<ViewModels.StatisticsViewModel> Statistics { get; } = new AvaloniaList<StatisticsViewModel>();
- private AnalogType selectedAnalog;
- private bool canChangedAnalog = true;
- public bool StatisticsVisibily { get => statisticsVisibily; set =>SetProperty(ref statisticsVisibily , value); }
- public AnalogSignalPreviewViewModel(bool createcontrol = false):base()
- {
- Content = typeof(Views.AnalogSignalPreviewView);
- if(createcontrol)
- {
- Control = (Control)Activator.CreateInstance(Content)!;
- }
- PlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
- {
- MaximumPadding = 0,
- MinimumPadding = 0,
- Title = App.Current?.FindResource("Time") + "",
- Unit = "s",
- Key = "X",
- MajorGridlineStyle = OxyPlot.LineStyle.Solid,
- Position = OxyPlot.Axes.AxisPosition.Bottom
- });
- PlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
- {
- Title = App.Current?.FindResource("Ampt") + "",
- Unit = "V",
- Key = "Y",
- MajorGridlineStyle = OxyPlot.LineStyle.Solid,
- Position = OxyPlot.Axes.AxisPosition.Left,
- });
- UpdatePlotTitle(lasttype);
- PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
- {
- ShowInvisibleSeries = true,
- });
- PlotController.BindMouseWheel(OxyMouseWheelCommand);
- GetEvent(Topic.DATA).Subscrip((_, _) =>
- {
- if (!UpSignalData) return;
- lock(locker)
- {
- if(SelectedAnalog == AnalogType.Driver)
- {
- }
- var data = ShakerDataViewModel.Instance.GetAnalogData(SelectedAnalog);
- PlotModel.InvalidatePlot(false);
- for(int i=0;i<data.Count;i++)
- {
- lineSeries[i].ItemsSource = data[i].Item1;
- Statistics[i].UpDateModel(data[i].Item2);
- }
- PlotModel.InvalidatePlot(true);
- }
- });
- GetEvent(Models.Topic.InitSeries).Subscrip((_, _) =>
- {
- lock(locker)
- {
- Menu.Clear();
- ShakerConfigViewModel.Instance.AnalogSignals
- .DistinctBy(x=>x.Value.AnalogType)
- .ToList()
- .ForEach(x =>
- {
- Menu.Add(new TimeDomainMenuViewModel()
- {
- IsChecked = false,
- IsEnabled = true,
- AnalogType = x.Value.AnalogType,
- Unit = x.Value.Unit,
- });
- });
- if (CanChangedAnalog)
- {
- int index = Menu.ToList().FindIndex(x => x.AnalogType == lasttype);
- if (index == -1)
- {
- SelectedAnalog = Menu.First().AnalogType;
- }
- else
- {
- SelectedAnalog = lasttype;
- }
- }
- else
- {
- SelectedAnalog = lasttype;
- }
- }
- });
- GetEvent(ViewModels.ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
- {
- PlotModel.InvalidatePlot(false);
- PlotModel.Axes[0].Title = App.Current?.FindResource("Time") + "";
- PlotModel.Axes[1].Title = App.Current?.FindResource("Ampt") + "";
- UpdatePlotTitle(SelectedAnalog);
- var config = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.First(x => x.AnalogType == SelectedAnalog);
- for(int i=0;i<PlotModel.Series.Count;i++)
- {
- PlotModel.Series[i].Title = App.Current?.FindResource(config.Name) + "";
- }
- });
- }
- [AllowNull]
- public Control Control { get; }
- private AnalogType lasttype = AnalogType.Displacement;
- private bool statisticsVisibily = true;
- private string attachTitle = string.Empty;
- private void UpdatePlotTitle(AnalogType analogType)
- {
- PlotModel.Title = string.IsNullOrEmpty(AttachTitle) ? App.Current?.FindResource(analogType.Description()) + "" : $"{App.Current?.FindResource(AttachTitle)}-{App.Current?.FindResource(analogType.Description())}";
- }
- public AvaloniaList<TimeDomainMenuViewModel> Menu { get; } = new AvaloniaList<TimeDomainMenuViewModel>();
- public AnalogSignalPreviewViewModel(Shaker.Models.AnalogType analogType,bool createcontrol = false):this(createcontrol)
- {
- lasttype = analogType;
- }
- public AnalogType SelectedAnalog
- {
- get => selectedAnalog;
- set
- {
- SetProperty(ref selectedAnalog, value);
- ChangeAnalogType(value);
- }
- }
- public bool UpSignalData { get; set; } = true;
- private void ChangeAnalogType(AnalogType type)
- {
- lock (locker)
- {
- lineSeries.Clear();
- PlotModel.Series.Clear();
- Statistics.Clear();
- for(int i=0;i<Menu.Count;i++)
- {
- Menu[i].IsChecked = Menu[i].AnalogType == type;
- }
- var config = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.First(x => x.AnalogType == type);
- PlotModel.Axes[1].Unit = config.Unit;
- var allconfig = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Where(x => x.AnalogType == type).ToList();
- for (int i = 0; i < allconfig.Count; i++)
- {
- LineSeries series = new LineSeries();
- series.Title = App.Current?.FindResource(allconfig[i].Name) + "";
- series.XAxisKey = "X";
- series.YAxisKey = "Y";
- series.DataFieldX = nameof(DataPoint.X);
- series.DataFieldY = nameof(DataPoint.Y);
- Statistics.Add(new StatisticsViewModel(new Models.StatisticsModel()
- {
- Name = allconfig[i].Name,
- }));
- lineSeries.Add(series);
- }
- PlotModel.InvalidatePlot(false);
- UpdatePlotTitle(type);
- lineSeries.ForEach(x => PlotModel.Series.Add(x));
- PlotModel.InvalidatePlot(true);
- }
- }
- public bool CanChangedAnalog
- {
- get => canChangedAnalog;
- set =>SetProperty(ref canChangedAnalog , value);
- }
- public OxyPlot.PlotModel PlotModel { get; } = new OxyPlot.PlotModel();
- public OxyPlot.PlotController PlotController { get; } = new OxyPlot.PlotController();
- public IViewCommand<OxyMouseWheelEventArgs> OxyMouseWheelCommand => new DelegatePlotCommand<OxyMouseWheelEventArgs>(OnOxyMouseWheel);
- private void OnOxyMouseWheel(IPlotView view, IController controller, OxyMouseWheelEventArgs args)
- {
- HandleZoomByWheel(view, args);
- if (view.ActualModel is PlotModel plotModel && plotModel.Axes.Count >= 1 && plotModel.Axes[0] is LinearAxis axis)
- {
- axis.MajorStep = (axis.Maximum - axis.Minimum) / 10;
- }
- }
- private void HandleZoomByWheel(IPlotView view, OxyMouseWheelEventArgs args, double factor = 1)
- {
- var m = new ZoomStepManipulator(view)
- {
- AxisPreference = AxisPreference.X,
- Step = args.Delta * 0.001 * factor,
- FineControl = args.IsControlDown,
- };
- m.Started(args);
- }
- }
- }
|