wzm
asked on
VB6 Graph
Hi Experts,
How to do so that the program draws a graph with x and y axis, where user will input the x(s) and y(s) coord.length of the x and y-axis are fixed.
Thank you very much.
How to do so that the program draws a graph with x and y axis, where user will input the x(s) and y(s) coord.length of the x and y-axis are fixed.
Thank you very much.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Place a pictue box on your form say Picture1.
Private Sub Form_Load()
' Create an instance of your graph class
Dim GR As New zGraph
' add some values to your graph
Dim l As Long
For l = 1 To 30
GR.AddValue Rnd * 20
Next
GR.DrawGraph Picture1
End Sub
'=========================
Option Explicit
' Print a Graph Class
' Create by: Nick Young nyoung@vipintersoft.com
' Date: 23-Jun-02
' Please send any bug fixes & improvments to code.
' EXAMPLE USE:
'Private Sub Form_Load()
'
'' Create an instance of your graph class
'Dim GR As New zGraph
'
'
'' add some values to your graph
'Dim l As Long
'For l = 1 To 30
' GR.AddValue Rnd * 20
'Next
'
'GR.DrawGraph Picture1
'
'End Sub
Public Values
Public Captions
Dim mlValueCount As Long
Public NumberFormat As String
Dim mbPrintCaption As Boolean
Dim mbPrintScale As Boolean
Dim mbAutoScale As Boolean
Public Sub AddValue(pValue, Optional psCaption As String = "")
' Add an array
mlValueCount = mlValueCount + 1
If mlValueCount < 1 Then
ReDim Values(0)
ReDim Captions(0)
Else
ReDim Preserve Values(mlValueCount)
ReDim Preserve Captions(mlValueCount)
End If
Values(mlValueCount) = pValue
Captions(mlValueCount) = psCaption
If Len(psCaption) = 0 Then
Captions(mlValueCount) = CStr(mlValueCount + 1)
End If
End Sub
Public Sub DrawGraph(picGraph As VB.PictureBox)
Dim lc As Long
Dim MaxValue As Single
Dim MinValue As Single
MinValue = 99999999#
picGraph.AutoRedraw = True ' need this for plot to stick
' Find the min/max value
For lc = 0 To UBound(Values)
If Values(lc) < MinValue Then
MinValue = Values(lc)
End If
If Values(lc) > MaxValue Then
MaxValue = Values(lc) ' knowing the max value may be a help
End If
Next
Dim TopGap As Single
Dim BottomGap As Single
Dim LeftGap As Single
Dim RightGap As Single
Dim AvailableHeight As Single
Dim BaseLine As Single
Dim PlotWidth As Single
' Uplift the max value
MaxValue = MaxValue * 1.1
' Drop the min value
MinValue = MinValue * 0.9
MaxValue = MaxValue - MinValue
Dim X1 As Long
Dim X2 As Long
Dim y1 As Long
Dim y2 As Long
' Set some margins
TopGap = 100
BottomGap = 200
LeftGap = 500
RightGap = 100
AvailableHeight = picGraph.ScaleHeight - TopGap - BottomGap
BaseLine = AvailableHeight + TopGap
PlotWidth = (picGraph.ScaleWidth - LeftGap - RightGap) / (UBound(Values))
' adjust values
For lc = 0 To UBound(Values)
Values(lc) = Values(lc) - MinValue
Next
For lc = 0 To UBound(Values)
X1 = lc * PlotWidth + LeftGap
y1 = BaseLine - (AvailableHeight * Values(lc) / MaxValue)
If lc < UBound(Values) Then
X2 = (lc + 1) * PlotWidth + LeftGap
y2 = BaseLine - (AvailableHeight * Values(lc + 1) / MaxValue)
picGraph.Line (X1, y1)-(X2, y2), RGB(0, 0, 255)
picGraph.Circle (X1, y1), 2, RGB(100, 100, 0)
End If
If mbPrintCaption Then
' print a caption
picGraph.CurrentX = X1 - picGraph.TextWidth(Caption
picGraph.CurrentY = BaseLine + 2
picGraph.Print Captions(lc);
End If
Next lc
picGraph.Circle (X2, y2), 2, RGB(100, 100, 0)
' box round graph
picGraph.Line (LeftGap, TopGap)-Step(picGraph.Scal
Dim hscale As Single
' draw hlines
hscale = MaxValue * 0.1
Do While hscale < MaxValue
y1 = BaseLine - (AvailableHeight * hscale / MaxValue)
picGraph.Line (LeftGap + 1, y1)-(picGraph.ScaleWidth - RightGap - 1, y1), RGB(0, 255, 0)
If mbPrintScale Then
' print a scale
Dim sScale As String
sScale = Format(hscale + MinValue, NumberFormat)
picGraph.CurrentX = LeftGap - picGraph.TextWidth(sScale)
picGraph.CurrentY = y1 - picGraph.TextHeight(sScale
picGraph.Print sScale;
End If
hscale = hscale + MaxValue * 0.1
Loop
End Sub
Private Sub Class_Initialize()
mlValueCount = -1
NumberFormat = "0.00"
mbPrintCaption = True
mbPrintScale = True
mbAutoScale = True
End Sub