Click here to Skip to main content
15,867,453 members
Articles / Multimedia / GDI+
Article

A control to display pie charts with highly customizable formatting

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

Sample Image

Introduction

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.

Background

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:

C#
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 = 
   PieChart.TextDisplayTypes.FitOnly;

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.

C#
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:

C#
try
{
  PieChart1.BeginModification();
  foreach (PieChartItem item in PieChart1.Items)
  {
    item.Text = item.Weight.ToString();
  }
}
finally
{
  PieChart1.EndModification();
}

History

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

License

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

 
AnswerRe: Can this be used on ASP.NET? Pin
mattsj198415-Apr-06 6:48
mattsj198415-Apr-06 6:48 
GeneralThanx Pin
Mohamed Y. Elamrani24-Mar-06 7:48
Mohamed Y. Elamrani24-Mar-06 7:48 
QuestionCF ready? Pin
spif200121-Mar-06 23:19
spif200121-Mar-06 23:19 
AnswerRe: CF ready? Pin
spif200121-Mar-06 23:50
spif200121-Mar-06 23:50 
Question2.0 only ? Pin
Christophe Bayol21-Mar-06 20:16
Christophe Bayol21-Mar-06 20:16 
AnswerRe: 2.0 only ? Pin
mattsj198421-Mar-06 23:08
mattsj198421-Mar-06 23:08 
GeneralRe: 2.0 only ? Pin
Christophe Bayol21-Mar-06 23:18
Christophe Bayol21-Mar-06 23:18 
GeneralOMG.. very nice Pin
clody21-Mar-06 7:09
clody21-Mar-06 7:09 
nice nice nice
so .. you got my 5

Claudiu

Wink | ;)
GeneralLovely Pin
J Whattam20-Mar-06 13:31
J Whattam20-Mar-06 13:31 
Generalooooh Pin
JaseNet19-Mar-06 2:26
JaseNet19-Mar-06 2:26 
Generalvery nice Pin
hariva14-Mar-06 22:15
hariva14-Mar-06 22:15 
JokeReally nice Pin
MansonP14-Mar-06 0:58
MansonP14-Mar-06 0:58 
GeneralSuperb ! Pin
Bernhard Hofmann13-Mar-06 20:46
Bernhard Hofmann13-Mar-06 20:46 
GeneralExcellent Work!!! Pin
oykica13-Mar-06 14:36
oykica13-Mar-06 14:36 
GeneralGot my 5! Pin
Marc Clifton13-Mar-06 10:02
mvaMarc Clifton13-Mar-06 10:02 
GeneralTotally astounding! Pin
Nish Nishant13-Mar-06 9:54
sitebuilderNish Nishant13-Mar-06 9:54 

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.