Microsoft Charts Overview
The Legends property is again a collection (this time of Legend objects). To quickly grasp what you can do with Legends, think of them as simple table. You have Cells, which make up Cell Columns. By default there are two Cell Columns. As you can see from previous examples, the default settings uses the Series instance name and the color of that Series on the chart. Legends can be extended by adding additional Cell Columns to the CellColumn collection. Column headers can also be added to give more meaning to the legend. In the below example, the headers "Name" and "Color" have been added to a default Legend instance.
Click here for larger image
Figure 1.6 Simple legend
Finally, the chart's legend can be positioned two different ways. You can have the Legend automatically positioned in a docking like fashion. The default is outside the ChartArea, aligned Near, and docked Right. The other option is to manually position it with exact coordinates if your application requires. A variant on the docking option is to dock the legend within the ChartArea. This is done by setting the DockedToChartArea property to the ChartArea object you wish to dock the legend.
Click here for larger image
Figure 1.7 More complex legend
Titles are like the other previously discussed items. You create individual instances of a Title and the chart control holds a collection of these Title instances in the Titles collection. The simplest way to think about a Title is as a Label control that is internally docked within a panel that itself is docked within the chart control. This means that you can have your title to be top-centered, left- centered, top-left, right-bottom, and the list could go on and on. The only "docking" functionality you may be familiar with that is unavailable is "Fill."
In the below example, a bold Title with a font size of 12 has been added to the chart called "My First Chart."
Figure 1.8 Chart with a customized title
Tips and Tricks
- 3D w/Transparency: A cool way to add a little pizzazz to your 3D charts is to use some transparency in the Series. To do this via the designer, you will the Alpha value to the RBG code for the color you are using. In the properties window, select the Series collection, select a Series, and, in the properties window for the Series, add the Alpha value before the existing 3 RBG values. You can accomplish the same thing byusing the code below:
chart1.Series["Series1"].Color = Color.FromArgb(220, 123, 123, 123);
- Leverage the Designer: There is such a robust set of properties and configurable options in the charting control that trying to configure all properties at run-time in code is too cumbersome in most cases. In cases where your application will use the chart in a "static" fashion (i.e., always a bar graph with two Series), the designer allows you to configure and view the chart at design time. This will give you the advantage of seeing how the chart will render without the need for multiple debug runs. On the other hand, if you intend to use the chart in a "dynamic" fashion (i.e., sometimes a pie graph and other times a line graph, which the user can change at run time), you will be required to alter the design at run-time using code. Instead of creating all the code manually for each type or circumstance, create it using the designer and then lift the code from the designer file on your form. This saves you time both in coding and figuring out which properties to use.
- Logical Names: This is an all around good programming practice, but it becomes even more important when dealing with properties that aren't strongly typed. When creating ChartAreas, Series, Titles and the like, be sure to name them with logical and easy to remember (and type) names. For example,
(chart1.Series["Series1"] , chart1.Series["Series2"]) is more logical and therefore less likely to introduce errors than
(chart1.Series["salesrepearningquarter"] , chart1.Series["salesrepcomdatabyquarter"] ).
The Microsoft Chart Controls offer robust, flexible, customizable chart controls for displaying information to your users in the most easily digestible ways. We have covered the chart properties at a high level. Now you can go out and start creating charts to wow your application users.
About the Authors
Matt Goebel is a manager with Crowe Horwath LLP in the Indianapolis, Indiana, office. He can be reached at 317.208.2555 or matt .firstname.lastname@example.org.
Rachel Baker is a senior developer with Crowe Horwath LLP in the Oak Brook, Illinois, office. She can be reached at 630.990.4434 or rachel .email@example.com.
Page 3 of 3