Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Scott Waletzko's Blog
Would you like a piece of pie?
Published: 12/15/2007
XMl / RSS

I like pie. Who doesn't like pie? The .NET Graphics object contains a method called "FillPie", which allows you to easily create pie charts without much effort at all. Sadly, the method doesn't put a real pie on your desk, but it's still pretty cool. Here's an example of how to use it:

VB:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    DrawPieChart( _
        New Integer() {30, 30, 30, 10}, _
        New Color() {Color.Red, Color.Blue, Color.Green, Color.Yellow}, _
        e.Graphics, _
        New Rectangle(100, 100, 100, 100) _
        )
End Sub

Private Sub DrawPieChart(ByVal Values() As Integer, ByVal Colors As Color(), ByVal Graphics As Graphics, ByVal Area As Rectangle)

    Dim iStartAngle As Integer = 0
    Dim iSweepAngle As Integer = 0

    ' validate values...
    Dim iTemp As Integer = 0
    For Each iValue As Integer In Values
        iTemp += iValue
    Next
    If (iTemp <> 100) Then
        Throw New System.ArgumentException("Values must sum to 100%.")
    End If

    'validate colors...
    Dim clsColors As New Collections.Generic.Dictionary(Of Color, Color)
    For Each clsColor As Color In Colors
        If (clsColors.ContainsKey(clsColor)) Then
            Throw New System.ArgumentException("Colors must be unique.")
        Else
            clsColors.Add(clsColor, clsColor)
        End If
    Next

    ' validate values and colors...
    If (Values.Length <> Colors.Length) Then
        Throw New System.ArgumentException("Values and Colors must be the same length.")
    End If

    For i As Integer = 0 To Values.Length - 1
        ' calculate sweep...
        iSweepAngle = CType(360 * (Values(i) * 0.01), Integer)
        ' draw pie...
        Graphics.FillPie(New SolidBrush(Colors(i)), Area, iStartAngle, iSweepAngle)
        ' move to next position in pie...
        iStartAngle += iSweepAngle
    Next

End Sub	
	
C#:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
	base.OnPaint(e);
	DrawPieChart(new int[] {30, 30, 30, 10}, new Color[] {Color.Red, Color.Blue, Color.Green, Color.Yellow}, e.Graphics, new Rectangle(100, 100, 100, 100));
}

private void DrawPieChart(int[] Values, Color[] Colors, Graphics Graphics, Rectangle Area)
{

	int iStartAngle = 0;
	int iSweepAngle = 0;

	// validate values...
	int iTemp = 0;
	foreach (int iValue in Values)
	{
		iTemp += iValue;
	}
	if (iTemp != 100)
		throw new System.ArgumentException("Values must sum to 100%.");

	//validate colors...
	Collections.Generic.Dictionary<Color, Color> clsColors = new Collections.Generic.Dictionary<Color, Color>();
	foreach (Color clsColor in Colors)
	{
		if (clsColors.ContainsKey(clsColor))
			throw new System.ArgumentException("Colors must be unique.");
		else
			clsColors.Add(clsColor, clsColor);
	}

	// validate values and colors...
	if (Values.Length != Colors.Length)
		throw new System.ArgumentException("Values and Colors must be the same length.");

	int tempFor1 = Values.Length;
	for (int i = 0; i < tempFor1; i++)
	{
		// calculate sweep...
		iSweepAngle = System.Convert.ToInt32(360 * (Values[i] * 0.01));
		// draw pie...
		Graphics.FillPie(new SolidBrush(Colors[i]), Area, (float)(iStartAngle), (float)(iSweepAngle));
		// move to next position in pie...
		iStartAngle += iSweepAngle;
	}

}	
	



Questions or Comments? .

VB to C# and C# to VB translation provided by Instant C# and Instant VB.