跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

提交记录
新增UI/Controls/Charts/ChartsItemTypePie.cs+212 −0看文件
@@ -0,0 +1,212 @@
1新增:using Newtonsoft.Json.Linq;
2新增:using System;
3新增:using System.Collections.Generic;
4新增:using System.Diagnostics;
5新增:using System.Linq;
6新增:using System.Text;
7新增:using System.Threading.Tasks;
8新增:using System.Windows;
9新增:using System.Windows.Controls;
10新增:using System.Windows.Input;
11新增:using System.Windows.Media;
12新增:using System.Windows.Shapes;
13新增:using UI.Base.Color;
14新增:using UI.Controls.Charts.Model;
15新增:
16新增:namespace UI.Controls.Charts
17新增:{
18新增: public class ChartsItemTypePie : Canvas
19新增: {
20新增: #region Data
21新增: /// <summary>
22新增: /// Data
23新增: /// </summary>
24新增: public List<ChartsDataModel> Data
25新增: {
26新增: get { return (List<ChartsDataModel>)GetValue(DataProperty); }
27新增: set { SetValue(DataProperty, value); }
28新增: }
29新增: public static readonly DependencyProperty DataProperty =
30新增: DependencyProperty.Register("Data",
31新增: typeof(List<ChartsDataModel>),
32新增: typeof(ChartsItemTypePie));
33新增:
34新增:
35新增: #endregion
36新增:
37新增: #region MaxValue 最大值
38新增: /// <summary>
39新增: /// 样式类型
40新增: /// </summary>
41新增: public double MaxValue
42新增: {
43新增: get { return (double)GetValue(MaxValueProperty); }
44新增: set { SetValue(MaxValueProperty, value); }
45新增: }
46新增: public static readonly DependencyProperty MaxValueProperty =
47新增: DependencyProperty.Register("MaxValue",
48新增: typeof(double),
49新增: typeof(ChartsItemTypePie));
50新增:
51新增:
52新增: #endregion
53新增: private double _lastAngle = -Math.PI / 2;
54新增: private int _zIndex = 1;
55新增: private List<Path> _paths = new List<Path>();
56新增: public ChartsItemTypePie()
57新增: {
58新增: DefaultStyleKey = typeof(ChartsItemTypePie);
59新增: Loaded += ChartsItemTypePie_Loaded;
60新增: }
61新增:
62新增: private void ChartsItemTypePie_Loaded(object sender, RoutedEventArgs e)
63新增: {
64新增: Render();
65新增: }
66新增:
67新增: private void Render()
68新增: {
69新增: _paths.Clear();
70新增: Children.Clear();
71新增: MaxValue = Data.Sum(m => m.Value);
72新增:
73新增: int i = 0;
74新增: foreach (var item in Data)
75新增: {
76新增: var angle = item.Value / MaxValue * 360;
77新增: //if (i == 1)
78新增: //{
79新增: // angle += 10;
80新增: //}
81新增:
82新增: var path = CreatePath(angle, UI.Base.Color.Colors.GetFromString(item.Color));
83新增: path.ToolTip = item.PopupText;
84新增: path.MouseEnter += Path_MouseEnter;
85新增: path.MouseLeave += Path_MouseLeave;
86新增: _paths.Add(path);
87新增: Children.Add(path);
88新增: i++;
89新增: }
90新增: }
91新增:
92新增: private void Path_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
93新增: {
94新增: foreach (var p in _paths)
95新增: {
96新增: p.Opacity = 1;
97新增: }
98新增: }
99新增:
100新增: private void Path_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
101新增: {
102新增: var path = sender as Path;
103新增: foreach (var p in _paths)
104新增: {
105新增: if (p != path)
106新增: {
107新增: p.Opacity = .2;
108新增: }
109新增: }
110新增: }
111新增: private double _lastX = -1, _lastY = -1;
112新增: private Path CreatePath(double angle_, SolidColorBrush color_)
113新增: {
114新增: Path path = new Path();
115新增:
116新增: PathGeometry pathGeometry = new PathGeometry();
117新增: double Radius = ActualHeight / 2;
118新增: //double Angle = angle_;
119新增: //Point startPoint = new Point(Radius, Radius);
120新增:
121新增: //if (Angle >= 360)
122新增: //{
123新增: // Angle = 359;
124新增: //}
125新增:
126新增:
127新增: double x = Math.Cos(_lastAngle) * Radius + Radius;
128新增: double y = Math.Sin(_lastAngle) * Radius + Radius;
129新增: var lin1 = new LineSegment(new Point(x, y), true);
130新增:
131新增: _lastAngle += Math.PI * angle_ / 180;
132新增:
133新增: x = Math.Cos(_lastAngle) * Radius + Radius;
134新增: y = Math.Sin(_lastAngle) * Radius + Radius;
135新增: //Point endPoint = ComputeCartesianCoordinate(Angle, Radius);
136新增: //endPoint.X += Radius;
137新增: //endPoint.Y += Radius;
138新增:
139新增: //_lastX = endPoint.X;
140新增: //_lastY = endPoint.Y;
141新增: //Debug.WriteLine($"angle:{angle_},start:{startPoint},endpoint:{endPoint}");
142新增:
143新增: var arcSeg = new ArcSegment()
144新增: {
145新增: Size = new Size(Radius, Radius),
146新增: IsLargeArc = angle_ > 180,
147新增: SweepDirection = SweepDirection.Clockwise,
148新增: Point = new Point(x, y),
149新增: RotationAngle = angle_,
150新增: };
151新增: var line2 = new LineSegment(new Point(Radius, Radius), true);
152新增: var fig = new PathFigure(new Point(Radius, Radius), new PathSegment[] { lin1, arcSeg, line2 }, false);
153新增: pathGeometry.Figures.Add(fig);
154新增: path.Data = pathGeometry;
155新增: path.Fill = color_;
156新增: return path;
157新增: }
158新增: //private Path CreatePath(double angle_, SolidColorBrush color_)
159新增: //{
160新增: // Path path = new Path();
161新增: // path.Stroke = color_;
162新增: // path.StrokeThickness = ActualHeight / 2;
163新增: // //path.StrokeStartLineCap = PenLineCap.Round;
164新增: // //path.StrokeEndLineCap = PenLineCap.Round;
165新增:
166新增: // double width = path.StrokeThickness;
167新增:
168新增: // PathGeometry pathGeometry = new PathGeometry();
169新增: // double Radius = (ActualHeight - width) / 2;
170新增: // double Angle = angle_ + _lastAngle;
171新增: // Point startPoint = new Point(Radius, 0);
172新增: // if (Angle >= 360)
173新增: // {
174新增: // Angle = 359;
175新增: // }
176新增: // Point endPoint = ComputeCartesianCoordinate(Angle, Radius);
177新增: // endPoint.X += Radius;
178新增: // endPoint.Y += Radius;
179新增:
180新增: // var pf = new PathFigure()
181新增: // {
182新增: // StartPoint = startPoint
183新增: // };
184新增: // pf.Segments.Add(new ArcSegment()
185新增: // {
186新增: // Size = new Size(Radius, Radius),
187新增: // IsLargeArc = Angle > 180,
188新增: // SweepDirection = SweepDirection.Clockwise,
189新增: // Point = endPoint,
190新增: // });
191新增: // pathGeometry.Figures.Add(pf);
192新增: // path.Data = pathGeometry;
193新增: // _lastAngle = Angle;
194新增: // Canvas.SetLeft(path, width / 2);
195新增: // Canvas.SetTop(path, width / 2);
196新增: // Canvas.SetZIndex(path, _zIndex);
197新增: // _zIndex--;
198新增: // return path;
199新增: //}
200新增: private Point ComputeCartesianCoordinate(double angle, double radius)
201新增: {
202新增: // convert to radians
203新增: double angleRad = (Math.PI / 180.0) * (angle - 90);
204新增:
205新增: double x = radius * Math.Cos(angleRad);
206新增: double y = radius * Math.Sin(angleRad);
207新增:
208新增: return new Point(x, y);
209新增: }
210新增:
211新增: }
212新增:}