ChartDirector 6.0 (ASP/COM/VB Edition)

Realtime Chart with Snapshot (Web)

The example extends the Simple Realtime Chart (Web) example by adding buttons to download the chart in PNG and PDF formats.

When the button is pressed, the URL of the web page will be modified by appending an extra query parameter specifying the download format. On the server side, the charting code will check for this parameter. If the parameter exists, it will send the chart as an attachment. When the browser receives the attachment, instead of displaying it, it should save it as a file. The exact behaviour depends on the browser. Some browsers may immediately save the attachment as a file in a download directory; some browsers may prompt the user for the filename and directory; some browsers that do not support downloading files may ignore the content.

NOTE: This sample script uses "cdjcv.js". When developing your own script using this sample script as a template, please ensure you copy "cdjcv.js" to the proper directory and reference it using the proper path.

Source Code Listing

[Web Version (in ASP)] aspdemo\realtimesnapshot.asp
<!DOCTYPE html>
    <title>Realtime Chart with Snapshot</title>
    <script type="text/javascript" src="cdjcv.js"></script>
<body style="margin:0px">
<table cellspacing="0" cellpadding="0" style="border:black 1px solid;">
        <td align="right" colspan="2" style="background:#000088; color:#ffff00; padding:0px 4px 2px 0px;">
            <a style="color:#FFFF00; font:italic bold 10pt Arial; text-decoration:none" href="">
                Advanced Software Engineering
    <tr valign="top">
        <td style="width:130px; background:#c0c0ff; border-right:black 1px solid; border-bottom:black 1px solid;">
            <br />
            <br />
            <div style="font:12px Verdana; padding:10px;">
                <b>Update Period</b><br />
                <select id="UpdatePeriod" style="width:110px">
                    <option value="5">5</option>
                    <option value="10" selected="selected">10</option>
                    <option value="20">20</option>
                    <option value="30">30</option>
                    <option value="60">60</option>
                <br /><br /><br />
                <b>Time Remaining</b><br />
                <div style="width:108px; border:#888888 1px inset;">
                    <div style="margin:3px" id="TimeRemaining">0</div>
            <br />
            <br />
            <br />
            <br />
            <div style="text-align:center">
                <input type="button" value="Download PDF" onclick="download('pdf')" style="width:112px; font:10pt Arial" /><br />
                <input type="button" value="Download PNG" onclick="download('png')" style="width:112px; font:10pt Arial" />
        <td style="border-left:black 1px solid; padding:5px 0px 0px 5px;" >
            <!-- ****** Here is the image tag for the chart image ****** -->
            <img id="ChartImage1" src="realtimesnapshotchart.asp?chartId=demoChart1">
<script type="text/javascript">

// Executes once every second to update the countdown display. Updates the chart when the countdown reaches 0.
function timerTick()
    // Get the update period and the time left
    var updatePeriod = parseInt(document.getElementById("UpdatePeriod").value);
    var timeLeft = Math.min(parseInt(document.getElementById("TimeRemaining").innerHTML), updatePeriod) - 1;

    if (timeLeft == 0)
        // Can update the chart now
    else if (timeLeft < 0)
        // Reset the update period
        timeLeft += updatePeriod;

    // Update the countdown display
    document.getElementById("TimeRemaining").innerHTML = timeLeft;
window.setInterval("timerTick()", 1000);

// Send a request to the server with the query parameter "download=xxx". The server should handle this
// as a download request.
function download(format)
    var imageURL = document.getElementById("ChartImage1").src;
    imageURL += ((imageURL.indexOf('?') == -1) ? '?' : '&') + "download=" + format;

    // Download as an attachment to the current window
    location.href = imageURL;


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

' Data to draw the chart. In this demo, the data buffer will be filled by a random data generator.
' In real life, the data is probably stored in a buffer (eg. a database table, a text file, or some
' global memory) and updated by other means.

' We use a data buffer to emulate the last 240 samples.
sampleSize = 240
ReDim dataSeries1(sampleSize - 1)
ReDim dataSeries2(sampleSize - 1)
ReDim dataSeries3(sampleSize - 1)
ReDim timeStamps(sampleSize - 1)

' Our pseudo random number generator
firstDate = DateAdd("s", -UBound(timeStamps) + 1, Now)
For i = 0 To UBound(timeStamps)
    timeStamps(i) = DateAdd("s", i, firstDate)
    p = Second(timeStamps(i)) + Minute(timeStamps(i)) * 60 + Hour(timeStamps(i)) * 3600
    dataSeries1(i) = Cos(p * 2.1) * 10 + 1 / (Cos(p) * Cos(p) + 0.01) + 20
    dataSeries2(i) = 100 * Sin(p / 27.7) * Sin(p / 10.1) + 150
    dataSeries3(i) = 100 * Cos(p / 6.7) * Cos(p / 11.9) + 150

' Create an XYChart object 600 x 320 pixels in size
Set c = cd.XYChart(600, 320)

' Set the plotarea at (55, 60) and of size 520 x 235 pixels with transparent background and border.
' Enable both horizontal and vertical grids by setting their colors to grey (cccccc). Set clipping
' mode to clip the data lines to the plot area.
Call c.setPlotArea(55, 60, 520, 235, -1, -1, cd.Transparent, &Hcccccc, &Hcccccc)
Call c.setClipping()

' Add a title to the chart using dark grey (0x333333) 20pt Arial Bold font
Call c.addTitle("Realtime Chart with Snapshot", "arialbd.ttf", 20, &H333333)

' Add a legend box at the top of the plot area using horizontal layout. Use 10pt Arial Bold font,
' transparent background and border, and line style legend icon.
Set b = c.addLegend(55, 30, False, "arialbd.ttf", 10)
Call b.setBackground(cd.Transparent, cd.Transparent)
Call b.setLineStyleKey()

' Set the x and y axis stems to transparent and the label font to 10pt Arial
Call c.xAxis().setColors(cd.Transparent)
Call c.yAxis().setColors(cd.Transparent)
Call c.xAxis().setLabelStyle("arial.ttf", 10)
Call c.yAxis().setLabelStyle("arial.ttf", 10)

' Add y-axis title using 12pt Arial font
Call c.yAxis().setTitle("Y-Axis Title Placeholder", "arial.ttf", 12)

' For the automatic x and y axis labels, set the minimum spacing to 75 and 30 pixels.
Call c.xAxis().setTickDensity(75)
Call c.yAxis().setTickDensity(30)

' Set the x-axis label format
Call c.xAxis().setLabelFormat("{value|hh:nn:ss}")

' Create a line layer to plot the lines
Set layer = c.addLineLayer2()

' The x-coordinates are the timeStamps.
Call layer.setXData(timeStamps)

' The 3 data series are used to draw 3 lines. Here we put the latest data values as part of the data
' set name, so you can see them updated in the legend box.
Call layer.addDataSet(dataSeries1, &Hff0000, c.formatValue(dataSeries1(UBound(dataSeries1)), _
    "Alpha: {value|2}"))
Call layer.addDataSet(dataSeries2, &H00cc00, c.formatValue(dataSeries2(UBound(dataSeries2)), _
    "Beta: {value|2}"))
Call layer.addDataSet(dataSeries3, &H0000ff, c.formatValue(dataSeries3(UBound(dataSeries3)), _
    "Gamma: {value|2}"))

' Check if is download request
downloadFormat = Request("download")
If Not (downloadFormat = "") Then
    fname = "demo_" & c.formatValue(timeStamps(UBound(timeStamps)), "yyyymmddhhnnss")
    If downloadFormat = "pdf" Then
        ' Output in PDF and stream as attachment
        Response.ContentType = "application/pdf"
        Response.AddHeader "Content-Disposition", "attachment; filename=""" & fname & ".pdf"""
        Response.BinaryWrite c.makeChart2(cd.PDF)
        ' Output in PNG and stream as attachment
        Response.ContentType = "image/png"
        Response.AddHeader "Content-Disposition", "attachment; filename=""" & fname & ".png"""
        Response.BinaryWrite c.makeChart2(cd.PNG)
    End If
End If

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