Click here to Skip to main content
Click here to Skip to main content

ASP.NET 4.0 Chart Control

By , 28 Aug 2011
 

NEW: View a live demo of the above charts tester code at http://dotnetcorner.ch/tools/aspnetcharts/default.aspx.

Charts Tester

Today, I would like to introduce the new Chart Control, which was newly added to the ASP.NET 4.0 Framework. With this chart control, you can build exciting graphical charts within minutes.

Our starting position is a new ASP.NET Web Application using Visual Studio 2010.

Implement the Control Into Your Page

If you open up your default.aspx and look at the toolbox, you will find the "Chart" Control in the section "Data":

If you drag the chart control into the design-surface, Visual Studio adds some settings to the web.config. This will be done automatically so the following is just an explanation of the changes:

Web.config Changes

ChartImage Settings

At the node <appSettings>, the following entry will be created:

<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />

This is the configuration of the temporary storage, where the generated files should be placed at. Please be aware of the fact that the storage will use a large amount of space, depending on how many charts will be generated.

ChartImage.axd Handler

At <system.web><httpHandlers>, the following entry will be added:

<add path="ChartImg.axd" verb="GET,HEAD,POST" 
type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" validate="false" />

The chartimage.axd is the main-component of the chart-control. The handler will be called for each generated image.

Control-definition

In the node <system.web><pages><controls>, the tag-definition will be added:

<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" 

assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture:neutral, 
PublicKeyToken=31bf3856ad364e35" />

This will add the tag-support, that we can use a <asp:chart /> tag in our aspx pages.

Reference to the DataVisualization Assembly

At the end, a reference to the DataVisualization assembly will be added at <system.web><compilation><assemblies>:

<add assembly="System.Web.DataVisualization, Version=4.0.0.0, 
Culture:neutral, PublicKeyToken=31bf3856ad364e35"/>

Default Chart-element in aspx Page

In the aspx page, where you dragged the control in, Visual Studio will add the default chart-control to the code:

<asp:Chart ID="Chart1" runat="server">

	<Series>
		<asp:Series Name="Series1">
		</asp:Series>
	</Series>
	<ChartAreas>
		<asp:ChartArea Name="ChartArea1">

		</asp:ChartArea>
	</ChartAreas>
</asp:Chart>

Charting Quickstart

Before we begin using the chart control, I would like to explain the properties "Series" and "ChartAreas":

Series

The Series collection contains the data-points for one or more data-series. Basically for each line in the chart (consisting of multiple data-points), one series of data is required.
Each series can have its own ChartType. Additionally, each series can be assigned to any ChartArea where each series can also have its own rendering-properties (for example colors, labels, etc.).

ChartAreas The ChartArea collection can have one or more configurations which controls the rendering of a chart.
Important: There must be at least one ChartArea definition that the rendering will be executed!

Now we know that we have to add a <asp:Series> in the property <series> which contains our datapoints. Also we have to be sure that there is at least one <asp:ChartArea> at the <ChartAreas> defined, otherwise we would get just a white picture.

After changing the chart-control code to the following:

<asp:Chart ID="cTestChart" runat="server">
	<Series>
		<asp:Series Name="Testing" YValueType="Int32">

			<Points>
				<asp:DataPoint AxisLabel="Test 1" YValues="10" />
				<asp:DataPoint AxisLabel="Test 2" YValues="20" />

				<asp:DataPoint AxisLabel="Test 3" YValues="30" />
				<asp:DataPoint AxisLabel="Test 4" YValues="40" />

			</Points>
		</asp:Series>
	</Series>
	<ChartAreas>
		<asp:ChartArea Name="ChartArea1">
		</asp:ChartArea>

	</ChartAreas>
</asp:Chart>

We will get this output in the rendered output page:

So we successfully created our first chart!

More Information

The chart control in the ASP.NET 4.0 Framework is a big and comprehensive control, so to explain the whole control would be too much for a short blog-post :)

Also, there are already many good introductions and tutorials about the chart control, so I will give you a short list on what you can look at next:

So I hope I was able to give you a short overview about the new ASP.NET Charts Control! If you have any questions or information, please just leave a comment! I'm happy to read any suggestions and I will try to answer every question!

Now, have a nice day and always happy coding! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Christoph Keller
Software Developer
Switzerland Switzerland
Member
I'm a software developer with ASP.NET experience since 2007 (started using ASP.NET 2.0, now using mostly ASP.NET 4.5 / MVC 2/3/4).
 
I also had first experiences with SharePoint 2010 development and Sitefinity CMS development.
 
Projects:
=========
 
New AMF Parser for Fiddler2: https://fiddleramfparser.codeplex.com/
 
=========
More about me: http://dotnetcorner.ch
My Blog: http://blog.dotnetcorner.ch

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAbout chart controlmemberchenna.chinnabayanna19 Mar '13 - 23:02 
Hi sir plz help me
 

I Have one linkbutton and one dropdown with 4 checkboxes
1)Area
2)Bar
3)line
4)pie These 4 are checkboxes placed in dropdownlist
 
N linkbuttons are there
each linkbutton specify for one clients
 
what my question is if i click the linkbuton of perticular client and if i click checkbox of Area i should get only one chart i.e is Area chart
if i click two checkboxes like Area and pie i should get two charts
if i click 3 checkboxes i should get 3 charts
if i click 4 checkboxes i should get 4 charts related to them
 

plz reply for it as early as possible
QuestionHow to display dots on the chart?memberWu John1 Mar '13 - 15:43 
Dear Christoph,
 
I read your post regarding the line charts, and downloaded sample charts myself.
 
Could you please take a look at the sample line chart?
https://www.sugarsync.com/pf/D9726155_61875891_635561
 
It is a VS2010 folder, the target page is:
\WebSamples\WebSamples\Interactivity\AJAX\ChartAsTrigger\ChartAsTrigger.aspx
 
I would like to add dots on the line chart. For example, users can see not only four bars (i.e.,10, 20,30, 40 points) on the chart, but someone's points on the bar.
 
In other words, I'd like to see the relation between a person's performance and the whole group, by using dots(to show individual's value) and line charts(to show a group's average performance).
 
Any help would be very appreciated.
AnswerRe: How to display dots on the chart?memberChristoph Keller6 Mar '13 - 3:38 
Dear John,
 
I'm sorry for the late answer.
 
As far as I know, you should be able to solve this by using multiple Series (with different Chart-Types each).
 
I never tested this by myself (and also do not have the infrastructure to test this atm), but this should solve your problem.
 
Here is also a video-tutorial about multiple-chart-types in one chart:
http://www.youtube.com/watch?v=SXxAJzYdbm4[^]
 
Hope this helps.
 
Best regards and as always, happy coding,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralRe: How to display dots on the chart?memberWu John7 Mar '13 - 1:09 
Hello Christoph,
 
Thanks for your kind reply. I assume that creating multiple series in one chart by using Visual Studio 2010 requires web chart controls.
 
I have watched the video and now trying to implement the suggestion. I will get back to you if I have any good news.
 
BR,
John
QuestionClient Rendered Version?memberessence6 Feb '13 - 11:27 
I really love how ASP.NET charts work. But I really wish there was a way to 'flip a switch' and have the chart render on the client... That way there could be zooming, etc. Anyone know any libraries/scripts that do this easily?
--Oren.

AnswerRe: Client Rendered Version?memberChristoph Keller6 Feb '13 - 21:41 
Hi Oren,
 
I don't know much about client-side charts, but imho Google Charts[^] looks awesome and it seems that there is a possibility for "zooming" into a chart.
 
Anyway, you know that this zooming is also supported by the ASP.NET Charts? You just define a Url for a data-point where you render the zoomed chart and voila. The configured data-point will now have a clickable area which opens the desired URL.
 
Hope this helps.
 
Best regards,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralRe: Client Rendered Version?memberessence7 Feb '13 - 8:44 
Yes. I'm looking for something that is easier to use than HighCharts/HighStocks (a JavaScript based chart system).
--Oren.

Questionchart controls with options...membervaraprasadreddy.bh19 Dec '12 - 8:26 
For asp.net look at here it may help some one...
 
http://reddyinfosoft.blogspot.in/2012/08/how-to-use-mschart-in-aspnet-using-c.html[^]
Questionhaving problem in pie chart drop outmembersharda jaiswal30 Oct '12 - 21:33 
i have some problems in chart control. i am using pie chart in 3d mode but i don't knw how 2 drop out a piece from pie chart and in Legend how can show percentage and numbers.
 
and in bar chart how to show the y axis value's description just like in Rs.
thanks in advnc..
AnswerRe: having problem in pie chart drop outmemberChristoph Keller31 Oct '12 - 5:28 
Hi,
 
The "drop-out" or "explode" effect can be created by using the ["Exploded"] Custom-Property of a single point of your area (the one who should be exploded).
 
For example:
 
chart.Series["SeriesName"].Points[0]["Exploded"] = "True";
 
What do you mean with "show percentage and numbers"? The legend should be data-bound, so you can write anything in it Smile | :)
 
Also I do not understand your last question. Could you give me an example?
 
Best regards,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralRe: having problem in pie chart drop outmembersharda jaiswal31 Oct '12 - 21:52 
thank u Smile | :)
it works...
 
In bar chart Y-axis show only numbers just like 10,20,30,... but doesn't show what the number represents... for example it represent Distance in Km.
GeneralRe: having problem in pie chart drop outmembersharda jaiswal1 Nov '12 - 2:08 
Hi,
second problem is also solved...
we can set title property of axis.
 
to set that i go to Property windows> ChartArea >Axes
than select Y(Value) axis
and write in Title
 
but for pie chart i could't understand how to manually bind data in Legend...
because automatically it shows only names but i want to show percent of each area with names in legend..
GeneralRe: having problem in pie chart drop outmemberChristoph Keller4 Nov '12 - 20:44 
Hi,
 
Sorry for the long answering time, I was off at friday Smile | :)
 
Perhaps this would help you:
http://social.msdn.microsoft.com/Forums/en/MSWinWebChart/thread/86021aca-0a1a-44a2-9683-2db4ad98d7bc[^]
 
Until now, I never used custom axis legends, sorry Smile | :)
 

Best regads and happy coding,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralRe: having problem in pie chart drop outmembersharda jaiswal6 Nov '12 - 18:39 
Hi,
Thanks for reply. Smile | :)
 
I Calculated percentage of pie chart at code behind and than gave it to chart.
GeneralMy vote of 3membersharda jaiswal19 Oct '12 - 21:51 
it's a good post as a pie chart introduction..
i'll try it..
GeneralRe: My vote of 3memberChristoph Keller21 Oct '12 - 3:41 
Thanks! If you have any Problems, feel free to contact me!
 
Best regards and as allways, happy Coding!
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralMy vote of 5memberD-Kishore4 Sep '12 - 1:05 
good
GeneralRe: My vote of 5memberChristoph Keller4 Sep '12 - 5:45 
Thank you! Smile | :)
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

QuestionGreat! (my vote of 5)memberMember 381279315 Mar '12 - 13:52 
But how to bind it with a database?
AnswerRe: Great! (my vote of 5)memberChristoph Keller15 Mar '12 - 19:59 
Hi and thanks for your vote!
 
There is a good tutorial about how to add real data from a database:
http://www.4guysfromrolla.com/articles/072909-1.aspx[^]
 
Does this helps?
 
Bestregards and have a nice day,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralMy vote of 5memberAkram El Assas4 Mar '12 - 6:57 
Nice article Cool | :cool: .
GeneralRe: My vote of 5memberChristoph Keller4 Mar '12 - 11:33 
Thank you! Smile | :)
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralMy vote of 5memberSunasara Imdadhusen21 Sep '11 - 23:12 
Excellent!!
GeneralRe: My vote of 5memberChristoph Keller23 Sep '11 - 1:53 
Thank you! Smile | :)
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

QuestionMy Vote of 0memberDewey28 Aug '11 - 15:20 
Plus 5 = 5.
 
You were right on time, I needed this so bad because I had to get a solution somewhat close to the Silverlight version we used in the past.
 
This neatly does the trick!
 
Great work!
AnswerRe: My Vote of 0memberChristoph Keller28 Aug '11 - 19:08 
Thank you for your feedback, and glad to hear that it was right on-time Smile | :)
 
have a nice day and happy coding,
Chris
New Url Un-Shortener available: http://dotnetcorner.ch/tools/unshortener/
 
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

GeneralMy vote of 5memberAnurag Gandhi16 Aug '11 - 1:01 
Nice One. Nice Demo.
Chart control is one of the nice control that has been added to the asp.Net 4.0.
GeneralRe: My vote of 5memberChristoph Keller16 Aug '11 - 18:44 
Thank you! Smile | :)
Check out my website http://dotnetcorner.ch/ and my blog http://blog.dotnetcorner.ch/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 28 Aug 2011
Article Copyright 2011 by Christoph Keller
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid