Click here to Skip to main content
15,440,620 members
Articles / Multimedia / GDI+
Posted 13 Mar 2006


235 bookmarked

A control to display pie charts with highly customizable formatting

Rate me:
Please Sign up or sign in to vote.
4.96/5 (135 votes)
25 Aug 20064 min read
A control to display pie charts with highly customizable formatting.

Sample Image


My idea was to create a control that can generate three-dimensional pie chart controls. I found several other controls on CodeProject, but none of them let me specify inclination, rotation, thickness, radius, and surface brightness and transparency. Additionally, many were lacking event-driven hooks, such as events which fire on items being clicked or mouse focus changing. Furthermore, none of them were written in the style of a standard Windows Forms control, e.g., the ListView control with an Items collection for storing ListViewItems. So, I set out to write a control that would offer these features, along with an easily extensible interface.


As a basis for my PieChart, I relied heavily on the look-and-feel of the pie chart control written by Julijan Sribar. In addition to the features offered by his control, the PieChart allows you to specify the inclination of the pie in the plane, as well as whether or not the radius is auto-sized or fixed. As a further aide to designing with this control, the Items collection of the PieChart is editable at design time in Visual Studio.

Using the code

The control is structured much like a standard Windows Forms control, in terms of use. The PieChart itself contains an Items property, which is of type PieChart.ItemCollection. This collection stores objects of type PieChartItem. Each PieChartItem is comprised of the Text, ToolTipText, Color, Offset, and Weight properties. To create and use the control, use the Windows Forms designer in Visual Studio to add a PieChart to a form. Then, you can use the control as easily as this:

PieChart pieControl = new PieChart();
// add an item with weight 10, color Red,
// text "Text", and tool-tip text "ToolTipText"
pieControl.Items.Add(new PieChartItem(10, 
      Color.Red, "Text", "ToolTipText"));
// add another item with weight 5, color Blue, text "Blue",
// tool-tip text "BlueTips", and an offset
// of 25 pixels from the center of the pie
pieControl.Items.Add(new PieChartItem(5, 
       Color.Blue, "Blue", "BlueTips"));
// set the control to automatically
// fit the pie inside the control
pieControl.AutoSizePie = true;
// set the control to only show the
// text if it fits inside its slice
pieControl.TextDisplayMode = 

The control defines special styles for normal items and focused items. An item becomes focused when the mouse moves over the control. The default item styles can be set using the ItemStyle property, while the focused item styles can be set with the FocusedItemStyle property. Below are the styles allowed for items.

  • SurfaceAlphaTransparency

    A floating-point value between 0 and 1 that is multiplied by the item's color's alpha channel to determine the item's transparency.

  • SurfaceBrightnessFactor

    A floating-point value between -1 and 1 that modifies the item's surface brightness. Positive values increase the brightness (with 1 increasing the brightness to white), while negative values decrease the brightness (with -1 causing black).

  • EdgeBrightnessFactor

    A floating-point value between -1 and 1 that modifies the item's edge brightness using the same algorithm as the SurfaceBrightnessFactor.

You may want to define custom actions when a property changes or when the state of the control is modified. Below is a list of the additional events that are fired by the control.

  • ItemClicked
  • ItemFocusChanging
  • ItemFocusChanged
  • AutoSizePieChanged
  • RadiusChanged
  • InclinationChanged
  • RotationChanged
  • ThicknessChanged
  • ShowEdgesChanged
  • TextDisplayModeChanged
  • ShowToolTipsChanged

Printing and Saving

You can use the control to print and save charts. In the demo, the File->Save menu allows you to save the chart as a PNG, JPEG, GIF, or BMP. The image will have all of the same properties as the control, and will be sized to fit the chart using the Padding, Radius, and AutoSizePie properties of the PieChart. However, you can specify different sizes in the code if you like. Also, the control can print your chart using a PrintDocument. Simply register the PrintDocument with the PieChart using the PieChart.AttachPrintDocument method, and then invoke the PrintDocument.Print method to initiate the printing. Note: I do not have access to a printer, and have only tested printing to the Microsoft Office Document Image virtual printer. Please alert me to any printing bugs.

Points of Interest

I personally find it annoying when a control attempts to redraw itself constantly while editing items in a loop. For example, you might write a loop that iterates through your items and changes the text of all of the items.

foreach (PieChartItem item in PieChart1.Items)
  item.Text = item.Weight.ToString();

Since changing the Text property of a PieChartItem causes the control to be redrawn, the control will redraw once for every item. To avoid this kind of a problem, I created the functions BeginModification and EndModification. Each time BeginModification is called, a counter is incremented; and each time EndModification is called, the counter is decremented. As long as the counter is greater than 0, changes to the control will not cause the control to refresh. Upon calling EndModification the final time, the control will refresh if it has been structurally or visually modified. Note that calling BeginModification without a corresponding call to EndModification will cause the control to never redraw! Therefore, you should put the call to EndModification in the finally block of a try-catch block. The following code modifies the loop so that it does not redraw for every item:

  foreach (PieChartItem item in PieChart1.Items)
    item.Text = item.Weight.ToString();


  • Revision 0: Original version.
  • Revision 1: Added support for printing, saving, and changing the control's padding.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United States United States
I am a software engineer. When I'm not coding, I enjoy reading, especially sci-fi and fantasy. I also like getting outside to run and hike, probably so that my pale skin isn't criminally blinding.

Comments and Discussions

QuestionMultipule selection Pin
calanit12-Dec-11 0:28
Membercalanit12-Dec-11 0:28 
GeneralGreat Control Pin
Vishwanath Patil21-Sep-09 23:16
MemberVishwanath Patil21-Sep-09 23:16 
GeneralTop Notch! Pin
Adrian Cole23-Jun-09 18:09
MemberAdrian Cole23-Jun-09 18:09 
Generalhats off [modified] Pin
harsh3726-Jan-09 4:52
Memberharsh3726-Jan-09 4:52 
GeneralExcellent! Pin
El Fenomeno5-Feb-08 12:58
MemberEl Fenomeno5-Feb-08 12:58 
QuestionOverlap Slices ?? Pin
spacemanksc15-Aug-07 22:40
Memberspacemanksc15-Aug-07 22:40 
GeneralDoughnut support Pin
Paul Selormey16-Jan-07 21:16
MemberPaul Selormey16-Jan-07 21:16 
GeneralRe: Doughnut support Pin
Angelo Cresta14-Apr-21 9:09
professionalAngelo Cresta14-Apr-21 9:09 
Questionhow to get random slice color Pin
Edward11119-Nov-06 14:50
MemberEdward11119-Nov-06 14:50 
AnswerRe: how to get random slice color Pin
mattsj198421-Nov-06 19:35
Membermattsj198421-Nov-06 19:35 
QuestionDo you have a similar BarChart component? Pin
rmgalante18-Nov-06 0:22
Memberrmgalante18-Nov-06 0:22 
AnswerRe: Do you have a similar BarChart component? Pin
mattsj198421-Nov-06 19:19
Membermattsj198421-Nov-06 19:19 
GeneralVery nice Pin
Popangler3-Nov-06 23:01
MemberPopangler3-Nov-06 23:01 
GeneralRe: Very nice Pin
mattsj198414-Nov-06 15:42
Membermattsj198414-Nov-06 15:42 
GeneralCode for Mouse Wheel Rotation support =) Pin
Marcos Meli1-Nov-06 15:49
MemberMarcos Meli1-Nov-06 15:49 
GeneralEXCELLENT!!! Pin
TheCardinal19-Oct-06 10:51
MemberTheCardinal19-Oct-06 10:51 
GeneralExcellent Pin
Antony M Kancidrowski2-Oct-06 12:46
MemberAntony M Kancidrowski2-Oct-06 12:46 
AnswerRe: Excellent Pin
mattsj19842-Oct-06 14:43
Membermattsj19842-Oct-06 14:43 
GeneralPlease help me Pin
O.S.Chakravarthi24-Aug-06 19:05
MemberO.S.Chakravarthi24-Aug-06 19:05 
GeneralRe: Please help me Pin
mattsj198424-Aug-06 20:22
Membermattsj198424-Aug-06 20:22 
GeneralRe: Please help me Pin
mattsj198424-Aug-06 20:33
Membermattsj198424-Aug-06 20:33 
GeneralRe: Please help me Pin
Captainx18-Sep-06 2:03
MemberCaptainx18-Sep-06 2:03 
GeneralRe: Please help me Pin
mattsj198419-Sep-06 20:12
Membermattsj198419-Sep-06 20:12 
QuestionCode Usage Pin
BigD0710-Aug-06 16:47
MemberBigD0710-Aug-06 16:47 
AnswerRe: Code Usage Pin
mattsj198410-Aug-06 21:13
Membermattsj198410-Aug-06 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.