Solved

How to draw Graph ?

Posted on 2002-07-18
8
639 Views
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
MSChart.
Plz, Help me
0
Comment
Question by:pramod_kumar_tk
  • 2
  • 2
  • 2
  • +2
8 Comments
 

Expert Comment

by:Amitabh
ID: 7163998

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

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

http://www.microsoft.com/mobile/developer/technicalarticles/chartevb.asp

0
 
LVL 17

Accepted Solution

by:
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
Next

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

' 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
        Else
            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
        Else
            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

Me.Show
DoEvents


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

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

End Sub


0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7164468
0
 

Author Comment

by:pramod_kumar_tk
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Expert Comment

by:Amitabh
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.

Regards,

A.Pandey

'==================================================
'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)
Else
  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
    Else
      ligChart.PointValue(PointNumber) = 0
    End If
  Next 'pointnumber
  ligChart.NumPointsInLine = 7
Next

' Draw chart
ligChart.Redraw

0
 
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>.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7373334
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 55
Visual Studio search word table and return Cell index 8 49
VBA Shell can't Find Word document 11 77
clicking a shape in a frame array vb6 3 40
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now