123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using Avalonia.Collections;
- using Avalonia.Controls;
- using IViewModel.Models;
- using IViewModel.Tools;
- using IViewModel.ViewModels;
- using OxyPlot;
- using OxyPlot.Axes;
- using OxyPlot.Series;
- using Shaker.Model;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Dynamicloadsimulationdevice.ViewModels
- {
- internal sealed class AnalogSignalPreviewViewModel:DisplayViewModelBase,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 ResultChannelType 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 = LanguageValueViewModel.Instance.Time,
- Unit = "s",
- Key = "X",
- MajorGridlineStyle = OxyPlot.LineStyle.Solid,
- Position = OxyPlot.Axes.AxisPosition.Bottom
- });
- PlotModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
- {
- Title = LanguageValueViewModel.Instance.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)
- {
- 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(Topic.InitSeries).Subscrip((_, _) =>
- {
- lock(locker)
- {
- Menu.Clear();
- ShakerChannelViewModel.Instance.ResultChannels
- .DistinctBy(x=>x.Value.ChannelType)
- .ToList()
- .ForEach(x =>
- {
- Menu.Add(new TimeDomainMenuViewModel()
- {
- IsChecked = false,
- IsEnabled = true,
- AnalogType = x.Value.ChannelType,
- 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(IViewModel.ViewModels.LanguageViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
- {
- PlotModel.InvalidatePlot(false);
- PlotModel.Axes[0].Title = LanguageValueViewModel.Instance.Time;
- PlotModel.Axes[1].Title = LanguageValueViewModel.Instance.Ampt;
- UpdatePlotTitle(SelectedAnalog);
- var config = ShakerChannelViewModel.Instance.ResultChannels.FirstOrDefault(x => x.Value.ChannelType == SelectedAnalog);
- if (config == null) return;
- for(int i=0;i<PlotModel.Series.Count;i++)
- {
- PlotModel.Series[i].Title = LanguageValueViewModel.Instance[config.Value.Name];
- }
- });
- }
- [AllowNull]
- public Control Control { get; }
- private ResultChannelType lasttype = ResultChannelType.ActualDisplacement;
- private bool statisticsVisibily = true;
- private string attachTitle = string.Empty;
- private void UpdatePlotTitle(ResultChannelType analogType)
- {
- PlotModel.Title = string.IsNullOrEmpty(AttachTitle) ? LanguageValueViewModel.Instance[analogType.Description()] : $"{LanguageValueViewModel.Instance[AttachTitle]}-{LanguageValueViewModel.Instance[analogType.Description()]}";
- }
- public AvaloniaList<TimeDomainMenuViewModel> Menu { get; } = new AvaloniaList<TimeDomainMenuViewModel>();
- public AnalogSignalPreviewViewModel(ResultChannelType analogType,bool createcontrol = false):this(createcontrol)
- {
- lasttype = analogType;
- }
- public ResultChannelType SelectedAnalog
- {
- get => selectedAnalog;
- set
- {
- SetProperty(ref selectedAnalog, value);
- ChangeAnalogType(value);
- }
- }
- public bool UpSignalData { get; set; } = true;
- private void ChangeAnalogType(ResultChannelType 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;
- }
- if (ShakerChannelViewModel.Instance.ResultChannels.Count == 0) return;
- var config = ShakerChannelViewModel.Instance.ResultChannels.FirstOrDefault(x => x.Value.ChannelType == type);
- if (config == null) return;
- PlotModel.Axes[1].Unit = config.Value.Unit;
- var allconfig = ShakerChannelViewModel.Instance.ResultChannels.Where(x => x.Value.ChannelType == type).ToList();
- for (int i = 0; i < allconfig.Count; i++)
- {
- LineSeries series = new LineSeries();
- series.Title = LanguageValueViewModel.Instance[allconfig[i].Value.Name];
- series.XAxisKey = "X";
- series.YAxisKey = "Y";
- series.Tag = allconfig[i].Value.Unit;
- series.DataFieldX = nameof(DataPoint.X);
- series.DataFieldY = nameof(DataPoint.Y);
- Statistics.Add(new StatisticsViewModel(new StatisticsModel()
- {
- Name = allconfig[i].Value.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);
- }
- }
- }
|