123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using Avalonia.Collections;
- using Avalonia.Controls;
- using Avalonia.Media;
- using CommunityToolkit.Mvvm.Input;
- using OxyPlot;
- using OxyPlot.Series;
- using Shaker.Models;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace ShakerApp.ViewModels
- {
- internal class PlotConfigViewModel:DisplayViewModelBase<IModel>
- {
- public override double Width => 750;
- public override double Height => 600;
- public override bool CanResize => false;
- public bool NoSeries { get => noSeries; set =>SetProperty(ref noSeries , value); }
- public AvaloniaList<SeriesConfigViewModel> SeriesConfigs { get; } = new AvaloniaList<SeriesConfigViewModel>();
- private PlotConfigViewModel()
- {
- Content = typeof(Views.PlotConfigView);
- Title = "PlotConfig";
- }
- static PlotConfigViewModel()
- {
- }
- public ICommand PlotConfigCommand => new RelayCommand<PlotModel>(InitPlot);
- [AllowNull]
- private PlotModel _PlotModel;
- private bool noSeries = false;
- public async void InitPlot(object? sender, PlotModel? model)
- {
- if(model ==null || sender is not Control control)
- {
- NoSeries = true;
- SaveIsEnabled = false;
- return;
- }
- _PlotModel = model;
- SeriesConfigs.Clear();
- var lists = _PlotModel.Series.OfType<LineSeries>().ToList();
- if (lists.Count == 0)
- {
- NoSeries = true;
- SaveIsEnabled = false;
- return;
- }
- NoSeries = false;
- SaveIsEnabled = true;
- lists.ForEach(x => SeriesConfigs.Add(new SeriesConfigViewModel(x)));
- base.InitData();
- BaseDialogWindow window = new BaseDialogWindow();
- window.DataContext = this;
- CloseWindowAction = () => window?.Close();
- await window.ShowDialog((Window)Window.GetTopLevel(control)!);
- }
- protected override void Save()
- {
- base.Save();
- var lists = _PlotModel.Series.OfType<LineSeries>().ToList();
- if (lists.Count == 0) return;
- _PlotModel.InvalidatePlot(false);
- try
- {
- /*
- * 关联的曲线可能由于后台通信指令重新加载
- */
- for(int i=0;i<Math.Min(lists.Count,SeriesConfigs.Count);i++)
- {
- lists[i].Color = OxyColor.FromUInt32(SeriesConfigs[i].Color.ToUInt32());
- lists[i].StrokeThickness = SeriesConfigs[i].StrokeThickness;
- lists[i].LineStyle = SeriesConfigs[i].LineStyle;
- lists[i].MarkerType = SeriesConfigs[i].MarkerType;
- }
- }
- catch
- {
- }
- _PlotModel.InvalidatePlot(true);
- }
- public static PlotConfigViewModel Instance { get; } = new PlotConfigViewModel();
- }
- internal class SeriesConfigViewModel : ViewModelBase
- {
- private Color color;
- private LineStyle lineStyle;
- private MarkerType markerType;
- private double strokeThickness;
- public SeriesConfigViewModel(LineSeries series)
- {
- Color = new Color(series.ActualColor.A, series.ActualColor.R, series.ActualColor.G, series.ActualColor.B);
- LineStyle = series.LineStyle;
- MarkerType = series.MarkerType;
- StrokeThickness = series.StrokeThickness;
- Name = series.Title;
- }
- public string Name { get; }
- public Avalonia.Media.Color Color { get => color; set =>SetProperty(ref color , value); }
- public LineStyle LineStyle { get => lineStyle; set =>SetProperty(ref lineStyle , value); }
- public MarkerType MarkerType { get => markerType; set =>SetProperty(ref markerType , value); }
- public double StrokeThickness { get => strokeThickness; set =>SetProperty(ref strokeThickness , value); }
- }
- }
|