|
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using BPA.Helper;
-
- namespace BPASmartClient.CustomResource.UserControls
- {
- /// <summary>
- /// TechnologyInstrument.xaml 的交互逻辑
- /// </summary>
- public partial class TechnologyInstrument : UserControl
- {
- public TechnologyInstrument()
- {
- InitializeComponent();
- this.Loaded += (o, e) => { DrawScale(); };
- }
-
-
- /// <summary>
- /// 单位
- /// </summary>
- public string Unit
- {
- get { return (string)GetValue(UnitProperty); }
- set { SetValue(UnitProperty, value); }
- }
- public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(TechnologyInstrument), new("%"));
-
-
- public string Title
- {
- get { return (string)GetValue(TitleProperty); }
- set { SetValue(TitleProperty, value); }
- }
- public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TechnologyInstrument), new("Title"));
-
-
- public double Value
- {
- get { return (double)GetValue(ValueProperty); }
- set { SetValue(ValueProperty, value); }
- }
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register("Value", typeof(double), typeof(TechnologyInstrument),
- new PropertyMetadata(default(double), new PropertyChangedCallback(OnValuePropertyChanged)));
-
- public double Minimum
- {
- get { return (double)GetValue(MinimumProperty); }
- set { SetValue(MinimumProperty, value); }
- }
- public static readonly DependencyProperty MinimumProperty =
- DependencyProperty.Register("Minimum", typeof(double), typeof(TechnologyInstrument),
- new PropertyMetadata(0d, new PropertyChangedCallback(OnPropertyChanged)));
-
- public double Maximum
- {
- get { return (double)GetValue(MaximumProperty); }
- set { SetValue(MaximumProperty, value); }
- }
- public static readonly DependencyProperty MaximumProperty =
- DependencyProperty.Register("Maximum", typeof(double), typeof(TechnologyInstrument),
- new PropertyMetadata(100d, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- /// <summary>
- /// 大刻度分段间隔数
- /// </summary>
- public int LargeScaleInterval
- {
- get { return (int)GetValue(LargeScaleIntervalProperty); }
- set { SetValue(LargeScaleIntervalProperty, value); }
- }
- public static readonly DependencyProperty LargeScaleIntervalProperty =
- DependencyProperty.Register("LargeScaleInterval", typeof(int), typeof(TechnologyInstrument),
- new PropertyMetadata(10, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- /// <summary>
- /// 小刻度分段数
- /// </summary>
- public int SmallScaleInterval
- {
- get { return (int)GetValue(SmallScaleIntervalProperty); }
- set { SetValue(SmallScaleIntervalProperty, value); }
- }
- public static readonly DependencyProperty SmallScaleIntervalProperty =
- DependencyProperty.Register("SmallScaleInterval", typeof(int), typeof(TechnologyInstrument),
- new PropertyMetadata(5, new PropertyChangedCallback(OnPropertyChanged)));
-
-
-
- public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as TechnologyInstrument)?.DrawScale();
- }
-
- public static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as TechnologyInstrument)?.Refresh();
- }
-
- private void Refresh()
- {
- DoubleAnimation da1 = new DoubleAnimation(Value.Scale(Maximum, Minimum, 100, 0), new Duration(TimeSpan.FromMilliseconds(500)));//指定动画的值
- this.ellips.BeginAnimation(EllipseProgressBehavior.ProgressProperty, da1);//指定动画对象
-
- var TempProgress = Value.Scale(Maximum, Minimum, 100, 0);
-
- //指针动画显示
- DoubleAnimation da = new DoubleAnimation(TempProgress.Scale(100, 0, 315, 45), new Duration(TimeSpan.FromMilliseconds(500)));//指定动画的值
- this.rtfPath.BeginAnimation(RotateTransform.AngleProperty, da);//指定动画对象
-
- }
-
- /// <summary>
- /// 画表盘的刻度
- /// </summary>
- private void DrawScale()
- {
- if (this.canvasPlate == null || Maximum <= Minimum) return;
- this.canvasPlate.Children.Clear();
-
-
- //double step = 270.0 / (this.Maximum - this.Minimum);//获取大刻度的角度
- //step = 270.0 / Interval;
- //int TextScale = (int)this.Minimum;
- for (int i = 0; i <= LargeScaleInterval; i++)
- {
- double calcValue = i * Maximum / LargeScaleInterval * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180;
- Line lineScale = new Line();
- //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
- lineScale.X1 = 150 - 130 * Math.Cos(calcValue);
- lineScale.Y1 = 150 - 130 * Math.Sin(calcValue);
- lineScale.Stroke = new SolidColorBrush(Colors.White);
- lineScale.StrokeThickness = 2;
-
- //添加刻度值
- TextBlock txtScale = new TextBlock();
- txtScale.Text = Math.Truncate((this.Minimum + (this.Maximum - this.Minimum) / LargeScaleInterval * i)).ToString();
- txtScale.Width = 34;
- txtScale.TextAlignment = TextAlignment.Center;
- txtScale.Foreground = new SolidColorBrush(Colors.White);
- txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
- txtScale.FontSize = 18;
- Canvas.SetLeft(txtScale, 150 - 105 * Math.Cos(calcValue) - 17);
- Canvas.SetTop(txtScale, 150 - 105 * Math.Sin(calcValue) - 10);
-
- this.canvasPlate.Children.Add(txtScale);
-
- lineScale.X2 = 150 - 140 * Math.Cos(calcValue);
- lineScale.Y2 = 150 - 140 * Math.Sin(calcValue);
- this.canvasPlate.Children.Add(lineScale);
- }
-
-
-
- //小刻度绘制
- double ste = (this.Maximum - this.Minimum) / LargeScaleInterval / SmallScaleInterval;
- for (int i = 0; i < (this.Maximum - this.Minimum) / ste; i++)
- {
- double calcValue = i * ste * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180;
-
- Line lineScale = new Line();
-
- lineScale.X1 = 150 - 130 * Math.Cos(calcValue);
- lineScale.Y1 = 150 - 130 * Math.Sin(calcValue);
- lineScale.Stroke = new SolidColorBrush(Colors.White);
- lineScale.StrokeThickness = 1;
- lineScale.Opacity = 0.5;
-
-
- lineScale.X2 = 150 - 140 * Math.Cos(calcValue);
- lineScale.Y2 = 150 - 140 * Math.Sin(calcValue);
- this.canvasPlate.Children.Add(lineScale);
- }
-
-
-
- return;
-
- for (double i = 0; i <= this.Maximum - this.Minimum; i++)
- {
- //添加刻度线
- Line lineScale = new Line();
-
- if (i % 10 == 0)//大刻度
- {
- //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
- lineScale.X1 = 150 - 130 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- lineScale.Y1 = 150 - 130 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- lineScale.Stroke = new SolidColorBrush(Colors.White);
- lineScale.StrokeThickness = 2;
-
- //添加刻度值
- TextBlock txtScale = new TextBlock();
- txtScale.Text = (i + this.Minimum).ToString();
- txtScale.Width = 34;
- txtScale.TextAlignment = TextAlignment.Center;
- txtScale.Foreground = new SolidColorBrush(Colors.White);
- txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
- txtScale.FontSize = 18;
-
- Canvas.SetLeft(txtScale, 150 - 105 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 17);
- Canvas.SetTop(txtScale, 150 - 105 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 10);
-
- this.canvasPlate.Children.Add(txtScale);
- }
- else//小刻度
- {
- lineScale.X1 = 150 - 130 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- lineScale.Y1 = 150 - 130 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- lineScale.Stroke = new SolidColorBrush(Colors.White);
- lineScale.StrokeThickness = 1;
- lineScale.Opacity = 0.5;
- }
-
- lineScale.X2 = 150 - 140 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- lineScale.Y2 = 150 - 140 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- this.canvasPlate.Children.Add(lineScale);
- }
-
-
-
- //if (this.canvasPlate == null) return;
- //this.canvasPlate.Children.Clear();
-
- //for (double i = 0; i <= this.Maximum - this.Minimum; i++)
- //{
- // //添加刻度线
- // Line lineScale = new Line();
-
- // if (i % 10 == 0)
- // {
- // //注意Math.Cos和Math.Sin的参数是弧度,记得将角度转为弧度制
- // lineScale.X1 = 200 - 170 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // lineScale.Y1 = 200 - 170 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // lineScale.Stroke = new SolidColorBrush(Colors.White);
- // lineScale.StrokeThickness = 2;
-
- // //添加刻度值
- // TextBlock txtScale = new TextBlock();
- // txtScale.Text = (i + this.Minimum).ToString();
- // txtScale.Width = 34;
- // txtScale.TextAlignment = TextAlignment.Center;
- // txtScale.Foreground = new SolidColorBrush(Colors.White);
- // txtScale.RenderTransform = new RotateTransform() { Angle = 45, CenterX = 17, CenterY = 8 };
- // txtScale.FontSize = 18;
-
- // Canvas.SetLeft(txtScale, 200 - 155 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 17);
- // Canvas.SetTop(txtScale, 200 - 155 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180) - 10);
-
- // this.canvasPlate.Children.Add(txtScale);
- // }
- // else
- // {
- // lineScale.X1 = 200 - 180 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // lineScale.Y1 = 200 - 180 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // lineScale.Stroke = new SolidColorBrush(Colors.White);
- // lineScale.StrokeThickness = 1;
- // lineScale.Opacity = 0.5;
- // }
-
- // lineScale.X2 = 200 - 190 * Math.Cos(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // lineScale.Y2 = 200 - 190 * Math.Sin(i * (270 / (this.Maximum - this.Minimum)) * Math.PI / 180);
- // this.canvasPlate.Children.Add(lineScale);
- //}
-
-
-
- }
-
-
- }
- }
|