Data Binding Microsoft Chart Control
Data Binding Microsoft Chart Control
RATE THIS
Alex Gorev
20 Feb 2009 9:42 PM
32
Overview
When you add a new visualization in your application, the very first thing you do, is bind it to the
data. Microsoft Chart control allows many different ways of populating data in the chart and it is
important to understand the differences between them and being able to choose the best
approach. I put together all the possible data binding options and the scenarios when they should
be used.
Data Sources
Before jumping into the binding methods I want to quickly list all the supportable data sources
which can be used in chart binding methods. Keep in mind that not all methods accept all source
types.
SqlCommand
OleDbCommand
SqlDataAdapter
OleDbDataAdapter
DataView
DataSet
DataReader
List
Array
IList
IListSource
IEnumerable
X Value - Defines location of the data point along X axis. If not specified, data point index
in the series will be used.
Additional Y Values - Chart types like Bubble or Stock require multiple Y values. You can
also bind additional Y values in the regular chart types, if you want to use those values for
labels or tooltips.
Category - When you displaying categories on the X axis, they are stored in the AxisLabel
property of the data point. All data binding methods will automatically detect the type of
the data and you should not worry about it.
Other Properties - Some of the binding methods allow you to set additional properties
like data point labels, tooltips and others.
Chart.DataBindTable
Simple binding
for X and Y
values.
No multiple
Y values
per series.
Automatic
series creation,
based on the
number of
columns in the
data source.
All series
have same
X value, or
it is not set.
No binding
for
extended
chart
properties
like
tooltips.
Chart.DataSource and
Chart.DataBind
Can be used at
design-time.
Supports
multiple Y
values.
Supports
multiple data
sources,
including
separate data
source for X
and Y values.
Supports
multiple Y
values.
Provides more
flexibility than
methods above
Same as the
above, plus:
Supports
binding for
extended chart
properties like
tooltips.
Automatically
creates series
for each unique
value in
specified
column used to
group the data.
No binding
for
extended
chart
properties
like
tooltips.
No binding
for
extended
chart
properties
like
tooltips.
Does not
support
different
data
sources for
X and Y
values of a
series.
Only single
level
grouping is
supported.
Points.DataBind(X)Y
Points.DataBind
Chart.DataBindCrossTab
Chart.DataSource property
Chart DataSource property is the only way to bind chart at design-time. In addition to specifying
the DataSource property you also need to create series and set their YValueMembers and
optionally XValueMember properties. Multiple Y values can be bound if you specify comma
separated list of members in the YValueMembers property.
Chart will automatically bind itself to specified data source and members just before rendering. You
can force chart to bind at any moment by calling Chart.DataBind() method.
PointProperty=FieldName[{Format}][,PointProperty= FieldName[{Format}]]
A list of these properties
includes: AxisLabel, Tooltip, Label, LegendText, LegendTooltip andCustomPropertyName (
the name of a custom property).
This method does not allow you to specify separate data sources for the X and Y values of a series
(for this functionality use the DataBind(X)Y methods described in the section above).
Example
Code below demonstrates how to bind X and Y values of the series to columns "Name" and "Sales"
respectively. The Label and Tooltip properties of the resulting data points are also bound to the
"Commissions" and "Year" column.
Chart1.Series["Series1"].Points.DataBind(
Label=Commissions{C2}");
Points.DataBind[X]Y
The DataBindXY method allows for the binding of X and Y values of a series, while DataBindY binds
Y values only. When binding X and Y values using DataBindXY the same or different data sources
may be used.
Some data sources support multiple values (e.g. data sources that have columns of data). For
example, OleDbDataReader may have access to more than one column of data. If the column to
be used is not specified, then the first available column will be used. To use a column other than
the first one just specify the column name in the method call.
Note that when specifying the column names to be bound to the Y values a comma can be
embedded as part of a column name by specifying a double comma.
Example
Code below demonstrates how to bind series points to the array of doubles.
DataGrid.DataBind();
// Closes the connection to the data source. This is the preferred
// method of closing any open connection.
myCommand.Connection.Close();
Empty Points - Understand what empty points are and how you can use them. Right now
the best source of the information is Chart Sample Environment but I will blog about it later.