Using csDrawGraph in Visual Basic

csDrawGraph can be used with Visual Basic. The instructions that are included with the component are aimed at ASP developers using VBScript, so the main points of interest for VB users are explained here.

We also have an ActiveX / OCX control, csXGraph, and in most cases this is more suitable for use in a visual environment.

First the DLL must be registered on the machine that will run the application. There is a detailed description of registration here if you are not familiar with this.

Next, import the type library into Visual Basic. In the VB development environment select "Project" from the menu bar, then "References". The dialogue box will then show available type libraries. Scroll down to "csDrawGraph Library", check the box and click OK. This will add the "draw" class from csDrawGraph to the Object Browser making it available for early binding.

We have a simple example project that you can download. It plots a bar graph with 3 hard coded values. It demonstrates how to display the graph in a VB picture box and this takes up most of the code.

Download the example - vbgraph.zip (2 KB)

Download the trial csDrawGraph component - csdgt.zip (619 KB)

The code for the example is listed below.

Private Type PicBmp
  Size As Long
  Type As Long
  hBmp As Long
  Reserved As Long
End Type

Private Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(7) As Byte
End Type

Private Declare Function OleCreatePictureIndirect Lib _
  "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
  ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Private Sub Command1_Click()
  Dim Graph As draw
  Dim Pic As PicBmp
  Dim r As Long
  Dim IPic As IPicture
  Dim IID_IDispatch As GUID
  With IID_IDispatch
    .Data1 = &H20400
    .Data4(0) = &HC0
    .Data4(7) = &H46
  End With
  Set Graph = CreateObject("csDrawGraphTrial.Draw")
  Graph.AddData "Item1", 5, "ff0000"
  Graph.AddData "Item2", 25, "00ff00"
  Graph.AddData "Item3", 15, "0000ff"
  With Pic
    .Size = Len(Pic)
    .Type = vbPicTypeBitmap
    .hBmp = Graph.BMPHandle(gtBar)
  End With
  r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
  Picture1.Picture = IPic
End Sub

Most of this code involves the use of the "OleCreatePictureIndirect" API call to create a bitmap that can be displayed in the picture box. csDrawGraph is able to export the bitmap handle using the BMPHandle method. The parameter of gtBar, gtPie or gtLine will determine the graph type. Note that the csDrawGraph object is created using the CreateObject command:

Dim Graph As draw
Set Graph = CreateObject("csDrawGraphTrial.Draw")

With the full version of the component use "csDrawGraph.Draw".

One drawback with using csDrawGraph in a VB application is the fact that the DLL needs to be distributed with each application. Our standard licence terms do not allow reselling of the DLL. For "in-house" applications this is not an issue as the multiple server licence allows for any number of installations within a single company or organisation. The single server licence, allows for one installation. If you need to distribute your application you should use our ActiveX control, csXGraph.