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

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 United States

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 Lithuania

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 Mexico

5/23/2010 8:28:55 AM

William Aaron

Hi, I had something hacked together in JS but this solution is much more elegant so I am going to switch.  Thanks.

William Aaron United States

7/18/2010 12:12:55 PM

Michael Bolton

William, I'm doing fine with the JS. I've just begun learning ASP.NET so most of stuffs I'm still doing with JS and find no problem. Do you want the code?

Michael Bolton United States

7/23/2010 9:11:24 AM

Woo

Thanks for the great series. Do you recommend both SeriesIDField and SeriesValueField or should I just stick to ID. Just wondering if I should just include both as a natural default. Thanks again.

Woo Canada

7/23/2010 10:07:51 AM

Alan Mendelevich

@Woo: if your ID and Value are equal there's no difference if you specify both or only the ID.

Alan Mendelevich Lithuania

8/28/2010 8:27:15 AM

Josh Jones

This is great information. I have been wanting to learn how to do this for a while.

Josh Jones United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Copyright © 2003 - 2010 Alan Mendelevich
Powered by BlogEngine.NET 1.6.1.0