Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw a simple graph with Visual Basic alone
Keywordsgraph, equation
CategoriesGraphics
 
Use the Line method to graph equations. Draw the axes first. Then loop over X coordinates drawing lines between points on a function.
 
Private Sub Form_Load()
Dim i As Integer
Dim x As Single
Dim y As Single

    Picture1.Scale (-10, 10)-(10, -10)
    
    ' Draw X axis.
    Picture1.Line (-10, 0)-(10, 0)
    For i = -9 To 9
        Picture1.Line (i, -0.5)-(i, 0.5)
    Next i

    ' Draw Y axis.
    Picture1.Line (0, -10)-(0, 10)
    For i = -9 To 9
        Picture1.Line (-0.5, i)-(0.5, i)
    Next i
    
    ' Draw y = 4 * sin(x).
    Picture1.ForeColor = vbRed
    x = -10
    y = 4 * Sin(x)
    Picture1.CurrentX = x
    Picture1.CurrentY = y
    For x = -10 To 10 Step 0.25
        y = 4 * Sin(x)
        Picture1.Line -(x, y)
    Next x
    
    ' Draw y = x ^ 3 / 5 - 3 * x + 1.
    Picture1.ForeColor = vbBlue
    x = -10
    y = x ^ 3 / 5 - 3 * x + 1
    Picture1.CurrentX = x
    Picture1.CurrentY = y
    For x = -10 To 10 Step 0.25
        y = x ^ 3 / 5 - 3 * x + 1
        Picture1.Line -(x, y)
    Next x
End Sub
 
My book Custom Controls Library implements 101 custom controls including a simple graph control. It provides many more features than this example but you can still modify it easily.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated