Solved

Graph in VB6

Posted on 2002-03-03
7
472 Views
Last Modified: 2012-06-27
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
Comment
Question by:khaledc
  • 3
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
billy21 earned 50 total points
ID: 6838421
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
 
LVL 1

Author Comment

by:khaledc
ID: 6838921
Actually that was my choice, but I was wondering how fast I can build an application with it!
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6839245
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:mdougan
ID: 6887057
Hello?
0
 
LVL 1

Author Comment

by:khaledc
ID: 6887384
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
 
LVL 18

Expert Comment

by:mdougan
ID: 6890949
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
 
LVL 1

Author Comment

by:khaledc
ID: 6891986
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

17 Experts available now in Live!

Get 1:1 Help Now