06/Feb/2010 - Darren Dignam

Graphing with SQL and ASP

Different ways of displaying your data.

Here are some ways to present your data in a visual way. This is a mixture of ASP and SQL but can be replicated with PHP if you need to. I am also going to assume you are fairly familliar with these technologies but if you need to brush up, then checkout w3schools.
The data source for this posting is going to be the downlaod tracker that is used for the blocsoft freeware downloads.

Tabular Data:

This is the simplist of tables. We loop through the rows of the dataset, and construct a table.

File

Times Downloaded

pingrange 40523
url2disk 2492
pinghost 6050
bus-telnet 2926
winsched 968

Total Downloads: 52959

The source code to create this table is here:
<%
mySQL = "SELECT FileName, Clicks FROM TrackerTable"
Set objRS = oConn.Execute(mySQL)
total=0
%>
<table border="1">
<tr><td><h3>File</h3></td><td><h3>Times Downloaded</h3></td></tr>
<%do until objRS.EOF%>
   <tr>
   <%for each x in objRS.Fields%>
      <td><%Response.Write(x.value)%></td>
   <%
   if x.name="Clicks" then total = total + x.value
   next
   objRS.MoveNext%>
   </tr>
<%loop 
objRS.Close
Set objRS = Nothing
%>
</table>
Total Downloads: <%=total %>

Bar Graph:

In this example we will construct some DIVs and use inline styles to resize them, so that they look like a bar graph.
pingrange (40523 - 76.52%)
url2disk (2492 - 4.71%)
pinghost (6050 - 11.42%)
bus-telnet (2926 - 5.52%)
winsched (968 - 1.83%)

There is a little more work required to get this effect, but it requires no javascript, and only a single SQL lookup to gather all the values needed. You can build upon this for your own needs - use PHP - or have vertical bars.

The SQL statement is a bit more complicated for this lookup, we have additional fields being generated with totals and percentages calculated too. Take a look:

SELECT name_column, value_column, TotalDownloads, @percent :=100 * ( value_column /  TotalDownloads  ) AS percentage
FROM (
	SELECT FileName AS name_column, Clicks AS value_column
	FROM TrackerTable
)select_values, (
	SELECT SUM( Clicks ) AS TotalDownloads
	FROM TrackerTable
)select_total 
The select statement in red is our basic SQL command from above, but we are nesting it within another SELECT. This is so we can calculate the total, and then the percentage. The tabular output of this command looks something like this:

name_column

value_column

TotalDownloads

percentage

pingrange332684466474.485043800
url2disk2094446644.688339600
pinghost56494466412.647770000
bus-telnet2788446646.242163700
winsched865446641.936682700
These fields can then be used to define the widths of the DIV in the graph. Here is the code:
<style type="text/css">
<!--
/* Some Styling Stuff */
#barChart{
	background:#70bff8;
	padding:0 10px 10px;
	margin:10px;
}
#barChart div{
	background:#bbb;
	border-bottom:1px solid #000;
	margin-top:-5px;
}
#barChart div div{
	height:33px;
	background:#fff;
	border-bottom:1px solid #000;border-left:2px solid #000;
}
#barChart span{
	position:relative;
	top:20px;
	left:10px;
}
-->
</style>
<div id="barChart">
<%
mySQL = "SELECT name_column, value_column, TotalDownloads, @percent :=100 * ( value_column /  TotalDownloads  ) AS percentage FROM ( SELECT FileName  AS name_column, Clicks AS value_column FROM TrackerTable )select_values, ( SELECT SUM( Clicks ) AS TotalDownloads FROM TrackerTable )select_accumulating_value"
Set objRS = oConn.Execute(mySQL)

do until objRS.EOF  %>
<span><strong><%=objRS("name_column") %></strong>&#160;(<%=objRS("value_column") %>&#160;-&#160;<%=Round(objRS("percentage"),2) %>%)</span>
<div><div style="width:<%=Round(objRS("percentage")) %>%"></div></div>
<%
objRS.MoveNext
loop 

objRS.Close
Set objRS = Nothing
%>
</div>
Here we can see some CSS styles being defined, with an ASP loop generating the bars. Our markup will consist of a container DIV barChart that defines the overall width of the chart. The line of code where we draw the actual bars is highlighted in red. The bars consist of two nested DIVs: the outer is 100% width, and the inner lighter coloured DIV, has its width defined inline style from the dataset. To get the desired look.

SQL Pie Chart:

Shlomi Noach explains over at his blog how to generate a pie chart with nothing other than SQL. Its quite remarkable really, he also suggests some real world uses for the technique.

                                                                                            
                                  ,,,,,,,,,,,,,,OOOOOOOOOOO                                 
                         ,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOOOOOO                        
                   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOOOOOOOOOOOO                  
               ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOOOOOOOOOOOOOllll              
            ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOOOOOOOOOlllllllllll           
          ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOOOOOOlllllllllllllllll         
        ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOOOOllllllllllllllllXXXXXXXX       
      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOllllllllllllXXXXXXXXXXXXXXXXXX     
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOllllllllXXXXXXXXXXXXXXXXXXXXXXXXXXX    
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOllllXXXXXXXXXXXXXXXXXXXXXXXXXX@@@@@@@@@    
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@   
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    
      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,     
        ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,       
          ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,         
            ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,           
               ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,              
                   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,                  
                         ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,                        
                                  ,,,,,,,,,,,,,,,,,,,,,,,,,                                 
                                                                                            
@@ winsched: 968 (2%)
XX bus-telnet: 2926 (6%)

ll url2disk: 2492 (5%)

OO pinghost: 6050 (11%)

,, pingrange: 40523 (77%)
This piechart including the legend, was generated by a single SQL statement! To learn how to make your own SQL piechart read Shlomi's blog, as I can not explain it any better! He also has some useful applications of the chart. The colouring was applied with client-side javascript afterwards, to help the regions stand out. Here is the script:
<script type="text/javascript"> 
var newString = ""; 
var theString = document.getElementById("pie").innerHTML; 
var counter = 0; 
var limit = theString.length;
var tmpChar = "";
var tmpStr = "";
for (counter;counter < limit ;counter++ ) { 
	tmpChar = theString.substring(counter, counter+1);
	switch(tmpChar)
	{
		case "@":
			tmpStr = "<span style='color:#fe2016'>"+tmpChar+"</span>";
			break;
		case "O":
			tmpStr = "<span style='color:#0d00f4'>"+tmpChar+"</span>";
			break;
		case "X":
			tmpStr = "<span style='color:#1fd90b'>"+tmpChar+"</span>";
			break;
		case ",":
			tmpStr = "<span style='color:#c516ca'>/</span>";
			break;
		default:
			tmpStr = tmpChar;
	}
	newString += tmpStr;  
} 
document.getElementById("pie").innerHTML = newString;
</script>

This piechart will not be to everyones taste, but you could tweak my colouring script above to make it look less ascii (with some images perhaps?). But if you are prepared to use javascript to draw graphs on your pages, then checkout these libraries:



Comments:

sdad
test - 7/2/2010 6:20:31 PM
asdasd

 (*required)
 (*private)
 (*required)
 
© 2009 BlocSoft.com All Rights Reserved. Contact