Binding Data to Series (Chart Controls)
This topic describes the various data-binding techniques. For a tutorial on binding data to series, see Tutorial: Data Binding a Chart to a Database.
Data Binding Methods
Method | Advantages | Disadvantages |
---|---|---|
Chart.DataBindTable |
|
|
Chart.DataSource and Chart.DataBind |
|
|
Points.DataBind(X)Y |
|
|
Points.DataBind |
Same as above, plus:
|
|
Chart.DataBindCrossTable |
|
|
Data Sources
The following are the possible data sources used for binding:
DataView.
Data Readers (SQL, OleDB).
DataSet (DataSource data-binding ONLY).
Arrays.
Lists.
All IEnumerable objects.
Note
When using non-tabular data sources such as lists, or arrays, you can bind only Y values, regardless of the type of data-binding method used. This is because columns cannot be specified for X values and other chart properties, such as Tooltip.
Example
The following code demonstrates how to bind a column chart to an Access database table. The table "SALESCOUNTS" has a "SalesRep" column with the names of the sales people, a "Prod A" column, a "Prod B" column, a "Prod C" column, a "Prod D" column, a "Prod E" column, and an "Other" column. The DataBindTable method automatically creates six Series objects: one for each product column and one for the "Other" column. One data point in each series automatically exists per record. The method also binds the X values of the six series to the "SalesRep" column and uses it for the axis labels of the data points.
Imports System.Data.OleDb
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
...
' Resolve the address to the Access database. We assume database is
' in Bin folder.
Dim fileNameString As String = "chartdata.mdb"
' Initialize a connection string.
Dim myConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString
' Define the database query.
Dim mySelectQuery As String = "SELECT * FROM SALESCOUNTS;"
' Create a database connection object using the connection string.
Dim myConnection As OleDbConnection = New OleDbConnection(myConnectionString)
' Create a database command on the connection using query.
Dim myCommand As OleDbCommand = New OleDbCommand(mySelectQuery,myConnection)
' Open the connection.
myCommand.Connection.Open()
' Create a database reader.
Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
' Specify the Name column to be used for point's X values.
chart1.DataBindTable(myReader,"SalesRep")
' Close the connection.
myConnection.Close()
' This is a loop to set all created charts appearance with custom attribute.
Dim series As Series
For Each series In chart1.Series
series.CustomAttributes = "DrawingStyle=LightToDark"
Next
using System.Data.OleDb;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
...
// Resolve the address to the Access database. We assume database is
// in Bin folder.
string fileNameString = "chartdata.mdb";
// Initialize a connection string.
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// Define the database query.
string mySelectQuery="SELECT * FROM SALESCOUNTS;";
// Create a database connection object using the connection string.
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// Create a database command on the connection using query.
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
// Open the connection.
myCommand.Connection.Open();
// Create a database reader.
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Specify the Name column to be used for point's X values.
chart1.DataBindTable(myReader,"SalesRep");
// Close the connection.
myConnection.Close();
// This is a loop to set all created charts appearance with custom attribute.
foreach (Series series in chart1.Series)
{
series.CustomAttributes = "DrawingStyle=LightToDark";
}
See Also
Reference
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting