Create new account I forgot my password    

Exploring the ASP.NET 3.5 Chart Controls
Rating: 26 user(s) have rated this article Average rating: 4.3
Posted by: Suprotim Agarwal, on 12/2/2008, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 42070 times
Abstract: The new Chart Controls for .NET Framework 3.5 enables ASP.NET and Windows Forms developers to easily create rich and professional-looking data visualization solutions. The Chart Controls currently contains 25 different chart types with 3-D support for most of them, and all of these, available for FREE. In this article, we will create a sample chart using the Chart controls.

Exploring the ASP.NET 3.5 Chart Controls
 
The new Chart Controls for .NET Framework 3.5 enables ASP.NET and Windows Forms developers to easily create rich and professional-looking data visualization solutions. The Chart Controls currently contains 25 different chart types with 3-D support for most of them, and all of these, available for FREE. Amongst the other features found in 3rd party charting controls, the Chart Controls for .NET framework provides feature like DataBinding, Data Export, support for Financial and Statistical data, State Management, Streaming, Serialization and much more.
To get started, download and install the following in the order given below:
Microsoft Chart Controls for Microsoft .NET Framework 3.5 – assemblies containing the Chart Controls (ASP.NET and Windows Forms)
Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008 – Add as a control in the Visual Studio toolbox and provides IntelliSense for Chart controls.
Once you have downloaded and installed these components, open Visual Studio 2008. In the Data tab on the Toolbox, observe the new ‘Chart’ control.
Chart Toolbox
To get started, let us create a sample application using the Chart control:
Step 1: Drag and drop the Chart control from the Toolbox to the Design surface of Default.aspx. You will see a ‘Column chart’ which looks similar to the following:
Sample Chart
If you click on the ‘Source’ view of the page, you find the following mark up added to the page:
<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
 
        <asp:Chart ID="Chart1" runat="server">
            <Series>
                <asp:Series Name="Series1">
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
By default the ChartType is ‘Column’. In order to change the Chart Type to either Line, Bar, Pie or any of these 25 chart types; with the Chart control selected, go to the Properties window > go to ‘Series’ > Click on the ellipse button(…) > in the ‘Series Collection Editor’, click on the ‘ChartType’ to view and select a different chart type as shown below:
Series Coll Editor
For this demonstration, we will keep the ChartType to the default ‘Column’.
We will be binding the chart to some data. For this purpose, we will be using the SqlDataSource.
Step 2: Click on the smart tag or right click Chart > Show Smart Tag > Choose a DataSource > New Data Source… > Select Database. We will keep the default ID for SqlDataSource, SqlDataSource1 and click OK.
Step 3: On clicking OK, you will be presented with a ‘Configure Data Source’ wizard. Click on ‘New Connection’ to open the ‘Add Connection’. Type your ‘Server Name’ and ‘Select a database Name’ to connect to. Over here, I have typed (local) as the ServerName and the database I am connecting to is ‘Northwind’. Click on ‘Test Connection’ to make sure that there are no errors connecting to the server. Click Ok.
Step 4: In your ‘Configure Data Source’, click Next. An option will be displayed for saving the connection string to the configuration file. Select the checkbox ‘Yes, save this connection as:’ , type a name for the connectionstring ‘NorthwindConnectionString’ and click Next.
Step 5: In the ‘Configure Select Statement’ > select ‘Specify a Custom SQL Statement or stored procedure’ > click Next
SQL Data Src
Type the SQL query to ‘list the count of products in a category’ as shown below:
SELECT CategoryName, COUNT(*) AS ProductCount FROM Products JOIN Categories ON Products.CategoryID = Categories.CategoryID GROUP BY CategoryName
Query
Click Next > Finish.
Step 6: Select the Chart control > Go to the Properties window > ChartAreas property and click the ellipse (…) button to open the ChartArea collection editor. Go to the ‘Axes’ property in the ChartArea Collection Editor and click the ellipse (…) button to open the Axis collection editor as shown below:
Axis Coll Editor
Set the ‘Title’ of the X axis to ‘Categories’ and the ‘Title’ of the Y axis to ‘Product Count’. Click OK to close the Axis Collection Editor and an OK again to close the ChartArea Collection Editor.
With the Chart control selected, go to the Series property in the Properties window and click the ellipse (…) button to open the Series collection editor. Go to the Data Source section and add the XValueMember as ‘CategoryName’ and YValueMembers to ‘ProductCount’. Also set the ‘IsValueShownAsLabel’ to True to display the value of ProductCount on the columns. Click OK.
Series Coll Editor
Step 7: With the properties set, run the application. You will see a chart as displayed below:
Final Chart
I am sure all we developers who have used 3rd party Charting solutions in the past, are going to welcome this awesome control from Microsoft. The best part is that it is available for FREE. For the ASP.NET MVC developers, here’s an article that would interest you Combining ASP.NET MVC and ASP.NET Charting Controls.
I hope you liked the article and I thank you for viewing it.  
 If you liked the article,  Subscribe to my RSS Feed or Subscribe Via Email









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by ppp_parvez on Wednesday, December 24, 2008 2:41 AM
hi,
You r doing great job.
Sir i want to download vs 2008 for free, plz kindly send to my mail the url to download visual studio 2008 for free.
Comment posted by Suprotim Agarwal on Friday, December 26, 2008 9:09 PM
Parvez: VS 2008 Trials can be downloaded from here:
http://msdn.microsoft.com/en-us/visualc/aa700831.aspx
Comment posted by Brian W on Wednesday, January 14, 2009 3:32 PM
Nice and easy article. For whatever reason my chart control will not work in VS2008 design mode; error "Error creatign control - Chart1. Object reference not set to an instance of an object." However the control works fine using code-behind. Long stroy short, could you provide the source code for reviewing? I am unable to get the chart to display my datasource data which haoppens to work in my test gridview. Thanks
Comment posted by sathish kumar.n on Friday, January 23, 2009 2:35 AM
this is very usfull forcreate an chart
Comment posted by arusenthil on Tuesday, February 03, 2009 1:39 AM
i am using vs 2008 professional edition so its not installed chart controls in toolbox ..can u pls anyone suggest for that one
Comment posted by kiran on Wednesday, May 06, 2009 1:08 AM
hi this very nice control but how to show timespan values like
1 day 00:10
2 days 23:25
0 days 22:12:12
how to show this values in y axies in chart control?????
Comment posted by Sudheer on Friday, June 19, 2009 6:07 AM
I have a requirement to show the chart inside a gridview. The gridview should be like a master child view and the child view should contain the chart. I tried so many alternatives but was not able to load a chart. Pls let me know if you have any clues.
Comment posted by Mohan on Monday, July 13, 2009 1:16 PM
Is there any possiblity of loading(binding using ajax services) chart control using javascript. Please help me our for doing the same.

Thanks,
Mohan.
Comment posted by Suprotim Agarwal on Friday, July 17, 2009 2:44 AM
Mohan: Do you mean ASP.NET Chart Control. Check this article http://www.codeproject.com/KB/webforms/ms-chart-with-jquery.aspx
Comment posted by jeni on Thursday, August 13, 2009 7:43 AM
i have done the same but my chart is not display in the page.What i have to do for that???
please rpl
Comment posted by Suprotim Agarwal on Saturday, August 15, 2009 2:39 AM
Jeni: Unfortunately that information is not enough to help you out. Use the contact page to mail me the project that's not working.
Comment posted by Priyanka on Thursday, November 26, 2009 6:58 AM
How to enable horizontal scrollbar in chart control?
Comment posted by Aleksey on Friday, February 05, 2010 11:07 AM
Finally, I found what I want! Thanks! But I need same vay detailed instructions to set up properties for PIE chart type, including how to identify what color presents what value.
Comment posted by Viswes on Thursday, February 11, 2010 12:23 AM
Plz provide Pie chart setup properties.
Comment posted by yogesh on Monday, February 15, 2010 1:55 AM
Is this control beta version????
Plz let me know, i want to use it in project.
Thanking u. Gr8 job..
Comment posted by Tanya on Wednesday, April 07, 2010 3:10 PM
hi.

i followed everything you did, but my YValueMembers and XValueMembers have blank dropdowns :(. i tried manually typing in the sql field names, but my chart still comes back blank. please help.
Comment posted by utam kumar on Sunday, July 11, 2010 10:52 PM
All methods and properties of Gridview control in asp.net language c#.
Comment posted by deype0 on Friday, July 23, 2010 9:15 PM
Thanks a lot!

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER