|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls.Primitives; |
| 4 | +using System.Windows.Media; |
| 5 | + |
| 6 | +namespace WPFDevelopers.Controls |
| 7 | +{ |
| 8 | + public class Gauge : RangeBase |
| 9 | + { |
| 10 | + public string Title |
| 11 | + { |
| 12 | + get { return (string)GetValue(TitleProperty); } |
| 13 | + set { SetValue(TitleProperty, value); } |
| 14 | + } |
| 15 | + |
| 16 | + public static readonly DependencyProperty TitleProperty = |
| 17 | + DependencyProperty.Register("TitleProperty", typeof(string), typeof(Gauge), new PropertyMetadata("WD")); |
| 18 | + |
| 19 | + public static readonly DependencyProperty ValueFormatProperty = |
| 20 | + DependencyProperty.Register("ValueFormat", typeof(string), typeof(Gauge), |
| 21 | + new PropertyMetadata("{0:0}%", OnValueFormatChanged)); |
| 22 | + |
| 23 | + private static void OnValueFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 24 | + { |
| 25 | + var gauge = d as Gauge; |
| 26 | + gauge?.InvalidateVisual(); |
| 27 | + } |
| 28 | + |
| 29 | + public string ValueFormat |
| 30 | + { |
| 31 | + get { return (string)GetValue(ValueFormatProperty); } |
| 32 | + set { SetValue(ValueFormatProperty, value); } |
| 33 | + } |
| 34 | + public double Thickness |
| 35 | + { |
| 36 | + get { return (double)GetValue(ThicknessProperty); } |
| 37 | + set { SetValue(ThicknessProperty, value); } |
| 38 | + } |
| 39 | + |
| 40 | + public static readonly DependencyProperty ThicknessProperty = |
| 41 | + DependencyProperty.Register("Thickness", typeof(double), typeof(Gauge), new PropertyMetadata(10.0, OnValueFormatChanged)); |
| 42 | + |
| 43 | + |
| 44 | + static Gauge() |
| 45 | + { |
| 46 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(Gauge), new FrameworkPropertyMetadata(typeof(Gauge))); |
| 47 | + } |
| 48 | + |
| 49 | + public Gauge() |
| 50 | + { |
| 51 | + SetValue(ValueProperty, 0.0); |
| 52 | + SetValue(MinimumProperty, 0.0); |
| 53 | + SetValue(MaximumProperty, 100.0); |
| 54 | + } |
| 55 | + |
| 56 | + protected override void OnValueChanged(double oldValue, double newValue) |
| 57 | + { |
| 58 | + InvalidateVisual(); |
| 59 | + } |
| 60 | + |
| 61 | + protected override void OnMinimumChanged(double oldValue, double newValue) |
| 62 | + { |
| 63 | + InvalidateVisual(); |
| 64 | + } |
| 65 | + |
| 66 | + protected override void OnMaximumChanged(double oldValue, double newValue) |
| 67 | + { |
| 68 | + InvalidateVisual(); |
| 69 | + } |
| 70 | + protected override void OnRender(DrawingContext drawingContext) |
| 71 | + { |
| 72 | + base.OnRender(drawingContext); |
| 73 | + if (Background == null) |
| 74 | + Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#293950")); |
| 75 | + var width = ActualWidth; |
| 76 | + var height = ActualHeight; |
| 77 | + var radius = Math.Min(width, height) / 2; |
| 78 | + drawingContext.DrawEllipse(Background, new Pen(Background, Thickness), new Point(width / 2, height / 2), radius, radius); |
| 79 | + var normalizedValue = (Value - Minimum) / (Maximum - Minimum); |
| 80 | + var mappedAngle = -220 + normalizedValue * 260; |
| 81 | + var angleInRadians = mappedAngle * Math.PI / 180; |
| 82 | + var pointerLength = radius * 0.7; |
| 83 | + var pointerX = width / 2 + pointerLength * Math.Cos(angleInRadians); |
| 84 | + var pointerY = height / 2 + pointerLength * Math.Sin(angleInRadians); |
| 85 | + drawingContext.DrawLine(new Pen(Brushes.Red, 2), new Point(width / 2, height / 2), new Point(pointerX, pointerY)); |
| 86 | + drawingContext.DrawEllipse(Brushes.White, new Pen(Brushes.Red, 2), new Point(width / 2, height / 2), width / 20, width / 20); |
| 87 | + var pathGeometry = new PathGeometry(); |
| 88 | + var startAngle = -220; |
| 89 | + angleInRadians = startAngle * Math.PI / 180; |
| 90 | + var startX = width / 2 + radius * Math.Cos(angleInRadians); |
| 91 | + var startY = height / 2 + radius * Math.Sin(angleInRadians); |
| 92 | + |
| 93 | + var pathFigure = new PathFigure() |
| 94 | + { |
| 95 | + StartPoint = new Point(startX, startY), |
| 96 | + }; |
| 97 | + |
| 98 | + var endAngle = 40; |
| 99 | + angleInRadians = endAngle * Math.PI / 180; |
| 100 | + var endX = width / 2 + radius * Math.Cos(angleInRadians); |
| 101 | + var endY = height / 2 + radius * Math.Sin(angleInRadians); |
| 102 | + |
| 103 | + var isLargeArc = (endAngle - startAngle > 180); |
| 104 | + var arcSegment = new ArcSegment() |
| 105 | + { |
| 106 | + Point = new Point(endX, endY), |
| 107 | + Size = new Size(radius, radius), |
| 108 | + RotationAngle = 0, |
| 109 | + SweepDirection = SweepDirection.Clockwise, |
| 110 | + IsLargeArc = isLargeArc, |
| 111 | + }; |
| 112 | + |
| 113 | + pathFigure.Segments.Add(arcSegment); |
| 114 | + pathGeometry.Figures.Add(pathFigure); |
| 115 | + if (BorderBrush == null) |
| 116 | + { |
| 117 | + var gradientBrush = new LinearGradientBrush |
| 118 | + { |
| 119 | + StartPoint = new Point(0, 0), |
| 120 | + EndPoint = new Point(1, 0) |
| 121 | + }; |
| 122 | + gradientBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#37D2C2"), 0.0)); |
| 123 | + gradientBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#5AD2B2"), 0.01)); |
| 124 | + gradientBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#B77D29"), 0.49)); |
| 125 | + gradientBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0)); |
| 126 | + gradientBrush.Freeze(); |
| 127 | + BorderBrush = gradientBrush; |
| 128 | + } |
| 129 | + drawingContext.DrawGeometry(null, new Pen(BorderBrush, Thickness), pathGeometry); |
| 130 | + var tickLength = radius * 0.1; |
| 131 | + var step = (Maximum - Minimum) / 10; |
| 132 | + for (int i = 0; i <= 10; i++) |
| 133 | + { |
| 134 | + var angle = startAngle + (i * (endAngle - startAngle) / 10); |
| 135 | + var tickStartX = width / 2 + (radius - tickLength) * Math.Cos(angle * Math.PI / 180); |
| 136 | + var tickStartY = height / 2 + (radius - tickLength) * Math.Sin(angle * Math.PI / 180); |
| 137 | + var tickEndX = width / 2 + (radius + Thickness / 2) * Math.Cos(angle * Math.PI / 180); |
| 138 | + var tickEndY = height / 2 + (radius + Thickness / 2) * Math.Sin(angle * Math.PI / 180); |
| 139 | + drawingContext.DrawLine(new Pen(Brushes.White, 2), new Point(tickStartX, tickStartY), new Point(tickEndX, tickEndY)); |
| 140 | + |
| 141 | + var labelValue = Minimum + step * i; |
| 142 | + var formattedText = DrawingContextHelper.GetFormattedText(labelValue.ToString(),Brushes.White, FlowDirection.LeftToRight,FontSize); |
| 143 | + |
| 144 | + var labelRadius = radius - tickLength * 2; |
| 145 | + var labelX = width / 2 + labelRadius * Math.Cos(angle * Math.PI / 180) - formattedText.Width / 2; |
| 146 | + var labelY = height / 2 + labelRadius * Math.Sin(angle * Math.PI / 180) - formattedText.Height / 2; |
| 147 | + drawingContext.DrawText(formattedText, new Point(labelX, labelY)); |
| 148 | + } |
| 149 | + var formattedValue = "{0:0}%"; |
| 150 | + try |
| 151 | + { |
| 152 | + formattedValue = string.Format(ValueFormat, Value); |
| 153 | + } |
| 154 | + catch (FormatException ex) |
| 155 | + { |
| 156 | + throw new InvalidOperationException("Formatting failed ", ex); |
| 157 | + } |
| 158 | + var currentValueText = DrawingContextHelper.GetFormattedText(formattedValue, Brushes.White, FlowDirection.LeftToRight, FontSize * 2); |
| 159 | + var valueX = width / 2 - currentValueText.Width / 2; |
| 160 | + var valueY = height / 2 + radius * 0.4; |
| 161 | + drawingContext.DrawText(currentValueText, new Point(valueX, valueY)); |
| 162 | + var titleValue = DrawingContextHelper.GetFormattedText(Title, Brushes.White, FlowDirection.LeftToRight, FontSize); |
| 163 | + valueX = width / 2 - titleValue.Width / 2; |
| 164 | + valueY = height / 2 + radius * 0.8; |
| 165 | + drawingContext.DrawText(titleValue, new Point(valueX, valueY)); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments