How to draw Graph ?

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
Who is Participating?
inthedarkConnect With a Mentor Commented:
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


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

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

Éric MoreauSenior .Net ConsultantCommented:
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

pramod_kumar_tkAuthor Commented:
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
pramod_kumar_tkAuthor Commented:
Hi Amithab
It is vc++ code not vb
I need it in vb
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

Éric MoreauSenior .Net ConsultantCommented:
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>.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.