终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

290 рядки
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using BPA.Helper;
  18. namespace BPASmartClient.CustomResource.UserControls
  19. {
  20. /// <summary>
  21. /// TechnologyInstrument.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class TechnologyInstrument : UserControl
  24. {
  25. public TechnologyInstrument()
  26. {
  27. InitializeComponent();
  28. this.Loaded += (o, e) => { DrawScale(); };
  29. }
  30. /// <summary>
  31. /// 单位
  32. /// </summary>
  33. public string Unit
  34. {
  35. get { return (string)GetValue(UnitProperty); }
  36. set { SetValue(UnitProperty, value); }
  37. }
  38. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(TechnologyInstrument), new("%"));
  39. public string Title
  40. {
  41. get { return (string)GetValue(TitleProperty); }
  42. set { SetValue(TitleProperty, value); }
  43. }
  44. public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TechnologyInstrument), new("Title"));
  45. public double Value
  46. {
  47. get { return (double)GetValue(ValueProperty); }
  48. set { SetValue(ValueProperty, value); }
  49. }
  50. public static readonly DependencyProperty ValueProperty =
  51. DependencyProperty.Register("Value", typeof(double), typeof(TechnologyInstrument),
  52. new PropertyMetadata(default(double), new PropertyChangedCallback(OnValuePropertyChanged)));
  53. public double Minimum
  54. {
  55. get { return (double)GetValue(MinimumProperty); }
  56. set { SetValue(MinimumProperty, value); }
  57. }
  58. public static readonly DependencyProperty MinimumProperty =
  59. DependencyProperty.Register("Minimum", typeof(double), typeof(TechnologyInstrument),
  60. new PropertyMetadata(0d, new PropertyChangedCallback(OnPropertyChanged)));
  61. public double Maximum
  62. {
  63. get { return (double)GetValue(MaximumProperty); }
  64. set { SetValue(MaximumProperty, value); }
  65. }
  66. public static readonly DependencyProperty MaximumProperty =
  67. DependencyProperty.Register("Maximum", typeof(double), typeof(TechnologyInstrument),
  68. new PropertyMetadata(100d, new PropertyChangedCallback(OnPropertyChanged)));
  69. /// <summary>
  70. /// 大刻度分段间隔数
  71. /// </summary>
  72. public int LargeScaleInterval
  73. {
  74. get { return (int)GetValue(LargeScaleIntervalProperty); }
  75. set { SetValue(LargeScaleIntervalProperty, value); }
  76. }
  77. public static readonly DependencyProperty LargeScaleIntervalProperty =
  78. DependencyProperty.Register("LargeScaleInterval", typeof(int), typeof(TechnologyInstrument),
  79. new PropertyMetadata(10, new PropertyChangedCallback(OnPropertyChanged)));
  80. /// <summary>
  81. /// 小刻度分段数
  82. /// </summary>
  83. public int SmallScaleInterval
  84. {
  85. get { return (int)GetValue(SmallScaleIntervalProperty); }
  86. set { SetValue(SmallScaleIntervalProperty, value); }
  87. }
  88. public static readonly DependencyProperty SmallScaleIntervalProperty =
  89. DependencyProperty.Register("SmallScaleInterval", typeof(int), typeof(TechnologyInstrument),
  90. new PropertyMetadata(5, new PropertyChangedCallback(OnPropertyChanged)));
  91. public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  92. {
  93. (d as TechnologyInstrument)?.DrawScale();
  94. }
  95. public static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  96. {
  97. (d as TechnologyInstrument)?.Refresh();
  98. }
  99. private void Refresh()
  100. {
  101. DoubleAnimation da1 = new DoubleAnimation(Value.Scale(Maximum, Minimum, 100, 0), new Duration(TimeSpan.FromMilliseconds(500)));//指定动画的值
  102. this.ellips.BeginAnimation(EllipseProgressBehavior.ProgressProperty, da1);//指定动画对象
  103. var TempProgress = Value.Scale(Maximum, Minimum, 100, 0);
  104. //指针动画显示
  105. DoubleAnimation da = new DoubleAnimation(TempProgress.Scale(100, 0, 315, 45), new Duration(TimeSpan.FromMilliseconds(500)));//指定动画的值
  106. this.rtfPath.BeginAnimation(RotateTransform.AngleProperty, da);//指定动画对象
  107. }
  108. /// <summary>
  109. /// 画表盘的刻度
  110. /// </summary>
  111. private void DrawScale()
  112. {
  113. if (this.canvasPlate == null || Maximum <= Minimum) return;
  114. this.canvasPlate.Children.Clear();
  115. //double step = 270.0 / (this.Maximum - this.Minimum);//获取大刻度的角度
  116. //step = 270.0 / Interval;
  117. //int TextScale = (int)this.Minimum;
  118. for (int i = 0; i <= LargeScaleInterval; i++)
  119. {
  120. double calcValue = i * Maximum / LargeScaleInterval * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180;
  121. Line lineScale = new Line();
  122. //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
  123. lineScale.X1 = 150 - 130 * Math.Cos(calcValue);
  124. lineScale.Y1 = 150 - 130 * Math.Sin(calcValue);
  125. lineScale.Stroke = new SolidColorBrush(Colors.White);
  126. lineScale.StrokeThickness = 2;
  127. //添加刻度值
  128. TextBlock txtScale = new TextBlock();
  129. txtScale.Text = Math.Truncate((this.Minimum + (this.Maximum - this.Minimum) / LargeScaleInterval * i)).ToString();
  130. txtScale.Width = 34;
  131. txtScale.TextAlignment = TextAlignment.Center;
  132. txtScale.Foreground = new SolidColorBrush(Colors.White);
  133. txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
  134. txtScale.FontSize = 18;
  135. Canvas.SetLeft(txtScale, 150 - 105 * Math.Cos(calcValue) - 17);
  136. Canvas.SetTop(txtScale, 150 - 105 * Math.Sin(calcValue) - 10);
  137. this.canvasPlate.Children.Add(txtScale);
  138. lineScale.X2 = 150 - 140 * Math.Cos(calcValue);
  139. lineScale.Y2 = 150 - 140 * Math.Sin(calcValue);
  140. this.canvasPlate.Children.Add(lineScale);
  141. }
  142. //小刻度绘制
  143. double ste = (this.Maximum - this.Minimum) / LargeScaleInterval / SmallScaleInterval;
  144. for (int i = 0; i < (this.Maximum - this.Minimum) / ste; i++)
  145. {
  146. double calcValue = i * ste * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180;
  147. Line lineScale = new Line();
  148. lineScale.X1 = 150 - 130 * Math.Cos(calcValue);
  149. lineScale.Y1 = 150 - 130 * Math.Sin(calcValue);
  150. lineScale.Stroke = new SolidColorBrush(Colors.White);
  151. lineScale.StrokeThickness = 1;
  152. lineScale.Opacity = 0.5;
  153. lineScale.X2 = 150 - 140 * Math.Cos(calcValue);
  154. lineScale.Y2 = 150 - 140 * Math.Sin(calcValue);
  155. this.canvasPlate.Children.Add(lineScale);
  156. }
  157. return;
  158. for (double i = 0; i <= this.Maximum - this.Minimum; i++)
  159. {
  160. //添加刻度线
  161. Line lineScale = new Line();
  162. if (i % 10 == 0)//大刻度
  163. {
  164. //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
  165. lineScale.X1 = 150 - 130 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  166. lineScale.Y1 = 150 - 130 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  167. lineScale.Stroke = new SolidColorBrush(Colors.White);
  168. lineScale.StrokeThickness = 2;
  169. //添加刻度值
  170. TextBlock txtScale = new TextBlock();
  171. txtScale.Text = (i + this.Minimum).ToString();
  172. txtScale.Width = 34;
  173. txtScale.TextAlignment = TextAlignment.Center;
  174. txtScale.Foreground = new SolidColorBrush(Colors.White);
  175. txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
  176. txtScale.FontSize = 18;
  177. Canvas.SetLeft(txtScale, 150 - 105 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 17);
  178. Canvas.SetTop(txtScale, 150 - 105 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 10);
  179. this.canvasPlate.Children.Add(txtScale);
  180. }
  181. else//小刻度
  182. {
  183. lineScale.X1 = 150 - 130 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  184. lineScale.Y1 = 150 - 130 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  185. lineScale.Stroke = new SolidColorBrush(Colors.White);
  186. lineScale.StrokeThickness = 1;
  187. lineScale.Opacity = 0.5;
  188. }
  189. lineScale.X2 = 150 - 140 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  190. lineScale.Y2 = 150 - 140 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  191. this.canvasPlate.Children.Add(lineScale);
  192. }
  193. //if (this.canvasPlate == null) return;
  194. //this.canvasPlate.Children.Clear();
  195. //for (double i = 0; i <= this.Maximum - this.Minimum; i++)
  196. //{
  197. // //添加刻度线
  198. // Line lineScale = new Line();
  199. // if (i % 10 == 0)
  200. // {
  201. // //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
  202. // lineScale.X1 = 200 - 170 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  203. // lineScale.Y1 = 200 - 170 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  204. // lineScale.Stroke = new SolidColorBrush(Colors.White);
  205. // lineScale.StrokeThickness = 2;
  206. // //添加刻度值
  207. // TextBlock txtScale = new TextBlock();
  208. // txtScale.Text = (i + this.Minimum).ToString();
  209. // txtScale.Width = 34;
  210. // txtScale.TextAlignment = TextAlignment.Center;
  211. // txtScale.Foreground = new SolidColorBrush(Colors.White);
  212. // txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
  213. // txtScale.FontSize = 18;
  214. // Canvas.SetLeft(txtScale, 200 - 155 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 17);
  215. // Canvas.SetTop(txtScale, 200 - 155 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 10);
  216. // this.canvasPlate.Children.Add(txtScale);
  217. // }
  218. // else
  219. // {
  220. // lineScale.X1 = 200 - 180 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  221. // lineScale.Y1 = 200 - 180 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  222. // lineScale.Stroke = new SolidColorBrush(Colors.White);
  223. // lineScale.StrokeThickness = 1;
  224. // lineScale.Opacity = 0.5;
  225. // }
  226. // lineScale.X2 = 200 - 190 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  227. // lineScale.Y2 = 200 - 190 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
  228. // this.canvasPlate.Children.Add(lineScale);
  229. //}
  230. }
  231. }
  232. }