DiagramPage.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace WindowedMyApp
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для DiagramPage.xaml
  20. /// </summary>
  21. public partial class DiagramPage : Page
  22. {
  23. private List<Category> Categories { get; set; }
  24. public DiagramPage()
  25. {
  26. InitializeComponent();
  27. float pieWidth = 650, pieHeight = 650, centerX = pieWidth / 2, centerY = pieHeight / 2, radius = pieWidth / 2;
  28. mainCanvas.Width = pieWidth;
  29. mainCanvas.Height = pieHeight;
  30. Categories = new List<Category>()
  31. {
  32. #region test #1
  33. //new Category
  34. //{
  35. // Title = "Category#01",
  36. // Percentage = 10,
  37. // ColorBrush = Brushes.Gold,
  38. //},
  39. //new Category
  40. //{
  41. // Title = "Category#02",
  42. // Percentage = 30,
  43. // ColorBrush = Brushes.Pink,
  44. //},
  45. //new Category
  46. //{
  47. // Title = "Category#03",
  48. // Percentage = 60,
  49. // ColorBrush = Brushes.CadetBlue,
  50. //},
  51. #endregion
  52. #region test #2
  53. //new Category
  54. //{
  55. // Title = "Category#01",
  56. // Percentage = 20,
  57. // ColorBrush = Brushes.Gold,
  58. //},
  59. //new Category
  60. //{
  61. // Title = "Category#02",
  62. // Percentage = 80,
  63. // ColorBrush = Brushes.LightBlue,
  64. //},
  65. #endregion
  66. #region test #3
  67. //new Category
  68. //{
  69. // Title = "Category#01",
  70. // Percentage = 50,
  71. // ColorBrush = Brushes.Gold,
  72. //},
  73. //new Category
  74. //{
  75. // Title = "Category#02",
  76. // Percentage = 50,
  77. // ColorBrush = Brushes.LightBlue,
  78. //},
  79. #endregion
  80. #region test #4
  81. //new Category
  82. //{
  83. // Title = "Category#01",
  84. // Percentage = 30,
  85. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
  86. //},
  87. //new Category
  88. //{
  89. // Title = "Category#02",
  90. // Percentage = 30,
  91. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
  92. //},
  93. //new Category
  94. //{
  95. // Title = "Category#03",
  96. // Percentage = 20,
  97. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
  98. //},
  99. //new Category
  100. //{
  101. // Title = "Category#04",
  102. // Percentage = 20,
  103. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
  104. //},
  105. //new Category
  106. //{
  107. // Title = "Category#05",
  108. // Percentage = 10,
  109. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
  110. //},
  111. #endregion
  112. #region test #5
  113. //new Category
  114. //{
  115. // Title = "Category#01",
  116. // Percentage = 20,
  117. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
  118. //},
  119. //new Category
  120. //{
  121. // Title = "Category#02",
  122. // Percentage = 30,
  123. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
  124. //},
  125. //new Category
  126. //{
  127. // Title = "Category#03",
  128. // Percentage = 20,
  129. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
  130. //},
  131. //new Category
  132. //{
  133. // Title = "Category#04",
  134. // Percentage = 20,
  135. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
  136. //},
  137. //new Category
  138. //{
  139. // Title = "Category#05",
  140. // Percentage = 10,
  141. // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
  142. //},
  143. #endregion
  144. #region test #6
  145. new Category
  146. {
  147. Title = "Category#01",
  148. Percentage = 20,
  149. ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
  150. },
  151. new Category
  152. {
  153. Title = "Category#02",
  154. Percentage = 60,
  155. ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
  156. },
  157. new Category
  158. {
  159. Title = "Category#03",
  160. Percentage = 5,
  161. ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
  162. },
  163. new Category
  164. {
  165. Title = "Category#04",
  166. Percentage = 10,
  167. ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
  168. },
  169. new Category
  170. {
  171. Title = "Category#05",
  172. Percentage = 5,
  173. ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
  174. },
  175. #endregion
  176. };
  177. detailsItemsControl.ItemsSource = Categories;
  178. // draw pie
  179. float angle = 0, prevAngle = 0;
  180. foreach (var category in Categories)
  181. {
  182. double line1X = (radius * Math.Cos(angle * Math.PI / 180)) + centerX;
  183. double line1Y = (radius * Math.Sin(angle * Math.PI / 180)) + centerY;
  184. angle = category.Percentage * (float)360 / 100 + prevAngle;
  185. Debug.WriteLine(angle);
  186. double arcX = (radius * Math.Cos(angle * Math.PI / 180)) + centerX;
  187. double arcY = (radius * Math.Sin(angle * Math.PI / 180)) + centerY;
  188. var line1Segment = new LineSegment(new Point(line1X, line1Y), false);
  189. double arcWidth = radius, arcHeight = radius;
  190. bool isLargeArc = category.Percentage > 50;
  191. var arcSegment = new ArcSegment()
  192. {
  193. Size = new Size(arcWidth, arcHeight),
  194. Point = new Point(arcX, arcY),
  195. SweepDirection = SweepDirection.Clockwise,
  196. IsLargeArc = isLargeArc,
  197. };
  198. var line2Segment = new LineSegment(new Point(centerX, centerY), false);
  199. var pathFigure = new PathFigure(
  200. new Point(centerX, centerY),
  201. new List<PathSegment>()
  202. {
  203. line1Segment,
  204. arcSegment,
  205. line2Segment,
  206. },
  207. true);
  208. var pathFigures = new List<PathFigure>() { pathFigure, };
  209. var pathGeometry = new PathGeometry(pathFigures);
  210. var path = new Path()
  211. {
  212. Fill = category.ColorBrush,
  213. Data = pathGeometry,
  214. };
  215. mainCanvas.Children.Add(path);
  216. prevAngle = angle;
  217. // draw outlines
  218. var outline1 = new Line()
  219. {
  220. X1 = centerX,
  221. Y1 = centerY,
  222. X2 = line1Segment.Point.X,
  223. Y2 = line1Segment.Point.Y,
  224. Stroke = Brushes.White,
  225. StrokeThickness = 5,
  226. };
  227. var outline2 = new Line()
  228. {
  229. X1 = centerX,
  230. Y1 = centerY,
  231. X2 = arcSegment.Point.X,
  232. Y2 = arcSegment.Point.Y,
  233. Stroke = Brushes.White,
  234. StrokeThickness = 5,
  235. };
  236. mainCanvas.Children.Add(outline1);
  237. mainCanvas.Children.Add(outline2);
  238. }
  239. }
  240. }
  241. }