123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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;
- namespace ShakerApp.ViewModels
- {
- public class AnalogSignalPreviewViewModel:ViewModelBase<IModel>,IDataPreview
- {
- List<OxyPlot.Series.LineSeries> lineSeries = new List<OxyPlot.Series.LineSeries>();
- private object locker = new object();
- public AvaloniaList<KeyValuePair<string, AnalogType>> AllAnalogTypes { get; } = new AvaloniaList<KeyValuePair<string, AnalogType>>();
- 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():base()
- {
- Content = typeof(Views.AnalogSignalPreviewView);
- 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,
- });
- PlotModel.Title = App.Current?.FindResource("ValveDriveSignal") + "";
- 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)
- {
- AllAnalogTypes.Clear();
- ShakerConfigViewModel.Instance.AnalogSignals.ToList()
- .ForEach(x =>
- {
- AllAnalogTypes.Add(new KeyValuePair<string, AnalogType>(x.Value.AnalogType.Description(), x.Value.AnalogType));
- });
- if (CanChangedAnalog)
- {
- int index = AllAnalogTypes.ToList().FindIndex(x => x.Value == lasttype);
- if (index == -1)
- {
- SelectedAnalog = AllAnalogTypes.First().Value;
- }
- else
- {
- SelectedAnalog = lasttype;
- }
- }
- else
- {
- SelectedAnalog = lasttype;
- }
- }
- });
- }
- private AnalogType lasttype = AnalogType.Displacement;
- private bool statisticsVisibily = true;
- public AnalogSignalPreviewViewModel(Shaker.Models.AnalogType analogType):this()
- {
- 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();
- var config = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.First(x => x.AnalogType == type);
- PlotModel.Axes[1].Unit = config.Unit;
- int count = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Count(x => x.AnalogType == type);
- var data = ShakerDataViewModel.Instance.GetAnalogData(type);
- for(int i=0;i<count;i++)
- {
- LineSeries series = new LineSeries();
- series.Title = App.Current?.FindResource(config.Name) + "";
- series.XAxisKey = "X";
- series.YAxisKey = "Y";
- series.DataFieldX = nameof(DataPoint.X);
- series.DataFieldY = nameof(DataPoint.Y);
- if(data.Count==count)
- {
- Statistics.Add(new StatisticsViewModel(data[i].Item2));
- series.ItemsSource = data[i].Item1;
- }
- else
- {
- Statistics.Add(new StatisticsViewModel());
- }
- lineSeries.Add(series);
- }
- PlotModel.InvalidatePlot(false);
- PlotModel.Title = App.Current?.FindResource(type.Description()) + "";
- 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);
- }
- }
- }
|