Using csDrawGraph with ASP.NET

csDrawGraph can be used with ASP.NET and this page describes how. We also have a .NET component that might be preferable called csASPNetGraph.

To use csDrawGraph with .NET the component must be registered on the server in the usual way and it can be called using Server.CreateObject. For example:

Dim Image = Server.CreateObject("csDrawGraph.Draw")

Note the use of Dim compared with Set in classic ASP. For the trial version the class name is "csDrawGraphTrial.Draw"

The above code uses late binding. A more efficient way of calling the component is to use early binding, which means creating a .NET equivalent assembly using the TLBIMP tool.

There are some incompatibilities between the data structures used by csDrawGraph and those of .NET, the most important being the method used to stream an image to the browser. This can no longer be done in a single line but the example below shows how it can be done in VB.NET.

<%@ Page language="vb" debug=true %>
Response.Expires = 0
Response.Buffer = true

Dim Chart = Server.CreateObject("csDrawGraph.Draw")

Chart.AddData("Item1", 17, "ff0000")
Chart.AddData("Item2", 28, "00ff00")
Chart.AddData("Item3", 5, "0000ff")

Dim OutArray As Array = Chart.GIFBar
Dim ByteArray(OutArray.Length - 1) As Byte
Array.Copy(OutArray, ByteArray, OutArray.Length)
Response.ContentType = "image/gif"

The last block of code produces the bar chart as a GIF and streams it to the browser. The output from the GIFBar property is read into OutArray which is then copied into an array of bytes called ByteArray. This array of bytes can be used in the Response.BinaryWrite command. In old ASP the BinaryWrite command could take the value of GIFBar directly.

This is the sample image produced:

Using csDrawGraph in ASP.NET

There are other differences in syntax between VBScript and VB.NET, mostly involving the use of brackets. In VBScript the AddData command would not use brackets, but it does in VB.NET.

The code for streaming the image in C# is:

  Array OutArray = (Array)(Chart.GIFData);
  Byte[] ByteArray = new Byte[OutArray.Length];
  Array.Copy(OutArray, ByteArray, OutArray.Length);
  Response.ContentType = "image/gif";

With C#, early binding must be used.