|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Specialized; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Media; |
| 7 | +using AR.Iec61850.SvPublisher.ViewModels; |
| 8 | + |
| 9 | +namespace AR.Iec61850.SvPublisher.Controls; |
| 10 | + |
| 11 | +public sealed class StateTimelinePlot : FrameworkElement |
| 12 | +{ |
| 13 | + public static readonly DependencyProperty SequenceStatesProperty = |
| 14 | + DependencyProperty.Register( |
| 15 | + nameof(SequenceStates), |
| 16 | + typeof(IEnumerable), |
| 17 | + typeof(StateTimelinePlot), |
| 18 | + new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, OnSequenceStatesChanged)); |
| 19 | + |
| 20 | + public IEnumerable? SequenceStates |
| 21 | + { |
| 22 | + get => (IEnumerable?)GetValue(SequenceStatesProperty); |
| 23 | + set => SetValue(SequenceStatesProperty, value); |
| 24 | + } |
| 25 | + |
| 26 | + private static void OnSequenceStatesChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) |
| 27 | + { |
| 28 | + var plot = (StateTimelinePlot)dependencyObject; |
| 29 | + plot.Detach(e.OldValue as IEnumerable); |
| 30 | + plot.Attach(e.NewValue as IEnumerable); |
| 31 | + plot.InvalidateVisual(); |
| 32 | + } |
| 33 | + |
| 34 | + protected override void OnRender(DrawingContext dc) |
| 35 | + { |
| 36 | + base.OnRender(dc); |
| 37 | + var width = ActualWidth; |
| 38 | + var height = ActualHeight; |
| 39 | + if (width < 100 || height < 80) |
| 40 | + return; |
| 41 | + |
| 42 | + dc.DrawRoundedRectangle(new SolidColorBrush(Color.FromRgb(250, 250, 250)), null, new Rect(0, 0, width, height), 6, 6); |
| 43 | + var states = GetStates().ToArray(); |
| 44 | + if (states.Length == 0) |
| 45 | + { |
| 46 | + DrawText(dc, "No states", new Point(16, 16), 12, Color.FromRgb(100, 116, 139)); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + var total = states.Sum(s => Math.Max(0.001, s.DurationSeconds)); |
| 51 | + var plot = new Rect(66, 36, Math.Max(10, width - 86), Math.Max(10, height - 72)); |
| 52 | + var gridPen = new Pen(new SolidColorBrush(Color.FromRgb(226, 232, 240)), 1); |
| 53 | + var axisPen = new Pen(new SolidColorBrush(Color.FromRgb(30, 41, 59)), 1); |
| 54 | + dc.DrawLine(axisPen, new Point(plot.Left, plot.Bottom), new Point(plot.Right, plot.Bottom)); |
| 55 | + dc.DrawLine(axisPen, new Point(plot.Left, plot.Top), new Point(plot.Left, plot.Bottom)); |
| 56 | + for (var i = 1; i <= 4; i++) |
| 57 | + { |
| 58 | + var y = plot.Top + plot.Height * i / 4.0; |
| 59 | + dc.DrawLine(gridPen, new Point(plot.Left, y), new Point(plot.Right, y)); |
| 60 | + } |
| 61 | + |
| 62 | + var cursor = 0.0; |
| 63 | + var phasePens = new[] |
| 64 | + { |
| 65 | + new Pen(new SolidColorBrush(Color.FromRgb(220, 38, 38)), 1.1), |
| 66 | + new Pen(new SolidColorBrush(Color.FromRgb(217, 119, 6)), 1.1), |
| 67 | + new Pen(new SolidColorBrush(Color.FromRgb(37, 99, 235)), 1.1) |
| 68 | + }; |
| 69 | + |
| 70 | + foreach (var state in states) |
| 71 | + { |
| 72 | + var startX = plot.Left + cursor / total * plot.Width; |
| 73 | + cursor += Math.Max(0.001, state.DurationSeconds); |
| 74 | + var endX = plot.Left + cursor / total * plot.Width; |
| 75 | + dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(26, 59, 130, 246)), null, new Rect(startX, plot.Top, Math.Max(1, endX - startX), plot.Height)); |
| 76 | + dc.DrawLine(new Pen(new SolidColorBrush(Color.FromRgb(100, 116, 139)), 1), new Point(startX, plot.Top - 18), new Point(startX, plot.Bottom)); |
| 77 | + DrawText(dc, state.Name, new Point(startX + 4, plot.Top - 30), 11, Color.FromRgb(30, 41, 59)); |
| 78 | + |
| 79 | + for (var phase = 0; phase < 3; phase++) |
| 80 | + DrawSine(dc, startX, endX, plot, phasePens[phase], state.VoltageScale, phase * 120 + state.AngleShiftDegrees); |
| 81 | + } |
| 82 | + |
| 83 | + DrawText(dc, "V / I preview", new Point(10, plot.Top + 4), 11, Color.FromRgb(30, 41, 59)); |
| 84 | + DrawText(dc, total.ToString("0.000", CultureInfo.InvariantCulture) + " s", new Point(plot.Right - 48, plot.Bottom + 10), 11, Color.FromRgb(30, 41, 59)); |
| 85 | + } |
| 86 | + |
| 87 | + private static void DrawSine(DrawingContext dc, double startX, double endX, Rect plot, Pen pen, double scale, double phaseDegrees) |
| 88 | + { |
| 89 | + if (endX - startX < 2) |
| 90 | + return; |
| 91 | + |
| 92 | + var geometry = new StreamGeometry(); |
| 93 | + using (var context = geometry.Open()) |
| 94 | + { |
| 95 | + var amplitude = Math.Clamp(scale, 0.05, 4.0) / 4.0 * plot.Height * 0.42; |
| 96 | + var centerY = plot.Top + plot.Height / 2.0; |
| 97 | + var phase = phaseDegrees * Math.PI / 180.0; |
| 98 | + for (var i = 0; i <= 120; i++) |
| 99 | + { |
| 100 | + var t = i / 120.0; |
| 101 | + var x = startX + (endX - startX) * t; |
| 102 | + var y = centerY - Math.Sin((t * Math.PI * 10) + phase) * amplitude; |
| 103 | + if (i == 0) |
| 104 | + context.BeginFigure(new Point(x, y), false, false); |
| 105 | + else |
| 106 | + context.LineTo(new Point(x, y), true, false); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + geometry.Freeze(); |
| 111 | + dc.DrawGeometry(null, pen, geometry); |
| 112 | + } |
| 113 | + |
| 114 | + private IEnumerable<SequenceStateViewModel> GetStates() |
| 115 | + { |
| 116 | + if (SequenceStates is null) |
| 117 | + yield break; |
| 118 | + |
| 119 | + foreach (var item in SequenceStates) |
| 120 | + { |
| 121 | + if (item is SequenceStateViewModel state) |
| 122 | + yield return state; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void Attach(IEnumerable? enumerable) |
| 127 | + { |
| 128 | + if (enumerable is INotifyCollectionChanged collection) |
| 129 | + collection.CollectionChanged += OnCollectionChanged; |
| 130 | + foreach (var item in enumerable ?? Array.Empty<object>()) |
| 131 | + if (item is INotifyPropertyChanged propertyChanged) |
| 132 | + propertyChanged.PropertyChanged += OnItemChanged; |
| 133 | + } |
| 134 | + |
| 135 | + private void Detach(IEnumerable? enumerable) |
| 136 | + { |
| 137 | + if (enumerable is INotifyCollectionChanged collection) |
| 138 | + collection.CollectionChanged -= OnCollectionChanged; |
| 139 | + foreach (var item in enumerable ?? Array.Empty<object>()) |
| 140 | + if (item is INotifyPropertyChanged propertyChanged) |
| 141 | + propertyChanged.PropertyChanged -= OnItemChanged; |
| 142 | + } |
| 143 | + |
| 144 | + private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| 145 | + { |
| 146 | + InvalidateVisual(); |
| 147 | + } |
| 148 | + |
| 149 | + private void OnItemChanged(object? sender, PropertyChangedEventArgs e) |
| 150 | + => InvalidateVisual(); |
| 151 | + |
| 152 | + private void DrawText(DrawingContext dc, string text, Point origin, double size, Color color) |
| 153 | + { |
| 154 | + var dpi = VisualTreeHelper.GetDpi(this); |
| 155 | + var formatted = new FormattedText( |
| 156 | + text, |
| 157 | + CultureInfo.CurrentUICulture, |
| 158 | + FlowDirection.LeftToRight, |
| 159 | + new Typeface("Segoe UI"), |
| 160 | + size, |
| 161 | + new SolidColorBrush(color), |
| 162 | + dpi.PixelsPerDip); |
| 163 | + dc.DrawText(formatted, origin); |
| 164 | + } |
| 165 | +} |
0 commit comments