Share via


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

  • Simple binding for X and Y values.

  • Automatic series creation, based on the number of columns in the data source.

  • Only one pass through data required.

  • No multiple Y values per series.

  • All series have same X value, or X value is not set.

  • No binding for extended chart properties (for example, tooltips).

Chart.DataSource

and

Chart.DataBind

  • The data source is iterated once for all series.

  • Supports multiple Y values.

  • No binding for extended chart properties (for example, tooltips).

Points.DataBind(X)Y

  • Supports multiple data sources, including separate data sources for X and Y values.

  • Supports multiple Y values.

  • More flexible compared to the two methods listed above.

  • No binding for extended chart properties (for example, tooltips).

  • One iteration through data per series.

Points.DataBind

Same as above, plus:

  • Supports binding for extended chart properties (for example, tooltips).

  • One iteration through data per series.

  • Does not support different data sources for X and Y values of a series.

Chart.DataBindCrossTable

  • Only one pass through data required.

  • Automatically creates one series for each unique value in a specified column (unique values used to group data).

  • None

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

Other Resources

Data Binding and Manipulation