Home » ASP » Article
ASP.NET Bar Chart Graph
|
Article by: |
Chris Goldfarb (4/8/2003) |
|
Sponsored by:
|
FindMyHosting - Web Hosting Search
|
Summary:
|
Complete source code to generate a horizontal bar chart graph in ASP.NET |
|
Viewed: 20929 times |
Rating (29 votes): |
|
4.6 out of 5 |
|
|
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
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).
|
|
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
|
|
|
To post comments you need to become a member. If you are already a member, please log in .
|
|