PlotConfigViewModel.cs 4.2 KB

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