How to Create a Chart with HTML/CSS/ASP/AJAX Part 2(ASP Chart Generation)
In part 1, we built all of the client side javascripts that will call the server side scripts to generate the chart. You will recall that in our javascript function getChart, the url that is used is generatechart.asp. The requirements called for a single chart to be generated with thresholds. If there was not a one day turnaround on this, we could have made the code a bit more portable and even included a database backend that would allow the users to change thresholds.
generatechart.asp
We will start by defining a few variables. Though ASP does not force you to define variables, it is should be standard practice. It also will help you for trying to redefine the same variable several times in your code.
' define the variables for the database functions
Dim rs, db, query ' Respectively recordset, database string, query string
' define the variables for storing our HTML strings.
Dim lHtml, cHtml, vHtml ' line Html, chart Html, table Html, values Html
' counters
Dim x ' x is for numbering the chart values
The vHtml variable is used to display the numbering on the gridline to the right of the chart. I needed to show in increments of 25 with a maximum value of 500. So I started at 500 and decremented the number by 25 every pass. Later we will use the vHtml string in our chart.
vHtml = "
"
x = 500
while x > 0
vHtml = vHtml & "
" & x & "
" & vbCrLf
x = x - 25
wend
vHtml = vHtml & "
"
Now we can initialize out lHtml and cHtml variables as well as our database variables
cHtml = "" & vHtml & "$#60;/td>" $amp; vbCrLf
lHtml = "Line"
query = "SELECT lineid, numcartons FROM t_loadedcartons"
db = "Provider=SQLOLEDB.1;Password=password;Persist Security Info=True; User ID=userid; Initial Catalog=LineProduction;Data Source=.\SQLExpress"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = db
rs.Source = query
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 1
rs.Open()
while not rs.EOF
call buildChart(rs.Fields.Item("lineid").Value, rs.Fields.Item("numcartons").Value)
rs.MoveNext
wend
' close the recordset
rs.Close()
We made our database call and looped through the recordset calling buildChart. This is the final item we have to build yet, and as you will see, each time it is called, it adds to the cHtml string.
function buildChart(lineId, numcartons)
' variable to hold what the theshold color should be
Dim style, divtag
if lineId = "" then
exit function ' lineId is a string value in the database. If it is empty, we exit the function
end if
if isnull(numcartons) then
numcartons = 0
end if
if numcartons = 0 then
style = "nocartons"
elseif (count > 0) and (count < 50) then
style = "lowest"
elseif (count >= 50) and (count < 100) then
style = "low"
elseif (count >= 100) and (count < 150) then
style = "normal"
elseif (count >= 150) and (count < 350) then
style = "high"
else
style = "highest"
end if
divtag = ""
lHtml = lHtml & "" & lineId & "" & vbCrLf
cHtml = cHtml & "" & numcartons & divtag & "" & vbCrLf
end function
Finally, we are ready to return the HTML data for viewing.
Response.Write("
This is just a very simple example of how to create a chart. I like this suggestion over at Create html bar graph in ASP/VBSscript for creating a function that is reusable. You would just need to come up with a method for dealing with thresholds.
No Comment
No comments yet
Leave a reply