123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- 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.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WindowedMyApp
- {
- /// <summary>
- /// Логика взаимодействия для DiagramPage.xaml
- /// </summary>
- public partial class DiagramPage : Page
- {
- private List<Category> Categories { get; set; }
- public DiagramPage()
- {
- InitializeComponent();
- float pieWidth = 650, pieHeight = 650, centerX = pieWidth / 2, centerY = pieHeight / 2, radius = pieWidth / 2;
- mainCanvas.Width = pieWidth;
- mainCanvas.Height = pieHeight;
- Categories = new List<Category>()
- {
- #region test #1
- //new Category
- //{
- // Title = "Category#01",
- // Percentage = 10,
- // ColorBrush = Brushes.Gold,
- //},
- //new Category
- //{
- // Title = "Category#02",
- // Percentage = 30,
- // ColorBrush = Brushes.Pink,
- //},
- //new Category
- //{
- // Title = "Category#03",
- // Percentage = 60,
- // ColorBrush = Brushes.CadetBlue,
- //},
- #endregion
- #region test #2
- //new Category
- //{
- // Title = "Category#01",
- // Percentage = 20,
- // ColorBrush = Brushes.Gold,
- //},
- //new Category
- //{
- // Title = "Category#02",
- // Percentage = 80,
- // ColorBrush = Brushes.LightBlue,
- //},
- #endregion
- #region test #3
- //new Category
- //{
- // Title = "Category#01",
- // Percentage = 50,
- // ColorBrush = Brushes.Gold,
- //},
- //new Category
- //{
- // Title = "Category#02",
- // Percentage = 50,
- // ColorBrush = Brushes.LightBlue,
- //},
- #endregion
- #region test #4
- //new Category
- //{
- // Title = "Category#01",
- // Percentage = 30,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
- //},
- //new Category
- //{
- // Title = "Category#02",
- // Percentage = 30,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
- //},
- //new Category
- //{
- // Title = "Category#03",
- // Percentage = 20,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
- //},
- //new Category
- //{
- // Title = "Category#04",
- // Percentage = 20,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
- //},
- //new Category
- //{
- // Title = "Category#05",
- // Percentage = 10,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
- //},
- #endregion
- #region test #5
- //new Category
- //{
- // Title = "Category#01",
- // Percentage = 20,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
- //},
- //new Category
- //{
- // Title = "Category#02",
- // Percentage = 30,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
- //},
- //new Category
- //{
- // Title = "Category#03",
- // Percentage = 20,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
- //},
- //new Category
- //{
- // Title = "Category#04",
- // Percentage = 20,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
- //},
- //new Category
- //{
- // Title = "Category#05",
- // Percentage = 10,
- // ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
- //},
- #endregion
- #region test #6
- new Category
- {
- Title = "Category#01",
- Percentage = 20,
- ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4472C4")),
- },
- new Category
- {
- Title = "Category#02",
- Percentage = 60,
- ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ED7D31")),
- },
- new Category
- {
- Title = "Category#03",
- Percentage = 5,
- ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")),
- },
- new Category
- {
- Title = "Category#04",
- Percentage = 10,
- ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#5B9BD5")),
- },
- new Category
- {
- Title = "Category#05",
- Percentage = 5,
- ColorBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A5A5A5")),
- },
- #endregion
- };
- detailsItemsControl.ItemsSource = Categories;
- // draw pie
- float angle = 0, prevAngle = 0;
- foreach (var category in Categories)
- {
- double line1X = (radius * Math.Cos(angle * Math.PI / 180)) + centerX;
- double line1Y = (radius * Math.Sin(angle * Math.PI / 180)) + centerY;
- angle = category.Percentage * (float)360 / 100 + prevAngle;
- Debug.WriteLine(angle);
- double arcX = (radius * Math.Cos(angle * Math.PI / 180)) + centerX;
- double arcY = (radius * Math.Sin(angle * Math.PI / 180)) + centerY;
- var line1Segment = new LineSegment(new Point(line1X, line1Y), false);
- double arcWidth = radius, arcHeight = radius;
- bool isLargeArc = category.Percentage > 50;
- var arcSegment = new ArcSegment()
- {
- Size = new Size(arcWidth, arcHeight),
- Point = new Point(arcX, arcY),
- SweepDirection = SweepDirection.Clockwise,
- IsLargeArc = isLargeArc,
- };
- var line2Segment = new LineSegment(new Point(centerX, centerY), false);
- var pathFigure = new PathFigure(
- new Point(centerX, centerY),
- new List<PathSegment>()
- {
- line1Segment,
- arcSegment,
- line2Segment,
- },
- true);
- var pathFigures = new List<PathFigure>() { pathFigure, };
- var pathGeometry = new PathGeometry(pathFigures);
- var path = new Path()
- {
- Fill = category.ColorBrush,
- Data = pathGeometry,
- };
- mainCanvas.Children.Add(path);
- prevAngle = angle;
- // draw outlines
- var outline1 = new Line()
- {
- X1 = centerX,
- Y1 = centerY,
- X2 = line1Segment.Point.X,
- Y2 = line1Segment.Point.Y,
- Stroke = Brushes.White,
- StrokeThickness = 5,
- };
- var outline2 = new Line()
- {
- X1 = centerX,
- Y1 = centerY,
- X2 = arcSegment.Point.X,
- Y2 = arcSegment.Point.Y,
- Stroke = Brushes.White,
- StrokeThickness = 5,
- };
- mainCanvas.Children.Add(outline1);
- mainCanvas.Children.Add(outline2);
- }
- }
- }
- }
|