ChartDirector 6.0 (ASP/COM/VB Edition)

Direct Database Access

In this example, we will demonstrate how to create a web page to show the monthly revenue for a given year. The user will select a year from a HTML form and press OK. The web server will query a database to obtain the necessary data, and return a web page containing the bar chart for the selected year.

The code for producing the HTML form is listed below. It outputs a drop down select list to allow the user to select a year. Based on the selected year, it uses an <IMG> tag with a ChartDirector script as the URL and the selected year as query parameter. The ChartDirector script will generate the chart image based on the selected year and deliver it to the browser.

[Web Version (in ASP)] aspdemo\dbdemo1.asp
<%@ language="vbscript" %>
Set cd = CreateObject("ChartDirector.API")

' The currently selected year
selectedYear = Request("year")
If Request("year") = "" Then
    selectedYear = 2001
    selectedYear = CInt(Request("year"))
End If

' The following code generates the <option> tags for the HTML select box, with the <option> tag
' representing the currently selected year marked as selected.

ReDim optionTags(2001 - 1990)
For i = 1990 To 2001
    If i = selectedYear Then
        optionTags(i - 1990) = "<option value='" & i & "' selected>" & i & "</option>"
        optionTags(i - 1990) = "<option value='" & i & "'>" & i & "</option>"
    End If
selectYearOptions = Join(optionTags, "")
<body style="margin:5px 0px 0px 5px">
<div style="font-size:18pt; font-family:verdana; font-weight:bold">
    Database Integration Demo (1)
<hr style="border:solid 1px #000080" />
<div style="font-size:10pt; font-family:verdana; margin-bottom:20px">
&#8226; <a href="viewsource.asp?file=<%=Request("SCRIPT_NAME")%>">
    View containing HTML page source code
<br />
&#8226; <a href="viewsource.asp?file=dbdemo1a.asp">
    View chart generation page source code
<br />
<br />
    I want to obtain the revenue data for the year
    <select name="year">
    <input type="submit" value="OK">

<img src="dbdemo1a.asp?year=<%=selectedYear%>">


As seen from the code above, the chart is created by the URL in the <IMG> tag, which is "dbdemo1a.asp". The source code of "dbdemo1a.asp" is as follows.

[Web Version (in ASP)] aspdemo\dbdemo1a.asp
<%@ language="vbscript" %>
Set cd = CreateObject("ChartDirector.API")

' Displays the monthly revenue for the selected year. The selected year should be passed in as a
' query parameter called "year"
selectedYear = Request("year")
if selectedYear = "" Then selectedYear = 2001

' SQL statement to get the monthly revenues for the selected year.
SQL = "Select Software, Hardware, Services From revenue Where Year(TimeStamp) = " & selectedYear & _
    " Order By TimeStamp"

' Connect to database and read the query result into arrays

Set rs = CreateObject("ADODB.RecordSet")
Call rs.Open(SQL, "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Set dbTable = cd.DBTable(rs)

software = dbTable.getCol(0)
hardware = dbTable.getCol(1)
services = dbTable.getCol(2)

' Now we have read data into arrays, we can draw the chart using ChartDirector

' Create a XYChart object of size 600 x 300 pixels, with a light grey (eeeeee) background, black
' border, 1 pixel 3D border effect and rounded corners.
Set c = cd.XYChart(600, 300, &Heeeeee, &H000000, 1)
Call c.setRoundedFrame()

' Set the plotarea at (60, 60) and of size 520 x 200 pixels. Set background color to white (ffffff)
' and border and grid colors to grey (cccccc)
Call c.setPlotArea(60, 60, 520, 200, &Hffffff, -1, &Hcccccc, &Hccccccc)

' Add a title to the chart using 15pt Times Bold Italic font, with a light blue (ccccff) background
' and with glass lighting effects.
Call c.addTitle("Global Revenue for Year " & selectedYear, "timesbi.ttf", 15).setBackground( _
    &Hccccff, &H000000, cd.glassEffect())

' Add a legend box at (70, 32) (top of the plotarea) with 9pt Arial Bold font
Call c.addLegend(70, 32, False, "arialbd.ttf", 9).setBackground(cd.Transparent)

' Add a stacked bar chart layer using the supplied data
Set layer = c.addBarLayer2(cd.Stack)
Call layer.addDataSet(software, &Hff0000, "Software")
Call layer.addDataSet(hardware, &H00ff00, "Hardware")
Call layer.addDataSet(services, &Hffaa00, "Services")

' Use soft lighting effect with light direction from the left
Call layer.setBorderColor(cd.Transparent, cd.softLighting(cd.Left))

' Set the x axis labels. In this example, the labels must be Jan - Dec.
labels = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")
Call c.xAxis().setLabels(labels)

' Draw the ticks between label positions (instead of at label positions)
Call c.xAxis().setTickOffset(0.5)

' Set the y axis title
Call c.yAxis().setTitle("USD (Millions)")

' Set axes width to 2 pixels
Call c.xAxis().setWidth(2)
Call c.yAxis().setWidth(2)

' Output the chart in PNG format
Response.ContentType = "image/png"
Response.BinaryWrite c.makeChart2(cd.PNG)

The first part of the above code is a database query using Microsoft ADO. The SQL statement is:

Select Software, Hardware, Services From revenue Where Year(TimeStamp) = " & _
    selectedYear & " Order By TimeStamp

The code then uses DBTable to read in the RecordSet using the first column as the array index. (Please refer to Reading Data from Databases for information on how to use DBTable and array index.)

After the data is read into the DBTable object, the second part of the code is to create a stacked bar chart using the data. This is very similar to the examples in other parts of this documentation, so it will not be explained further.