ailon's DevBlog: Development related stuff in my life

Add Interactive Flash Charts to Your ASP.NET Web Application. Part 4: Column Chart from Code-behind

8/8/2008 10:29:36 AM

This is the fourth part in the series of tutorials showing how to add dynamic, interactive, data-driven charts to your ASP.NET web applications. These tutorials use amCharts Flash charting components and "ASP.NET Controls for amCharts" for ASP.NET integration.

This part is long overdue. Sorry for that. A tutorial on adding amCharts to ASP.NET page from code-behind was the most requested on amCharts ASP.NET controls forum. So, here we go.

Make sure you have your environment setup, ASP.NET controls for amCharts DLL added and amCharts Column chart added to your project. Details on doing that were provided in Part 1 of the series so I wont repeat it here.

Now let's add a new ASP.NET web form to our project and let's call it CodeBehindBar.aspx. We will only add a single PlaceHolder object to the markup side of our script so it looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CodeBehindBar.aspx.cs" Inherits="CodeBehindBar" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:PlaceHolder ID="ChartPlaceHolder" runat="server" />
    </div>
    </form>
</body>
</html>

Now let's switch to the code-behind file.

We will be creating a column chart with two graphs and we'll use Northwind database as data source. The first graph will represent sales totals for 1997 grouped by category and divided by 1000 (just for the sake of being in the same range as our second graph). The second graph will show quantities of products in each group we have in stock.

Make sure that you have access to some SQL Server instance with Northwind database installed, connection string setup, and let's start by pulling our data from SQL Server and placing it into DataSet:

// Get data from Northwind
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

SqlCommand myCommand = new SqlCommand("select CategoryName, CategorySales/1000 as CategorySales from [Category Sales for 1997]", conn);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds, "Category Sales for 1997");

myCommand.CommandText = "select CategoryName, sum(UnitsInStock) as UnitsInStock from [Products by Category] group by CategoryName";
da.Fill(ds, "Products by Category");

conn.Close();

Now it's time to create our chart. We create a ColumnChart object and setup it's DataSource. This is what will be used for the chart series (X axis).

// create column chart
ColumnChart chart = new ColumnChart();
chart.DataSource = ds.Tables["Category Sales for 1997"].DefaultView;
chart.DataSeriesIDField = "CategoryName";
chart.DataSeriesValueField = "CategoryName"; // here it's the same as ID and could've been ommited

Notice that we've specified "CategoryName" as both SeriesIDField and SeriesValueField. This isn't necessary in case like this were we have the same field for both ID and Value. It would suffice to just specify the ID, but I've added Value line so you know you can use different data fields for that.

What we've done so far is created a grid where to add actual data graphs. Now let's create 2 graphs:

// crete sales graph
ColumnChartGraph graph1 = new ColumnChartGraph();
graph1.DataSource = ds.Tables["Category Sales for 1997"].DefaultView;
graph1.DataSeriesItemIDField = "CategoryName";
graph1.DataValueField = "CategorySales";
graph1.Title = "Sales (in thousands)";

// crete stock graph
ColumnChartGraph graph2 = new ColumnChartGraph();
graph2.DataSource = ds.Tables["Products by Category"].DefaultView;
graph2.DataSeriesItemIDField = "CategoryName";
graph2.DataValueField = "UnitsInStock";
graph2.Title = "Units in stock";

and add these graphs to our chart:

chart.Graphs.Add(graph1);
chart.Graphs.Add(graph2);

What's left is just data-binding the chart and adding it to our place holder control on the page

chart.DataBind();
ChartPlaceHolder.Controls.Add(chart);

And that' basically it. This is what you should see if you run this:

 amcharts_code_behind

You can customize the look and feel by changing various properties according to your taste.

Download the source code here: CodeBehindBar.zip (1kb)

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Related posts

Comments

9/2/2008 11:24:25 PM

jewel thomas

Hi,

How could i create a stacked bar chart ? How do we pass the different values that should be stacked on each bar/column ?

Thanks & Regards
Jewel Thomas

jewel thomas us

9/3/2008 8:42:46 AM

ailon

You just add a separate graph for each stack level and set the ColumnType property of ColumnChart to Stacked.

ailon lt

9/3/2008 6:57:28 PM

jewel thomas

Thanks for the help.

jewel thomas us

9/5/2008 5:08:17 AM

Marco Ballesteros

Hi,
your explanation about how to add an ampie chart was very good, before that i was having troubles to do that. Well , now I can use an ampie chart connected to a sql database and it is working pretty good. (I'm using the asp.net charts and sql server, Visual Web Developer 2005 Express)

Now i´m trying to use a columnchart and I don't know the correct way to fill de properties of the chart, because is not working as I want.

The situation is like this, I'm doing a query to the database and the result is a 3 column group of data. (ID,Description & Amount) The thing is that I want to show in the chart each row of the group as a serie. Right now I'm trying to do this but I'm having troubles beacuase the chart show me every rows in an unique serie.

What I want is ---> X rows = X series

I hope you can help .

Have a nice day.

Marco Ballesteros mx

11/25/2008 5:49:39 PM

Busby SEO Test

hmmm.. nice tutorial!

Busby SEO Test us

11/27/2008 3:07:19 AM

Busby SEO Test rules

one that i like .NET is it has many usefull component in their framework. nice tutorial

Busby SEO Test rules us

11/30/2008 2:46:34 PM

Busby SEO test

nice code, salut...

Busby SEO test us

12/1/2008 8:27:04 PM

Busby SEo Test

The .Net framework is such a robust one that even the charting your data visually becomes easier. Nice one you have here.

Busby SEo Test us

12/1/2008 8:27:37 PM

Busby SEo Test

Maybe you can blog some more examples to ease out our coding burdens on data driven charts. thanks man

Busby SEo Test us

12/19/2008 5:09:33 PM

Busby SEO Test

So where you getting all these codes? They are all good!

Busby SEO Test us

12/25/2008 11:45:47 AM

Busby SEO Test!!!

thanks for the code, i will be more patience to learn it Smile

Busby SEO Test!!! us

12/26/2008 2:03:33 PM

Busby SEO Test

yes.be patient

Busby SEO Test us

1/29/2009 12:42:14 PM

belajar seo wordpress

hell, there are many busby name with different links here. lol Tong

belajar seo wordpress us

1/29/2009 2:04:55 PM

busby seo test

very great code

busby seo test us

2/3/2009 2:46:06 PM

Kampanye Damai Pemilu Indonesia

great job, i am gonna try it asap Smile

Kampanye Damai Pemilu Indonesia us

3/5/2009 3:08:50 PM

Kampanye Damai Pemilu Indonesia 2009

Great Tuts !!

that's Useful for me, thanks admin...

Kampanye Damai Pemilu Indonesia 2009 us

3/6/2009 3:12:18 PM

phrekaholic

what a great code, thanks for sharing.

phrekaholic us

3/11/2009 9:23:25 AM

tukang nggame

thanks for this nice post.

tukang nggame us

3/12/2009 5:41:56 PM

funny sayings


This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this! You are bookmarked!
Thx,

funny sayings us

3/15/2009 6:19:46 PM

how to write good

awesome. thanks for this!

how to write good us

3/18/2009 2:37:25 PM

funny life quotes


This is truly a great read for me!! I love the quality information and news your blog provides Smile I have you bookmarked to show this to my brother!
Thanks,

funny life quotes us

3/20/2009 7:33:12 PM

Home theatre

They're right.. here is full of interesting info..

thank you admin for sharing this article..

Home theatre us

3/22/2009 3:09:02 PM

food quotes



Great post, one of the most informative posts on this subject online actuallly.

Thanks and keep up the great work.

food quotes us

3/24/2009 12:25:58 AM

blogging tips

very nice blog, i like it.

blogging tips us

3/24/2009 3:59:32 AM

computer IT

What a good cause. Thanks for posting

computer IT us

3/24/2009 4:02:33 AM

artikel komputer

Nice post, many thanks for it!

artikel komputer id

3/24/2009 4:09:16 AM

kampanye damai pemilu indonesia 2009

what a great info, thank you for sharing!

kampanye damai pemilu indonesia 2009 us

3/24/2009 8:37:12 PM

glipmax

great, this is what i am looking for. thanks.

glipmax us

3/24/2009 8:37:57 PM

tukang nggame

thanks, this info is so useful for me

tukang nggame id

Comments are closed
Copyright © 2003 - 2009 Alan Mendelevich
Powered by BlogEngine.NET 1.3.1.0