codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

ASP.NET Bar Chart Graph

Article by: Chris Goldfarb (4/8/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: Complete source code to generate a horizontal bar chart graph in ASP.NET
Viewed: 97658 times Rating (62 votes): 
 4.1 out of 5
 Rate this Article  Read Comments  Post Comments

ASP.NET Bar Chart Graph



Online Demo

This script generates a series of horizontal bars in graph chart format, on an .aspx web page.

To run the graph script is very simple. First save the following code into a file called dotnetgraph.ascx

Select All Code


Then save the following in the same directory to a file called dotnetgraph.aspx

Select All Code


Within this aspx page you will see we set all the quantities and labels for the graph within the Page_Load function.

There is obviously plenty of scope for development in this section - for example, pulling these values directly from a database. But hopefully the elegant simplicity of these files will be a useful introduction to the concepts involved.

Added by the author

I was still a little new to .NET when I wrote this article two years ago , so there are a couple things I'd change if implementing now: 1. Use StringBuilder instead for the RenderItem method:
private String RenderItem (Int32 iVal, Int32 iMod, String sColor) {
StringBuilder sb = new StringBuilder();
sb.Append("<table border=0 bgcolor=" + sColor + " cellpadding=0 cellspacing=0><tr>");
sb.Append("<td align=center width=" + (iVal * iMod) + " nobr nowrap>");
sb.Append("<b>" + iVal + "</b>");
sb.Append("</tr><td></table>");
return sb.ToString();
}


StringBuilder is orders of magnitude faster than manipulating strings directly (strings are immutable).




CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'ASP.NET Bar Chart Graph'
Posted by :  Archive Import (Jeremy Leppan) at 07:36 on Friday, September 05, 2003
Here however is something you could tweak

sql = "select count(login_id) as total_logins , user_name from valid_logins where ip_address <> '192.168.0.3' and datediff('d',date_time,now()) < datepart('d',now()) + 1 group by user_name order by count(login_id) DESC "
rs.open(sql,dbconn)

while not rs.eof
redim preserve sItems(x_count)
redim preserve iValue(x_count)
sItems(x_count) = rs("user_name").value
iValue(x_count) = rs("total_logins").value
x_count = x_count + 1
rs.movenext()
end while
Posted by :  Archive Import (jeremy leppan) at 07:39 on Friday, September 05, 2003
Explaination of the above
______________________________

You see the select - it select the total amount each user has logged into my system. But it does a little more than that.

It filters out the local address I use for testing. This could be replaced with a range if you like.

Then the date_diff thing. Its just a nice way to get all entries for this month.

You can not just to a datediff - 1 month. Cauase that would give you traffic for the last 30 days. You want traffic from the beginning of the month.

The group by, this is the part, that groups the logins to users.

then the loop.

Nothing fancy - just builds the instuctions for the graph
Posted by :  Archive Import (Yvonne) at 07:04 on Wednesday, September 17, 2003
how come we cant manage to run the bar chart even though we have implemented all the codings as accordingly??
Posted by :  Archive Import (TP) at 03:13 on Thursday, September 18, 2003
Is the code u provided above must be run in C# program? because we are using Visual Studio.Net - ASP.Net. However, the graph won't come out. can u send us the code for ASP.Net using vb language instead of c#.
Posted by :  csandvig at 12:24 on Friday, October 17, 2003
Excellent example - simple and it works! Thank you.
Posted by :  cüneyt at 19:38 on Saturday, February 28, 2004
Great, it works fine with static values.

I am trying to get the data from a database(MSDE), and
count the number of items and pass the result to iValue. I am using c#.

I have tried to use:
iValue = select count(BedID) from Wards;

but this returns a string not an Int32.

Please help?

thanks
Posted by :  dupati1 at 14:11 on Thursday, July 08, 2004
Hi i have the same problem...

I am trying to get the data from SQL server and it returned as a string...

how can i make it as String[] and Int32[]

Please help...

I can post code if you need more information.

-VJ
Posted by :  oytun12 at 13:45 on Thursday, December 16, 2004
Cüneyt & VJ
System.Convert.ToInt32(string)
Posted by :  enric_ccm at 01:17 on Wednesday, February 16, 2005
can you send me the full code in C# for creating a bar chart with the data from a SQL Server database? i try out your code above but i didn't display any thing
Posted by :  enric_ccm at 04:37 on Wednesday, February 16, 2005
can you send me a full coding of generating the bar chart using data from SQL Server database? i have try your code but it display nothing when i link it to database
Posted by :  rajeshkutti at 00:33 on Thursday, March 31, 2005
It's working fine,but i need chart to be generated dynamically to Database(SqlServer2000) values as a caomparitive chart between the scheduled vs actual period
Posted by :  ethan at 13:25 on Tuesday, August 09, 2005
my god you people are so lazy - you are asking this guy to do your work for you? you cant create an array? give me a break.
Posted by :  rjp1979 at 08:04 on Thursday, October 27, 2005
Well said Ethan.

Thanks heaps Chris. Easy to use and effective :)
Posted by :  mcab21 at 15:37 on Thursday, April 06, 2006
Hi, can anyone help - i am using ASP.Net and more specifically VB.Net. I need a bar simple bar chart - something like above. I'm having problems designing my own. Can someone provide me a simple example to work from?

Cheers,

Rob
Posted by :  surya_sree at 04:13 on Saturday, June 10, 2006
Plz, i need the Download the option of the Code. Like search Engnie, Bar Graph etc.,
Posted by :  David Bruce at 02:35 on Thursday, August 30, 2007
test
Posted by :  dc197 at 02:36 on Thursday, August 30, 2007
test
Posted by :  ramy@groupescapes.com at 16:11 on Thursday, December 06, 2007
Any leads as to how I can generate a linear preferably or else a bar chart based on two column populated out of a SQL DB?
I keep running into pie charts but don't seem to break through (5 days later that is)
I would appreciate any help you can provide.
Thanks again
Posted by :  lovesharma at 03:21 on Saturday, January 12, 2008
hi,
i m trying to develop a web application using ASP.NET,VB.NET & SQL SERVER
2005.
in my application i want to show data on graph based on user selection.
is it possibile using Office Web Components?

if yes than how.
pls help me
Posted by :  suryakiran255 at 00:23 on Thursday, January 24, 2008
Hello! this is surya kiran. I am working on developing online exams and then generating reports according to the scores.which consists of graphs. I want to know is there any graph which is like slide bar type according to the rating the person scores. Can I get that?


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.








Recent Forum Threads
• ** Site Hacked ** javascript inserted...
• Losing background/text color when converting HTML to PDF
• hidden div block when displayed, displays at an offset of 200px only in IE 6..
• help - sketch
• Re: Perl Script - File Handling.
• Open a file from website
• Re: to open 5 terminals from one and also execute different commands on each terminal
• read a selected multiple line, those should match some of the values read in the files..
• Help me please


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008