<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Data Visualization in C# with GDI+
By Rick Leinecker

Rate This Article: Add This Article To:

Data Visualization in C# with GDI+ - ' Pie Charts'
( Page 2 of 3 )

Pie Charts

The next thing we'll talk about is drawing a pie chart. The demonstration program relies on the Graphics.FillPie method to draw the individual slices of the pie (or arcs). Each data element will be represented by a single pie slice. There are three overrides for the Graphics. FillPie method. All three overrides provide GDI+ with a Brush object, the upper left coordinate of the bounding rectangle, the width and height of the ellipse, the start angle, and the sweep angle. How these overrides differ is in the way the information is given to the method.

The sweep angle is an integer containing the number of degrees (1-359) for the slice. The angle start indicates where in the circle (0-359) that the slice starts. GDI+ then draws a slice from the angle start for as many degrees as is indicated by the sweep angle.

Let's do some simple examples. Let's say that we want to draw a single slice that's 30 degrees. The following code would do the trick.

Graphics.FillPie(objSolidBrush, nXCoordinate, nYCoordinate,
    nWidth, nHeight, 0, 30);

I created a simple program that draws the previous object into a window, and you can see it in the next figure.

Please note, though, that many times that the Size property of Form windows won't have the correct values for your calculations. Instead, you'll usually need to use the ClientRectangle.Size property of the Form window. The demonstration program has a display panel that's attached to the Form window. While panels also have a ClientRectangle property, it should be the same as the Size property.

The following code is the complete method from the demonstration program that draws the bar graph.

// This method draws the pie chart.
private void DrawPieChart(Graphics g)
{
	// Get the width of the display panel.
	int nWidth = pnlDisplay.Size.Width;
	// Get the height of the display panel.
	int nHeight = pnlDisplay.Size.Height;

	// Calculate the midpoint of the display panel.
	int cx = nWidth / 2;
	int cy = nHeight / 2;

	// Now we need width and height
	//   to be the same so that we'll
	//   draw a circle and not an ellipse.
	if (nWidth > nHeight)	
	{
		nWidth = nHeight;
	}
	else
	{
		nHeight = nWidth;
	}

	// Calculate the x and y coordinate of
	//   the upper left corner of the pie (or ellipse)
	//   bounding rectangle.
	int x = cx - ( nWidth / 2 );
	int y = cy - ( nHeight / 2 );

	// Get the count into a local variable
	//   so that it's a little easier and the
	//   code is more readable.
	int nCount = m_DataSetToRender.Length;

	// The start angle will be increment through
	//   the draw process so that the next pie
	//   slice (or arc) will continue drawing where
	//   the last one left off.
	int nStartAngle = 0;

	// We'll cycle through colors, so we need a variable.
	int nColorIndex = 0;

	// Loop through the data.
	for (int i = 0; i < nCount; i++)
	{
		// Draw the slice (or arc).
		g.FillPie(new SolidBrush(m_Colors[nColorIndex]),
			x, y, nWidth, nHeight, nStartAngle,
                    m_DataSetToRender[i]);

		// Update the start angle.
		nStartAngle += m_DataSetToRender[i];

		// Cycle to the next color and wrap around
		//   if we've gotten past the end.
		nColorIndex++;
		if (nColorIndex >= m_Colors.Length)
		{
			nColorIndex = 0;
		}
	}
}

The following figure shows the demonstration program rendering a pie chart.



 
 
>>> More Microsoft Languages Articles          >>> More By Rick Leinecker