提交记录新增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 | 新增:} | |

