Solved

# How to draw Graph ?

Posted on 2002-07-18
645 Views
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
Question by:pramod_kumar_tk
[X]
###### 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
• 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.

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

0

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

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 70

Expert Comment

ID: 7164468
0

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
0

Author Comment

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

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.

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 70

Expert Comment

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

ID: 7373334
0

## Featured Post

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
###### Suggested Courses
Course of the Month5 days, 5 hours left to enroll