PlotConfigViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using CommunityToolkit.Mvvm.Input;
  5. using OxyPlot;
  6. using OxyPlot.Series;
  7. using Shaker.Models;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Input;
  15. namespace ShakerApp.ViewModels
  16. {
  17. internal class PlotConfigViewModel:DisplayViewModelBase<IModel>
  18. {
  19. public override double Width => 750;
  20. public override double Height => 600;
  21. public override bool CanResize => false;
  22. public bool NoSeries { get => noSeries; set =>SetProperty(ref noSeries , value); }
  23. public AvaloniaList<SeriesConfigViewModel> SeriesConfigs { get; } = new AvaloniaList<SeriesConfigViewModel>();
  24. private PlotConfigViewModel()
  25. {
  26. Content = typeof(Views.PlotConfigView);
  27. Title = "PlotConfig";
  28. }
  29. static PlotConfigViewModel()
  30. {
  31. }
  32. public ICommand PlotConfigCommand => new RelayCommand<PlotModel>(InitPlot);
  33. [AllowNull]
  34. private PlotModel _PlotModel;
  35. private bool noSeries = false;
  36. public async void InitPlot(object? sender, PlotModel? model)
  37. {
  38. if(model ==null || sender is not Control control)
  39. {
  40. NoSeries = true;
  41. SaveIsEnabled = false;
  42. return;
  43. }
  44. _PlotModel = model;
  45. SeriesConfigs.Clear();
  46. var lists = _PlotModel.Series.OfType<LineSeries>().ToList();
  47. if (lists.Count == 0)
  48. {
  49. NoSeries = true;
  50. SaveIsEnabled = false;
  51. return;
  52. }
  53. NoSeries = false;
  54. SaveIsEnabled = true;
  55. lists.ForEach(x => SeriesConfigs.Add(new SeriesConfigViewModel(x)));
  56. base.InitData();
  57. BaseDialogWindow window = new BaseDialogWindow();
  58. window.DataContext = this;
  59. CloseWindowAction = () => window?.Close();
  60. await window.ShowDialog((Window)Window.GetTopLevel(control)!);
  61. }
  62. protected override void Save()
  63. {
  64. base.Save();
  65. var lists = _PlotModel.Series.OfType<LineSeries>().ToList();
  66. if (lists.Count == 0) return;
  67. _PlotModel.InvalidatePlot(false);
  68. try
  69. {
  70. /*
  71. * 关联的曲线可能由于后台通信指令重新加载
  72. */
  73. for(int i=0;i<Math.Min(lists.Count,SeriesConfigs.Count);i++)
  74. {
  75. lists[i].Color = OxyColor.FromUInt32(SeriesConfigs[i].Color.ToUInt32());
  76. lists[i].StrokeThickness = SeriesConfigs[i].StrokeThickness;
  77. lists[i].LineStyle = SeriesConfigs[i].LineStyle;
  78. lists[i].MarkerType = SeriesConfigs[i].MarkerType;
  79. }
  80. }
  81. catch
  82. {
  83. }
  84. _PlotModel.InvalidatePlot(true);
  85. }
  86. public static PlotConfigViewModel Instance { get; } = new PlotConfigViewModel();
  87. }
  88. internal class SeriesConfigViewModel : ViewModelBase
  89. {
  90. private Color color;
  91. private LineStyle lineStyle;
  92. private MarkerType markerType;
  93. private double strokeThickness;
  94. public SeriesConfigViewModel(LineSeries series)
  95. {
  96. Color = new Color(series.ActualColor.A, series.ActualColor.R, series.ActualColor.G, series.ActualColor.B);
  97. LineStyle = series.LineStyle;
  98. MarkerType = series.MarkerType;
  99. StrokeThickness = series.StrokeThickness;
  100. Name = series.Title;
  101. }
  102. public string Name { get; }
  103. public Avalonia.Media.Color Color { get => color; set =>SetProperty(ref color , value); }
  104. public LineStyle LineStyle { get => lineStyle; set =>SetProperty(ref lineStyle , value); }
  105. public MarkerType MarkerType { get => markerType; set =>SetProperty(ref markerType , value); }
  106. public double StrokeThickness { get => strokeThickness; set =>SetProperty(ref strokeThickness , value); }
  107. }
  108. }