Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

ASP.NET 4.0 Chart Control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (56 votes)
26 Jan 2016CPOL3 min read 465.7K   18.9K   118   42
A short introduction into the new ASP.NET 4.0 Charts Control

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":

Image 2

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:

XML
<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:

XML
<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:

ASP.NET
<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>:

ASP.NET
<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.NET
<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.NET
<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:

Image 3

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)


Written By
Software Developer
Switzerland Switzerland
I'm a software developer with ASP.NET experience since 2007

Projects:
=========
See my projects list here: http://dotnetcorner.ch/Home/Projects

=========
More about me: http://dotnetcorner.ch
My Blog: http://dotnetcorner.ch/Home/Blog

Comments and Discussions

 
Questionasp.net chart control Pin
Member 1312781117-Mar-20 21:20
Member 1312781117-Mar-20 21:20 
QuestionCustomizing the chart control x-axis labels Pin
Chethan T R30-May-16 19:03
Chethan T R30-May-16 19:03 
QuestionSource Code, Support, etc. Pin
Dewey27-Jan-16 10:41
Dewey27-Jan-16 10:41 
QuestionThank you Pin
t_blue26-Jan-16 12:47
t_blue26-Jan-16 12:47 
AnswerRe: Thank you Pin
Christoph Keller26-Jan-16 20:57
Christoph Keller26-Jan-16 20:57 
QuestionTool tip on Mouse over Pin
Manoj Mokavoor1-Aug-15 0:31
professionalManoj Mokavoor1-Aug-15 0:31 
AnswerRe: Tool tip on Mouse over Pin
Christoph Keller2-Aug-15 21:07
Christoph Keller2-Aug-15 21:07 
QuestionContinuation of the previous question Pin
shkhan8-Apr-15 9:14
shkhan8-Apr-15 9:14 
QuestionNice one, thanks, but have a question! Pin
shkhan8-Apr-15 9:04
shkhan8-Apr-15 9:04 
Generalthanxxxxx Pin
Nabil Mosali7-Dec-14 17:56
Nabil Mosali7-Dec-14 17:56 
GeneralRe: thanxxxxx Pin
Christoph Keller7-Dec-14 20:06
Christoph Keller7-Dec-14 20:06 
QuestionGreat tutorial! Pin
Member 1060697918-Feb-14 10:28
Member 1060697918-Feb-14 10:28 
AnswerRe: Great tutorial! Pin
Christoph Keller23-Feb-14 1:06
Christoph Keller23-Feb-14 1:06 
Questionchart in asp.net 4.0 is not shown after route Pin
idoyohanan22-Sep-13 10:28
idoyohanan22-Sep-13 10:28 
QuestionAbout chart control Pin
chenna.chinnabayanna19-Mar-13 23:02
chenna.chinnabayanna19-Mar-13 23:02 
QuestionHow to display dots on the chart? Pin
Wu John1-Mar-13 15:43
Wu John1-Mar-13 15:43 
AnswerRe: How to display dots on the chart? Pin
Christoph Keller6-Mar-13 3:38
Christoph Keller6-Mar-13 3:38 
GeneralRe: How to display dots on the chart? Pin
Wu John7-Mar-13 1:09
Wu John7-Mar-13 1:09 
QuestionClient Rendered Version? Pin
essence6-Feb-13 11:27
essence6-Feb-13 11:27 
AnswerRe: Client Rendered Version? Pin
Christoph Keller6-Feb-13 21:41
Christoph Keller6-Feb-13 21:41 
GeneralRe: Client Rendered Version? Pin
essence7-Feb-13 8:44
essence7-Feb-13 8:44 
Questionchart controls with options... Pin
varaprasadreddy.bh19-Dec-12 8:26
varaprasadreddy.bh19-Dec-12 8:26 
Questionhaving problem in pie chart drop out Pin
Sharda Jaiswal30-Oct-12 21:33
Sharda Jaiswal30-Oct-12 21:33 
AnswerRe: having problem in pie chart drop out Pin
Christoph Keller31-Oct-12 5:28
Christoph Keller31-Oct-12 5:28 
GeneralRe: having problem in pie chart drop out Pin
Sharda Jaiswal31-Oct-12 21:52
Sharda Jaiswal31-Oct-12 21:52 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.