A few months back Microsoft released a free charting control for .NET.  You can download the charting control and the plugin for VS2008 at the following links:

Once these two packages are installed, we can go ahead and start creating our first charting project.  For this project, we will create a pie chart illustrating the amount of customers grouped by region using the AdventureWorks sample database for SQL Server 2005.  The AdventureWorks sample database can be downloaded freely here:

Fire up VS2008 and you should see a new item in your toolbox under the data heading:

charttoolbox-thumb.png

Go ahead and drag a chart control onto the page and we will begin designing our chart.  For this demo, we will be creating a 3-D pie chart.

Let’s also drag a SqlDataSource control onto the page.  Configure it to connect to the AdventureWorks database and to issue the following SELECT query:

SELECT Sales.SalesTerritory.Name AS RegionName, COUNT(Sales.Customer.TerritoryID) AS CustomerCount
FROM Sales.SalesTerritory INNER JOIN Sales.Customer ON Sales.SalesTerritory.TerritoryID = Sales.Customer.TerritoryID
WHERE (Sales.SalesTerritory.CountryRegionCode = 'US') GROUP BY Sales.Customer.TerritoryID, Sales.SalesTerritory.Name

This query will return all US based territories along with the total number of customers belonging to each.

We can then configure the chart to use the SqlDataSource as shown in the picture below:

chartdatasource-thumb.png

Now that the data source has been configured and attached to the chart, we can begin configuring the chart itself.

First let’s set the width of the chart to 800 pixels and the height of the chart to 600 pixels.  These properties can be found on the Chart control itself.  Let’s also set the DataSourceID property to the data source that we created earlier on the page.

Let’s give the chart a title.  To do this, we can modify the Titles collection and edit the (Text) property.  You can modify font size and family here as well as lots of other properties which we won’t cover.

titles-thumb.png

Time to change the chart into a pie chart and set up how to display the data from our data source.  To do this, edit the chart’s Series collection.  First, set the ChartType to Pie.  Then, we can set the Data Source’s XValueMember and YValueMembers to RegionName and CustomerCount respectively.  If these values do not appear in the DropDownList, just go ahead and type them in.  Also go ahead and set the property IsValueShownAsLabel to True and the PieLabelStyle to Outside.  This will show a line pointing to the pie slice with a label containing the number of customers for that region.  Since the region name will be included in the legend that we will create, I think it is appropriate to show the customer count here.

series-thumb.png

Let’s also create a legend for this chart.  To do so, modify the Legends collection and add a new legend.  This should automatically hook the legend up to the series that we previously configured.  If it doesn’t, you can re-open that series and set the Legend property appropriately.

Next, let’s make the chart 3-D.  Modify the ChartAreas collection and set the (Enable3D) property to True beneath the Area3DStyle section.

chartareas-thumb.png

You can now build and run your project.  If all has gone well, you should see something like the following in your web browser:

chartfinal-thumb

Your source code should look similar to what is shown below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ChartingDemo._Default" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Customers By US Region</title>
</head>
<body>
    <form id="formDemo" runat="server">
    <div>
        <asp:Chart ID="ChartDemo" runat="server" DataSourceID="SqlDataSourceDemo" Height="600px"
            Width="800px">
            <Legends>
                <asp:Legend Font="Calibri, 8.25pt" IsTextAutoFit="False" Name="LegendDemo">
                </asp:Legend>
            </Legends>
            <BorderSkin SkinStyle="Emboss" />
            <Titles>
                <asp:Title Font="Calibri, 15.75pt" Name="TitleDemo" Text="ASP.NET Charting Demo">
                </asp:Title>
            </Titles>
            <Series>
                <asp:Series Name="SeriesDemo" ChartType="Pie" CustomProperties="PieLabelStyle=Outside"
                    Font="Calibri, 8.25pt" IsValueShownAsLabel="True" Legend="LegendDemo" XValueMember="RegionName"
                    YValueMembers="CustomerCount">
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartAreaDemo">
                    <Area3DStyle Enable3D="True" />
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
        <asp:SqlDataSource ID="SqlDataSourceDemo" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT Sales.SalesTerritory.Name AS RegionName, COUNT(Sales.Customer.TerritoryID) AS CustomerCount FROM Sales.SalesTerritory INNER JOIN Sales.Customer ON Sales.SalesTerritory.TerritoryID = Sales.Customer.TerritoryID WHERE (Sales.SalesTerritory.CountryRegionCode = 'US') GROUP BY Sales.Customer.TerritoryID, Sales.SalesTerritory.Name">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
Intro to the ASP.NET Charting Control
Tagged on:     

Leave a Reply