Dashboard.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Animation;
  9. using System.Windows.Media;
  10. using System.Windows.Shapes;
  11. using HandyControl.Expression.Shapes;
  12. namespace HandyControl.Controls
  13. {
  14. public class Dashboard : System.Windows.Controls.Control
  15. {
  16. public const string ArcName = "arckd";
  17. public const string ValueTxtName = "valueTxt";
  18. public const string CanvasName = "canvasPlate";
  19. public const string ZZRotateName = "rtPointer";
  20. private static double StartAngle = -135.5;
  21. static Dashboard()
  22. {
  23. DefaultStyleKeyProperty.OverrideMetadata(typeof(Dashboard), new FrameworkPropertyMetadata(typeof(Dashboard)));
  24. }
  25. Arc arckd;
  26. TextBlock valueTxt;
  27. Canvas canvasPlate;
  28. RotateTransform rtPointer;
  29. public override void OnApplyTemplate()
  30. {
  31. base.OnApplyTemplate();
  32. if (arckd == null)
  33. arckd = GetTemplateChild(ArcName) as Arc;
  34. if (valueTxt == null)
  35. valueTxt = GetTemplateChild(ValueTxtName) as TextBlock;
  36. if (canvasPlate == null)
  37. canvasPlate = GetTemplateChild(CanvasName) as Canvas;
  38. if (rtPointer == null)
  39. rtPointer = GetTemplateChild(ZZRotateName) as RotateTransform;
  40. DrawScale();
  41. DrawAngle();
  42. arckd.EndAngle = StartAngle + (-StartAngle * 2d) * ((double)Value / Maximum);
  43. valueTxt.Text = Value.ToString();
  44. }
  45. public double Value
  46. {
  47. get { return (double)GetValue(ValueProperty); }
  48. set
  49. {
  50. SetValue(ValueProperty, value);
  51. }
  52. }
  53. public static readonly DependencyProperty ValueProperty =
  54. DependencyProperty.Register("Value", typeof(double), typeof(Dashboard),
  55. new PropertyMetadata(default(double), new PropertyChangedCallback(OnValuePropertyChanged)));
  56. public double Minimum
  57. {
  58. get { return (double)GetValue(MinimumProperty); }
  59. set { SetValue(MinimumProperty, value); }
  60. }
  61. public static readonly DependencyProperty MinimumProperty =
  62. DependencyProperty.Register("Minimum", typeof(double), typeof(Dashboard),
  63. new PropertyMetadata(double.NaN, new PropertyChangedCallback(OnPropertyChanged)));
  64. public double Maximum
  65. {
  66. get { return (double)GetValue(MaximumProperty); }
  67. set { SetValue(MaximumProperty, value); }
  68. }
  69. public static readonly DependencyProperty MaximumProperty =
  70. DependencyProperty.Register("Maximum", typeof(double), typeof(Dashboard),
  71. new PropertyMetadata(double.NaN, new PropertyChangedCallback(OnPropertyChanged)));
  72. public Brush PlateBackground
  73. {
  74. get { return (Brush)GetValue(PlateBackgroundProperty); }
  75. set { SetValue(PlateBackgroundProperty, value); }
  76. }
  77. public static readonly DependencyProperty PlateBackgroundProperty =
  78. DependencyProperty.Register("PlateBackground", typeof(Brush), typeof(Dashboard), null);
  79. public string Unit
  80. {
  81. get { return (string)GetValue(UnitProperty); }
  82. set { SetValue(UnitProperty, value); }
  83. }
  84. // Using a DependencyProperty as the backing store for Unit. This enables animation, styling, binding, etc...
  85. public static readonly DependencyProperty UnitProperty =
  86. DependencyProperty.Register("Unit", typeof(string), typeof(Dashboard), new PropertyMetadata("km/h"));
  87. public Brush PlateBorderBrush
  88. {
  89. get { return (Brush)GetValue(PlateBorderBrushProperty); }
  90. set { SetValue(PlateBorderBrushProperty, value); }
  91. }
  92. public static readonly DependencyProperty PlateBorderBrushProperty =
  93. DependencyProperty.Register("PlateBorderBrush", typeof(Brush), typeof(Dashboard), null);
  94. public Thickness PlateBorderThickness
  95. {
  96. get { return (Thickness)GetValue(PlateBorderThicknessProperty); }
  97. set { SetValue(PlateBorderThicknessProperty, value); }
  98. }
  99. public static readonly DependencyProperty PlateBorderThicknessProperty =
  100. DependencyProperty.Register("PlateBorderThickness", typeof(Thickness), typeof(Dashboard), null);
  101. public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  102. {
  103. (d as Dashboard).DrawScale();
  104. }
  105. public static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  106. {
  107. Dashboard dashboard = d as Dashboard;
  108. dashboard.DrawAngle();
  109. if (dashboard.arckd == null)
  110. return;
  111. dashboard.arckd.EndAngle = StartAngle + (-StartAngle * 2d) * ((double)dashboard.Value / dashboard.Maximum);
  112. dashboard.valueTxt.Text = dashboard.Value.ToString();
  113. }
  114. /// <summary>
  115. /// 画表盘的刻度
  116. /// </summary>
  117. private void DrawScale()
  118. {
  119. if (this.canvasPlate == null) return;
  120. this.canvasPlate.Children.Clear();
  121. for (double i = 0; i <= this.Maximum - this.Minimum; i++)
  122. {
  123. //添加刻度线
  124. Line lineScale = new Line();
  125. if (i % 10 == 0)
  126. {
  127. //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
  128. lineScale.X1 = 200 - 170 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  129. lineScale.Y1 = 200 - 170 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  130. lineScale.Stroke = new SolidColorBrush(Colors.White);
  131. lineScale.StrokeThickness = 2;
  132. //添加刻度值
  133. TextBlock txtScale = new TextBlock();
  134. txtScale.Text = (i + this.Minimum).ToString();
  135. txtScale.Width = 34;
  136. txtScale.TextAlignment = TextAlignment.Center;
  137. txtScale.Foreground = new SolidColorBrush(Colors.White);
  138. txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
  139. txtScale.FontSize = 18;
  140. Canvas.SetLeft(txtScale, 200 - 155 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 17);
  141. Canvas.SetTop(txtScale, 200 - 155 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 10);
  142. this.canvasPlate.Children.Add(txtScale);
  143. }
  144. else
  145. {
  146. lineScale.X1 = 200 - 180 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  147. lineScale.Y1 = 200 - 180 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  148. lineScale.Stroke = new SolidColorBrush(Colors.White);
  149. lineScale.StrokeThickness = 1;
  150. lineScale.Opacity = 0.5;
  151. }
  152. lineScale.X2 = 200 - 190 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  153. lineScale.Y2 = 200 - 190 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  154. this.canvasPlate.Children.Add(lineScale);
  155. }
  156. }
  157. private void DrawAngle()
  158. {
  159. if (rtPointer == null)
  160. return;
  161. double step = 270.0 / (this.Maximum - this.Minimum);
  162. DoubleAnimation da = new DoubleAnimation(this.Value * step - 45, new Duration(TimeSpan.FromMilliseconds(200)));
  163. this.rtPointer.BeginAnimation(RotateTransform.AngleProperty, da);
  164. }
  165. }
  166. }