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.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 |
| pingrange | 33268 | 44664 | 74.485043800 |
| url2disk | 2094 | 44664 | 4.688339600 |
| pinghost | 5649 | 44664 | 12.647770000 |
| bus-telnet | 2788 | 44664 | 6.242163700 |
| winsched | 865 | 44664 | 1.936682700 |
<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> (<%=objRS("value_column") %> - <%=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
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OOOOOOOOOOOOO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,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,
@@ winsched: 968 (2%)
XX bus-telnet: 2926 (6%)
ll url2disk: 2492 (5%)
OO pinghost: 6050 (11%)
,, pingrange: 40523 (77%)
<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:




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