Microsoft Charts Overview, Page 2
The Series property, like the ChartAreas property, is a collection. A single Series instance contains 3 important properties: the ChartArea property, the ChartType property, and the Points collection property.
- ChartArea: Identifies which ChartArea to use.
- ChartType: Identifies which chart type will be used to represent the data. You have the basic choices of Bar, Column, Pie, and Line, but also some more advance options like CandleStick, Bubble, Kagi, and Pyramid.
- Points: This is a collection of DataPoint objects that contain the X and Y values that are part of the Series and plotted on the chart. Data binding is the most common way to add points, and will be discussed later in this article.
Other common used and good-to-know properties on a Series instance include the following:
- Color: This property is used to individually set each series of data points' colors. By default, this property is blank and the control will vary the colors automatically to distinguish between multiple Series.
- IsValueShownAsLabel: By setting this property to True (default is False) the chart will display the Y value of each data point on the chart itself.
Figure 1.3 Y-value data labels
When combining multiple Series instances in a single ChartArea, you are able to easily represent like data points in a comparable manner. For example, the two separate and single charts below each have 6 points of data.
Click here for larger image
Figure 1.4 Separate data series
Now suppose you wanted to compare the two sets of data points. You could place two MSChart controls next to each other or use two ChartAreas within a chart. While either of those options would work, they would not give you the optimal visual representation of the data. This is why MSChart makes it so simple to combine data points to allow for easy side-by-side comparisons. By adding the data from the second chart as a second Series instance to the first chart we are given a much more visually appealing chart for our comparison.
Figure 1.5 Combined data series
When using multiple Series instances, it is important to keep in mind which ChartType you are using for each Series. Not all ChartType options are compatible together, and others, like the Pie chart for example, don't do a great job at representing separate sets of data.
In summary, the chart control's hierarchy is as follows: the MSChart control may have zero to many ChartAreas. A ChartArea may have zero to many Series. A Series may have zero to many DataPoints.
Data can be bound at either design-time or run-time. To bind at design-time use the Data Source Configuration Wizard that can be found by clicking on the dropdown button for the DataSource property directly on the MSChart control. If you already have data sources configured, you can select them from the list provided.
You can also bind to a data source at run-time through several different methods. There are two different set of methods at different levels in the control that allow binding. One set is on the MSChart control itself and the other is on the Points property of a Series instance. Below is a quick list of the methods and some tips on when to use them.
- DataBind(): The basic method to bind to the set data source.
- DataBindTable(): Binds the chart to a specific data table. Does not allow for binding multiple Y values, different data sources per series or different data sources for X and Y values.
- DataBindCrossTab(): Binds the chart to a data source and allows grouping of data based on a column of data. A separate series is automatically created per unique value in the specified column.
- DataBind(): Binds a Series to a single data source and allows for other properties to be bound to the same data source (e.g. Labels, Tooltips, Legend Text, etc.).
- DataBindXY(): Allows binding to separate data sources for the X value and Y value. It also is used to bind a separate data source per Series.
- DataBindY(): Binds just the Y value of the DataPoints in the Series.
As far as data sources go, the MSChart control can bind to all objects that implement the IEnumerable interface. This includes, but is not limited to, DataReader, DataSet, Array, and List. It also allows you to bind to SqlCommand, OleDbCommand, SqlDataAdapters, and OleDbDataAdapter objects.