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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

Expert Comment

by:Éric Moreau
ID: 7164468
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


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

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 70

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month6 days, 5 hours left to enroll

626 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