aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Controls/MultiPieChart.xaml.cs
blob: 2415573fec7a007d4f0eb7fa314ea10940b4c931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Tango.PPC.Common.Controls
{
	/// <summary>
	/// Interaction logic for MultiPieChart.xaml
	/// </summary>
	public partial class MultiPieChart : UserControl
	{
		private const string DefaultDataBrush = "#939496";

		#region dependency properties

		public static readonly DependencyProperty SizeProperty =
		  DependencyProperty.Register( "Size", typeof( double ), typeof( MultiPieChart ), new PropertyMetadata( 100.0, OnPiePropertyChanged ) );

		public static readonly DependencyProperty InnerPieSliceFillProperty =
		   DependencyProperty.Register( "InnerPieSliceFill", typeof( Brush ), typeof( MultiPieChart ), new PropertyMetadata( CreateBrush( "#939496" ), OnPiePropertyChanged ) );

		public static readonly DependencyProperty OuterPieSliceFillProperty =
		   DependencyProperty.Register( "OuterPieSliceFill", typeof( Brush ), typeof( MultiPieChart ), new PropertyMetadata( CreateBrush( "#D0D1D3" ), OnPiePropertyChanged ) );

		public static readonly DependencyProperty DataListProperty =
		   DependencyProperty.Register( "DataList", typeof( IList<double> ), typeof( MultiPieChart ), new PropertyMetadata( null, OnDataListPropertyChanged ) );

		public static readonly DependencyProperty DataBrushesProperty =
		   DependencyProperty.Register( "DataBrushes", typeof( MultiPieChartBrushCollection ), typeof( MultiPieChart ), new PropertyMetadata( new MultiPieChartBrushCollection(), OnPiePropertyChanged ) );

		public double Size
		{
			get { return (double)GetValue( SizeProperty ); }
			set { SetValue( SizeProperty, value ); }
		}

		public Brush InnerPieSliceFill
		{
			get { return (Brush)GetValue( InnerPieSliceFillProperty ); }
			set { SetValue( InnerPieSliceFillProperty, value ); }
		}

		public Brush OuterPieSliceFill
		{
			get { return (Brush)GetValue( OuterPieSliceFillProperty ); }
			set { SetValue( OuterPieSliceFillProperty, value ); }
		}

		public IList<double> DataList
		{
			get { return (IList<double>)GetValue( DataListProperty ); }
			set { SetValue( DataListProperty, value ); }
		}

		public MultiPieChartBrushCollection DataBrushes
		{
			get { return (MultiPieChartBrushCollection)GetValue( DataBrushesProperty ); }
			set { SetValue( DataBrushesProperty, value ); }
		}

		#endregion

		#region constructor

		public MultiPieChart()
		{
			InitializeComponent();
            this.Loaded += (_, __) => { CreatePieChart(); };
		}

		#endregion

		#region overrides

		#endregion

		#region events

		private static void OnPiePropertyChanged( DependencyObject dep, DependencyPropertyChangedEventArgs ev )
		{
			//var chart = (MultiPieChart)dep;

			//if ( chart.IsInitialized )
			//{
			//	chart.CreatePieChart();
			//}
		}

		private static void OnDataListPropertyChanged( DependencyObject dep, DependencyPropertyChangedEventArgs ev )
		{
			var chart = (MultiPieChart)dep;

			var collection = ev.OldValue as INotifyCollectionChanged;
			if ( collection != null )
			{
				collection.CollectionChanged -= chart.DataListCollectionChanged;
			}

			collection = ev.NewValue as INotifyCollectionChanged;
			if ( collection != null )
			{
				collection.CollectionChanged += chart.DataListCollectionChanged;
			}
		}


		private void DataListCollectionChanged( object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs )
		{
			//CreatePieChart();
		}

		#endregion

		#region private methods

		private void CreatePieChart()
		{
            Size = ActualWidth;

			if ( _pieChartImage != null )
			{
				if ( !double.IsNaN( Size ) && DataList != null && DataList.Any() )
				{
					_pieChartImage.Width = _pieChartImage.Height = Width = Height = Size;

					var di = new DrawingImage();
					_pieChartImage.Source = di;

					var dg = new DrawingGroup();
					di.Drawing = dg;

					if ( DataList.Count > 1 )
					{
						var total = DataList.Sum();
						var startPoint = new Point( Width / 2, 0 );
						double radians = 0;

						for ( int i = 0; i < DataList.Count; i++ )
						{
							var data = DataList[i];
							var dataBrush = GetBrushFromList( i );
							var percentage = data / total;

							Point endPoint;
							var angle = 360 * percentage;

							if ( i + 1 == DataList.Count )
							{
								endPoint = new Point( Width / 2, 0 );
							}
							else
							{
								radians += ( Math.PI / 180 ) * angle;
								var endPointX = Math.Sin( radians ) * Height / 2 + Height / 2;
								var endPointY = Width / 2 - Math.Cos( radians ) * Width / 2;
								endPoint = new Point( endPointX, endPointY );
							}

							dg.Children.Add( CreatePathGeometry( dataBrush, startPoint, endPoint, angle > 180 ) );

							startPoint = endPoint;
						}
					}
					else
					{
						dg.Children.Add( CreateEllipseGeometry( GetBrushFromList( 0 ) ) );
					}
				}
				else
				{
					_pieChartImage.Source = null;
				}
			}
		}

		private GeometryDrawing CreatePathGeometry( Brush brush, Point startPoint, Point arcPoint, bool isLargeArc )
		{
			/*
			 * <GeometryDrawing Brush="@Brush">
				  <GeometryDrawing.Geometry>
					 <PathGeometry>
						<PathFigure StartPoint="@Size/2">
						   <PathFigure.Segments>
							  <LineSegment Point="@startPoint"/>
							  <ArcSegment Point="@arcPoint"  SweepDirection="Clockwise" Size="@Size/2"/>
							  <LineSegment Point="@Size/2"/>
						   </PathFigure.Segments>
						</PathFigure>
					 </PathGeometry>
				  </GeometryDrawing.Geometry>
			   </GeometryDrawing>
			 * */

			var midPoint = new Point( Width / 2, Height / 2 );

			var drawing = new GeometryDrawing { Brush = brush };
			var pathGeometry = new PathGeometry();
			var pathFigure = new PathFigure { StartPoint = midPoint };

			var ls1 = new LineSegment( startPoint, false );
			var arc = new ArcSegment
			{
				SweepDirection = SweepDirection.Clockwise,
				Size = new Size( Width / 2, Height / 2 ),
				Point = arcPoint,
				IsLargeArc = isLargeArc
			};
			var ls2 = new LineSegment( midPoint, false );

			drawing.Geometry = pathGeometry;
			pathGeometry.Figures.Add( pathFigure );

			pathFigure.Segments.Add( ls1 );
			pathFigure.Segments.Add( arc );
			pathFigure.Segments.Add( ls2 );

			return drawing;
		}

		private GeometryDrawing CreateEllipseGeometry( Brush brush )
		{
			var midPoint = new Point( Width / 2, Height / 2 );

			var drawing = new GeometryDrawing { Brush = brush };
			var ellipse = new EllipseGeometry( midPoint, Size / 2, Size / 2 );

			drawing.Geometry = ellipse;

			return drawing;
		}

		private static SolidColorBrush CreateBrush( string brush )
		{
			var color = ColorConverter.ConvertFromString( brush );
			if ( color != null )
			{
				return new SolidColorBrush( (Color)color );
			}

			return null;
		}

		private Brush GetBrushFromList( int index )
		{
			if ( DataBrushes == null || !DataBrushes.Any() )
			{
				return CreateBrush( DefaultDataBrush );
			}
			else
			{
				var modIndex = index % DataBrushes.Count;
				return DataBrushes[modIndex];
			}
		}

		#endregion
	}
}