• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

Graph in VB6

Dear All,

I am using Graph32.ocx to plot some graphs. So far, Graph32.ocx is doing what I want in a sense that it is sufficient to my work. However, the need has arised to plot different x values (which Graph32.ocx can't do). Therefore, I was wondering what a good quick solution to solve my problem? (without buying a software or writing my own code).

Thank you for your help in advance.
0
khaledc
Asked:
khaledc
  • 3
  • 3
1 Solution
 
billy21Commented:
Have you tried mschrt20.ocx?  Otherwise known as the mschart control.  Its very flexible and there is a great deal of help available on it in MSDN.  I would not recommend using a 3rd party chart/graph control.
0
 
khaledcAuthor Commented:
Actually that was my choice, but I was wondering how fast I can build an application with it!
0
 
mdouganCommented:
You have placed pretty tough restrictions - "without buying a software or writing my own code".  

Unfortunately, I don't think that either Graph32 or MSChart allow you to have much control over how the x values are plotted.  I recently answered a similar question from someone who wanted the x Values to go in the opposite direction from the way that the chart control automatically plots them.  Here is the answer I provided.  It's an example of how to do custom charting using only a visual basic picture box.  You might want to look at the original question, as I also show how to create a chart using Ole Automation to Excel behind the scenes, and then copying to a VB picturebox:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=vbcontrols&qid=20248751


[PAQ]
Here is a quick example of drawing a chart using VB

The only thing I left out of your request was the footnote (I'm sure you'll be able to see how you could
produce this) and also I didn't plot the points for 0.25 and 0.5 because it complicated my loop array
processing, but I assume that you'll be pulling your x values out of some column anyway, so, rather
than use the x value from a loop variable as I am, you'll just use the x value from your recordset.

From this you should be able to get the basic idea of how you can graph in a VB picturebox.

Suggestions.  I think I forgot to set the picturebox's AutoRedraw property to true, but this is something
you should probably do.

The biggest "trick" to getting this code to work was in setting the picturebox's ScaleMode property
to User Defined, and setting the ScaleTop property to -30 (that means that the top of the picture box
would have a Y value of -30, and a scaleHeight of +230 which means that the max Y value would be 200
(this leaves room for the legend), likewise the ScaleLeft property is set to -3 and the ScaleWidth property
is set to 15, this means the minimum X would be -3 and the maximum X would be 12.  Now, I choose to
draw the Axis lines at X = 0 and Y = 130.

You might be able to print such a chart, by either using a windows API call to BitBlt to copy the picture
to the printer.  Or, you could probably copy each routine, replacing the picChart. with Printer. and
then at the end, issue a Printer.EndDoc

Let me know if this is clear enough or not. (I also sent this to your e-mail address)

To use this code, create a regular project, add one big picture box on the form, call it picChart, add
one command button, call it cmdChart.  Set the Scale properties of the picChart as I outlined above.
 Copy and paste all of this code to the form's code section (making sure to clean up any line wraps)
and run.

Dim aRed(8) As Long
Dim aBlue(8) As Long
Dim aGreen(8) As Long

Private Sub cmdDraw_Click()
   picChart.Cls
   picChart.DrawStyle = vbSolid
   picChart.ForeColor = vbBlack
   DrawYAxis
   DrawXAxis
   DrawLegend
   ChartArrays
End Sub

Private Sub DrawYAxis()
Dim i As Long

' Draw Axis
   picChart.Line (0, -10)-(0, 130)
   picChart.CurrentX = -0.15
   picChart.CurrentY = -20
   picChart.Print "Y"
   
   For i = -10 To 130 Step 10
       picChart.CurrentX = -0.75
       picChart.CurrentY = i - 2.5
       picChart.Print CStr(i)
       picChart.Line (-0.15, i)-(0.15, i)
   Next i
   
End Sub

Private Sub DrawXAxis()
Dim i As Long
   
' Draw Axis
   picChart.Line (0, 130)-(8, 130)
   picChart.CurrentX = 8.25
   picChart.CurrentY = 128
   picChart.Print "X"

   picChart.CurrentX = 0.25 - 0.2
   picChart.CurrentY = 135
   picChart.Print Format(0.25, ".00")
   picChart.Line (0.25, 128)-(0.25, 132)

   picChart.CurrentX = 0.5 - 0.1
   picChart.CurrentY = 135
   picChart.Print Format(0.5, ".00")
   picChart.Line (0.5, 128)-(0.5, 132)

   For i = 1 To 8 Step 1
       picChart.CurrentX = i - 0.05
       picChart.CurrentY = 135
       picChart.Print CStr(i)
       picChart.Line (i, 128)-(i, 132)
   Next i
End Sub

Private Sub DrawLegend()
' Red Series
   picChart.CurrentX = 0
   picChart.CurrentY = 160
   picChart.ForeColor = vbRed
   picChart.Print "Red Series"
   picChart.DrawStyle = vbDot
   picChart.Line (3, 165)-(7, 165)
   picChart.DrawStyle = vbSolid
   picChart.Circle (5, 165), 0.1

' Blue Series
   picChart.CurrentX = 0
   picChart.CurrentY = 170
   picChart.ForeColor = vbBlue
   picChart.Print "Blue Series"
   picChart.Line (3, 175)-(7, 175)
   
   picChart.CurrentX = 4.95
   picChart.CurrentY = 172.5
   picChart.Print "X"
   
' Green Series
   picChart.CurrentX = 0
   picChart.CurrentY = 180
   picChart.ForeColor = vbGreen
   picChart.Print "Green Series"
   picChart.Line (3, 185)-(7, 185)
   

End Sub

Private Sub ChartArrays()
Dim i As Long

picChart.ForeColor = vbRed

For i = 0 To 7
   picChart.DrawStyle = vbDot
   picChart.Line (i, aRed(i))-(i + 1, aRed(i + 1))
   picChart.DrawStyle = vbSolid
   picChart.Circle (i + 1, aRed(i + 1)), 0.1
Next i

picChart.ForeColor = vbBlue
picChart.DrawStyle = vbSolid

For i = 0 To 7
   picChart.Line (i, aBlue(i))-(i + 1, aBlue(i + 1))
   picChart.CurrentX = (i + 1) - 0.05
   picChart.CurrentY = aBlue(i + 1) - 2.5
   picChart.Print "X"
Next i

picChart.ForeColor = vbGreen

For i = 0 To 7
   picChart.Line (i, aGreen(i))-(i + 1, aGreen(i + 1))
Next i


End Sub


Private Sub Form_Load()
Dim i As Long

Randomize
For i = 0 To 8
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
   aRed(i) = Int((130 - (-10) + 1) * Rnd + (-10))
Next i

For i = 0 To 8
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
   aBlue(i) = Int((130 - (-10) + 1) * Rnd + (-10))
Next i

For i = 0 To 8
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
   aGreen(i) = Int((130 - (-10) + 1) * Rnd + (-10))
Next i

End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mdouganCommented:
Hello?
0
 
khaledcAuthor Commented:
mdougan,

Although your code above may (or may not) work, I was specific in my question to the fact that I need an alternative to Graph32.ocx and not writing code. I appreciate your effort.

However, billy21 had a better suggestion than yours.

Regards.
0
 
mdouganCommented:
No problem, it's just that you need to close out your questions once they have been answered to your satisfaction.  That makes them available to other people as a PAQ, and that is what makes this site valuable to more people than just the one asking the question.
0
 
khaledcAuthor Commented:
mdougan,

Thank you for your advice. I will make sure this would be the case for any of my future questions.

Regards.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now