HisProgressBar.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Windows.Media;
  2. namespace HistoryView.Controls;
  3. /// <summary>
  4. /// Interaction logic for HisProgressBar.xaml
  5. /// </summary>
  6. public partial class HisProgressBar : UserControl
  7. {
  8. public HisProgressBar()
  9. {
  10. InitializeComponent();
  11. }
  12. private BarData? _current;
  13. public BarData? CurrentData
  14. {
  15. get
  16. {
  17. lock (this)
  18. return _current;
  19. }
  20. set
  21. {
  22. lock (this)
  23. {
  24. _current = value;
  25. if (value is null)
  26. return;
  27. App.Current.Dispatcher.Invoke(() =>
  28. {
  29. for (int i = 0; i < RealtimeData.Count; i++)
  30. {
  31. if (i == RealtimeData.Count - 1)
  32. {
  33. RealtimeData[i].Value = value.Value < 3 ? 3 : value.Value;
  34. RealtimeData[i].Bursh = value.Bursh;
  35. break;
  36. }
  37. RealtimeData[i].Value = RealtimeData[i + 1].Value;
  38. RealtimeData[i].Bursh = RealtimeData[i + 1].Bursh;
  39. }
  40. });
  41. }
  42. }
  43. }
  44. public int RefreshRate
  45. {
  46. get { return (int)GetValue(RefreshRateProperty); }
  47. set { SetValue(RefreshRateProperty, value); }
  48. }
  49. public static readonly DependencyProperty RefreshRateProperty =
  50. DependencyProperty.Register("RefreshRate", typeof(int), typeof(HisProgressBar), new PropertyMetadata(1000));
  51. private ObservableCollection<BarData> RealtimeData
  52. {
  53. get { return (ObservableCollection<BarData>)GetValue(RealtimeDataProperty); }
  54. set { SetValue(RealtimeDataProperty, value); }
  55. }
  56. public static readonly DependencyProperty RealtimeDataProperty =
  57. DependencyProperty.Register("RealtimeData", typeof(ObservableCollection<BarData>), typeof(HisProgressBar), new PropertyMetadata(new ObservableCollection<BarData>()));
  58. public double CurrentValue
  59. {
  60. get { return (double)GetValue(CurrentValueProperty); }
  61. set { SetValue(CurrentValueProperty, value); }
  62. }
  63. public static readonly DependencyProperty CurrentValueProperty =
  64. DependencyProperty.Register("CurrentValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(0d, PropertyChangedCallback));
  65. private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  66. {
  67. if (d is not HisProgressBar info)
  68. return;
  69. //double value = Math.Round(Convert.ToDouble(e.NewValue) / info.MaxValue, 1);
  70. lock (info)
  71. info.CurrentData = new((double)e.NewValue, info.WarningValue, info.AlarmValue);
  72. if (info.RealtimeData is null || info.RealtimeData.Count == 0)
  73. {
  74. info.RealtimeData ??= [];
  75. var width = info.Width / 3;
  76. for (int i = 0; i < width; i++)
  77. info.RealtimeData.Add(new(1, info.WarningValue, info.AlarmValue));
  78. }
  79. }
  80. public double MaxValue
  81. {
  82. get { return (double)GetValue(MaxValueProperty); }
  83. set { SetValue(MaxValueProperty, value); }
  84. }
  85. public static readonly DependencyProperty MaxValueProperty =
  86. DependencyProperty.Register("MaxValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(100d));
  87. public double WarningValue
  88. {
  89. get { return (double)GetValue(WarningValueProperty); }
  90. set { SetValue(WarningValueProperty, value); }
  91. }
  92. public static readonly DependencyProperty WarningValueProperty =
  93. DependencyProperty.Register("WarningValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(25d));
  94. public double AlarmValue
  95. {
  96. get { return (double)GetValue(AlarmValueProperty); }
  97. set { SetValue(AlarmValueProperty, value); }
  98. }
  99. public static readonly DependencyProperty AlarmValueProperty =
  100. DependencyProperty.Register("AlarmValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(75d));
  101. }
  102. public partial class BarData : ObservableObject
  103. {
  104. public BarData(double value, double warningValue, double alarmValue)
  105. {
  106. string hexString = "A0";
  107. this.Value = value;
  108. this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}32CD32"));
  109. if (value > alarmValue || value < warningValue)
  110. {
  111. this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}FF4040"));
  112. return;
  113. }
  114. //if (value >= warningValue)
  115. //{
  116. // this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}FF8C00"));
  117. // return;
  118. //}
  119. }
  120. [ObservableProperty]
  121. private double _value;
  122. [ObservableProperty]
  123. private SolidColorBrush _bursh;
  124. }