Tutorial: Data Binding a Chart to a Database
This tutorial demonstrates how to bind data from a database, as well as how to use tooltips and adjust the axis labels in your chart.
To complete this tutorial, you must have installed the AdventureWorks2008 sample OLTP database installed in an instance of SQL Server (such as SQL Server 2008 Express). To install the sample database, go to www.codeplex.com.
Binding data from a Database
You first bind data from a data source control to the Chart control, then bind specific columns from the data source to the X value and Y values of a series.
To bind data from a database to a chart
In the design surface of your Windows Forms or web application, drag and drop the Chart control from the Toolbox:
In the Properties window of the Chart control, in the Data category, click the DataSourceID (ASP.NET) or DataSource (Windows Forms) property, then click the drop-down arrow, and then select <New data source…>.
In the Choose a Data Source Type page, select Database, and then click Next.
In the Choose your Data Connection page, click New Connection.
In the Add Connection dialog box, make sure that the Data source field is set to Microsoft SQL Server (SqlClient), and then provide the server name and the database name of the AdventureWorks2008 database.
Click Test Connection, then click OK.
Click Next twice.
In the Configure the Select Statement page, select Specify a custom SQL statement, and then click Next.
Copy and paste the following query into the SQL statement field, and then click Next.
select LastName, SalesYTD, SalesLastYear from HumanResources.Employee as e inner join Person.Person as p on e.BusinessEntityID = p.BusinessEntityID inner join Sales.SalesPerson as s on e.BusinessEntityID = s.BusinessEntityID
Click Test Query, and then click Finish.
A SQL data source is now bound to the Chart control with the data columns specified in the query.
To bind data to the X and Y values in the chart
Open the Series Collection Editor for the Chart control.
In the Properties window for the default series, in the Data Source category, click the XValueMember property, then click the drop-down arrow, and then select LastName.
The XValueMember drop-down list displays all the columns returned by the data source that is bound to the Chart control.
Click the YValueMembers property, then click the drop-down arrow, and then select SalesYTD.
The X value of the default series is now bound to the LastName column, and the first Y value of the default series is now bound to the SalesYTD column.
In the Series Collection Editor, click Add, and then do the same to the new series with the following values:
Run the application.
You can now see two series that display data points that are automatically populated from the data source. You may also notice that the chart does not display all the employees; this is because the Chart control automatically adjusts the labels for readability.
Adjusting Axis Labels and Titles
You use the Axes collection property in the ChartArea Collection Editor to customize labels and titles of the chart area axes.
To adjust the axis labels and add titles
Open the ChartArea Collection Editor for the Chart control.
In the Properties window for the default chart area, in the Axes category, click the Axes collection property, and then click the ellipsis button (…). This opens the Axis Collection Editor.
The Axes collection (an Axis array) contains the axes of the chart area.
In the Properties window for the (primary) X axis, in the Labels category, expand the LabelStyle property, then set its Interval property to 1, and then verify that the IntervalStyle property is not set.
This causes the Chart control to show the label for every data point that is displayed.
In the Title category, set the Title property to "Employee".
In the Properties window for the (primary) Y axis, in the Title category, set the Title property to "Sales".
Click OK twice.
Run the application.
The Chart control now displays every employee on the primary X axis by automatically arranging them at a 90-degree angle.
Adding Tooltips to the Series
Now you will add tooltips to the two series to distinguish between the data points.
To add tooltips to the series
Open the Series Collection Editor again for the Chart control.
Select the first series.
In the Properties window for the first series, in the Map Area category, set the Tooltip property to "#VALX: #VAL sales this year.".
Notice that if you click the ellipsis button in the Tooltip property's value field, the String Keyword Editor opens. This dialog box helps you build a tooltip string with keywords.
In the Properties window for the first series, in the Map Area category, set the Tooltip property to "#VALX: #VAL sales last year.".
Run the application, and then hold the cursor on top of each data point to see its tooltip.
You have now learned how to bind data from a database, as well as how to use tooltips and adjust the axis labels in your chart. To learn how to use events to customize your chart, see Tutorial: Customizing a Chart with Events.