|
Write a comment |
Articles |
DHTML Gallery |
.NET |
MyBlog |
About Me |
FAQ
|
![]() |
AJAX Bar Graph using ASP.NET
Downloading and Viewing the demos
Using AJAX bar graph in your applications
Creating new ASP.NET AJAX website. <atlas:ScriptManager ID="ScriptManager1" runat="server" />
Writing the server side method
using System.Web.Services;
//Class declaration
[WebMethod]
public int GetNextValue()
{
//In real life get data from database and return to the caller
Random r = new Random();
System.Threading.Thread.Sleep(r.Next(100, 500));
return r.Next(1, 199);
}
HTML required for Graph <table class="gridTable">
<tr valign="bottom">
<td><div id="d1" class="gridDiv"></div> </td>
<td><div id="d2" class="gridDiv"></div></td>
<!-- add desired number of <td> tags here -->
<td><div id="d14" class="gridDiv"></div></td>
</tr>
</table>
Add the magic Javascript <script type="text/javascript" src=AJAXGraph.js></script> Once this is done time to write your own javascript. This section is important. Most of the time copy paste might work , but at times you will want to implement your own custom behavior. Add the following javascript just after the above above script line, before the <body> tag: <script language="javascript">
var start =1;var mygraph;
function OnComplete(a)
{
mygraph.PlotValue(a);
if(start ==0)return;
window.setTimeout("PageMethods.GetNextValue(OnComplete,null)",500);
}
function init()
{ mygraph = new AJAXGraph(['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','d11','d12','d13','d14','d15','d16']);
PageMethods.GetNextValue(OnComplete,null);
}
</script>
There are a few important things to explain here. First , notice the long list of parameters (d1..d16) being passed while constructing a new AJAXGraph object. This is actually list of the <div> tags enclosed in the <td>'s. Looks a bit awkward, but right now I don't have better way of doing this. The init() function is called when 'onload' event fires. Don't miss this line: <body onload=init()> The PageMethods object houses javascript functions for every 'WebMethod' defined in code behind. So, PageMethods.GetNextValue(OnComplete,null) will execute the 'GetNextValue' method on server. The 'OnComplete' function acts like a callback function for AJAX request. When a response is received , it gets called. Call to 'mygraph.PlotValue' will automatically plot the new value received from server onto the graph. Note that the value returned from server should not be greater than the height of table itself. The value actually represents height in pixels of a bar in the graph. You should ideally process value returned form server so that it is under required limit. Also note that server method only returns an integer, so consumes less network bandwidth. You can vary the speed at which the graph refreshes by varying the second parameter in the call to window.setTimeout, in the 'OnComplete()' function. For more clearer picture, you can always refer to the source of downloaded files.
Just by changing the CSS classes you can create some stunning looking UI.
Check out the screenshots below.
Hope you found this article interesting. As always, mail me your suggestions
and questions at |
| Copyright (c) 2007-2008 Ashish Patil . Please read FAQ for more details. |