How to draw Graph ?

Posted on 2002-07-18
Last Modified: 2008-03-17
Hi Experts,

I want to draw the data in a graph ? How can I draw Graphs ?
I had seen MSChart ctrl , but I can't understan it .
Is there any other ctrl to plot graph ? or can give me details how to plot graph on
Plz, Help me
Question by:pramod_kumar_tk
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 7163998

go to project -> components -> insertable objects -> microsoft graph 2000 chart

the following is the link which shows a sample code using it.

LVL 17

Accepted Solution

inthedark earned 200 total points
ID: 7164332
You don't need chart control to draw a graph, if you want a simple method use the line method of a picture box.

1) New Project
2) Place a picture box on your form - set autoredraw=true
3) Paste the following code

Option Explicit

Sub PlotGraph(picG As VB.PictureBox, Values(), Optional Style$ = "B")

Dim topmarg As Single
Dim botmarg As Single
Dim leftmarg As Single
Dim rightmarg As Single
Dim sw As Single
Dim sh As Single

Dim mx As Single
Dim my As Single

Dim MaxValue, MinValue

topmarg = 500 ' set some clear space at the top for titles
botmarg = 500 ' and some clear space at the bottom
leftmarg = 500 ' and some clear space at the left
rightmarg = 500 ' and some clear space at the right

sw = picG.ScaleWidth ' total width to play with
sh = picG.ScaleHeight ' total height
Dim pt As Long  ' current plot
Dim ox As Single    ' origin
Dim oy As Single    ' origin

Dim x1 As Single    ' current plot coord
Dim y1 As Single    ' current plot coord
Dim x2 As Single    ' current plot coord
Dim y2 As Single    ' current plot coord

' now calculate origin and starting point of the graph
' the origin is the bottom left part of the actual grid
ox = leftmarg
oy = sh - botmarg

' now calculate amount of space within the margins
' max values for x and y values
mx = sw - leftmarg - rightmarg
my = sh - topmarg - botmarg

' so now we need to know your sample size:
Dim Count As Single
Dim pw As Single

Count = UBound(Values) + 1

' width of each plot
pw = Int(mx / Count)

' now get max value
MaxValue = 0
For pt = 0 To UBound(Values)
    If Values(pt) > MaxValue Then
        MaxValue = Values(pt)
    End If

' now get min value
MinValue = MaxValue
For pt = 0 To UBound(Values)
    If Values(pt) < MinValue Then
        MinValue = Values(pt)
    End If

' now find a suitable scale grade
Dim range As Single

range = MaxValue - MinValue

Dim ScaleLeaps As Single

' Now plot BAR graph
If UCase(Style) = "B" Then

    For pt = 0 To UBound(Values)
        ' calc x value
        x1 = ox + (pt * pw) ' origin + plotnum * plotwidth
        x2 = x1 + pw ' - gap ' if you need a gap e.g. gap=2

        ' calc y value
        If range = 0 Then
            y1 = oy
            y1 = oy - (my * ((Values(pt) - MinValue) / range))
        End If
        y2 = oy
        picG.Line (x1, y1)-(x2, y2), RGB(0, 0, 255), BF
    Next pt
End If

Dim onedone As Boolean

If UCase(Style) = "L" Then
    onedone = False
    For pt = 0 To UBound(Values)
        ' calc x value
        x2 = ox + (pt * pw) ' origin + plotnum * plotwidth
        ' calc y value
        If range = 0 Then
            y2 = oy
            y2 = oy - (my * ((Values(pt) - MinValue) / range))
        End If
        If onedone Then
            picG.Line (x1, y1)-(x2, y2), RGB(0, 0, 255)
        End If
        onedone = True
        x1 = x2
        y1 = y2
    Next pt

End If

End Sub

Private Sub Form_Load()

ReDim Values(9)
Values(0) = 1
Values(1) = 2
Values(2) = 3
Values(3) = 4
Values(4) = 5
Values(5) = 6
Values(6) = 7
Values(7) = 8
Values(8) = 9
Values(9) = 10


PlotGraph Picture1, Values(), "L" ' line

'PlotGraph Picture1, Values(), "B" ' bar

End Sub

LVL 69

Expert Comment

by:Éric Moreau
ID: 7164468

Author Comment

ID: 7166123
Hi all
I will check ur comment later.
I had seen some example in msdn which draw graph using excel. but unfortunatly the parameters for ChartWizard which is not easy
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.


Author Comment

ID: 7166127
Hi Amithab
It is vc++ code not vb
I need it in vb

Expert Comment

ID: 7166247
Hi promod

its actually a chart control with its source code in VC++. but as we can use any other COM component, we can use this control in VB.

the sample source code for using this chart is attached in the article itself. i am once again writing that portion  below.

would request you to go through this article. am sure you would find it helpful.



'Sample Code
'The complete source code for the procedure that draws the chart looks like this:

Dim PointNumber As Long
Dim LineNumber As Long
Dim FirstPoint As Long

' Initalize chart
ligChart.HorizTicLines = txtHorizTicLines.Text
ligChart.VertTicLines = txtVertTicLines.Text
ligChart.BarGraph = (optBarChart.Value = True)
If optBackgroundWhite.Value Then
  ligChart.BgndColor = RGB(255, 255, 255)
  ligChart.BgndColor = RGB(0, 0, 0)
End If
ligChart.Ystart = 0
ligChart.Yrange = 100

' Set chart data
For LineNumber = 0 To 6
  ligChart.LineNumber = LineNumber
  'ligChart.LineColor = RGB(255, 0, 0)
  ligChart.LineThickness = txtLineThickness.Text
  ligChart.LinestyleSolid = chkLineStyleSolid.Value = 1
  FirstPoint = Rnd(1) * 100
  For PointNumber = 0 To 6
    FirstPoint = FirstPoint + (Rnd(1) * 20 - 10)
    If FirstPoint < 0 Then FirstPoint = 0
    If PointNumber = LineNumber Or ligChart.BarGraph = False Then
      ligChart.PointValue(PointNumber) = FirstPoint
      ligChart.PointValue(PointNumber) = 0
    End If
  Next 'pointnumber
  ligChart.NumPointsInLine = 7

' Draw chart

LVL 69

Expert Comment

by:Éric Moreau
ID: 7348093
This question appears to be abandoned. A question regarding it will be left in the CleanUp
area; if you have any comment about the question, please leave it here.

Unless there is objection or further activity, one of the moderators will be asked to accept the comment
of <emoreau>.


Expert Comment

ID: 7373334

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now