Here's good place to start....
Instructions:
Make a large form 800 x 600
Place a large Picture box in it (700 x 500)
Name the picturebox “pb1” – make its backcolor black
(I think you could use a panel here too)
Make a button at the bottom. Name it “cbGo”
Make a second button at the bottom named “cbExit”
I have done my best to deal with continuation lines and the
Distortion of this board but I may have missed.
Play with it J
Imports System.Drawing
Public Class Form1
Protected g As Graphics
Protected a As GraphClass
Protected firsttime As Boolean
Protected Enum FrameCoord
X_Axis_VertLoc = 400
HorizontalDisplacement = 50
Y_Axis_XEnd = 650
Y_Axis_Start = 50
X_Axis_Begin = HorizontalDisplacement
End Enum
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
g = pb1.CreateGraphics()
a = New GraphClass(g)
End Sub
Private Sub cbGo_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cbGo.Click
Static dtNow As DateTime = DateTime.Now
a.SetupFrame()
For i As Short = 0 To 649
a.PlotPoint(i, (Math.Sin(i / 57.3) * 100) + 100)
Next
End Sub
Private Sub cbExit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cbExit.Click
Application.exit
End Sub
Public Class GraphClass
Protected Gain As New Single
Private Intersection As Point = _
New Point(FrameCoord.HorizontalDisplacement, _
FrameCoord.X_Axis_VertLoc)
Protected g As Graphics ' picture box graphic
Protected AxisColor As Color = _
System.Drawing.ColorTranslator.FromOle(&HFF00)
Protected AxisPen As Pen
Public Sub New(ByVal obj As Graphics)
' this graphics is derived from a picture box object in the
‘substantiator
Gain = 1.0
g = obj
AxisPen = New Pen(AxisColor, 2)
End Sub
Public Sub SetupFrame()
Dim pY_Begin As _
New Point(FrameCoord.HorizontalDisplacement, _
FrameCoord.Y_Axis_Start)
Dim pY_End As New Point(FrameCoord.HorizontalDisplacement, _
FrameCoord.X_Axis_VertLoc)
Dim pX_Begin As Point = Intersection
Dim pX_End As New Point(FrameCoord.Y_Axis_XEnd, _
FrameCoord.X_Axis_VertLoc)
' Draw line to screen.
Drawline(AxisPen, pY_Begin, pY_End)
Drawline(AxisPen, pX_Begin, pX_End)
For i As Short = FrameCoord.HorizontalDisplacement + 50 _
To FrameCoord.Y_Axis_XEnd Step 50
Drawline(Pens.BlanchedAlmond, New Point(i, _
FrameCoord.X_Axis_VertLoc - 10), New Point(i, _
FrameCoord.X_Axis_VertLoc))
Next
For i As Short = FrameCoord.X_Axis_VertLoc - 50 To _
FrameCoord.Y_Axis_Start Step -50
Drawline(Pens.BlanchedAlmond, _
New Point(FrameCoord.HorizontalDisplacement, i), _
New Point(FrameCoord.HorizontalDisplacement + 10, i))
Next
End Sub
Public Sub PlotPoint(ByVal X As Integer, _
ByVal Y As Integer, _
Optional ByVal pn As Pen = Nothing)
Dim xp As Integer = (-Y + FrameCoord.X_Axis_VertLoc) * Gain
If pn Is Nothing Then
g.DrawLine(AxisPen, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
Else
g.DrawLine(pn, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
End If
End Sub
Public Sub Drawline(ByVal P As Pen, _
ByVal StartPoint As Point, _
ByVal EndPoint As Point)
If P Is Nothing Then
g.DrawLine(AxisPen, StartPoint, EndPoint)
Else
g.DrawLine(P, StartPoint, EndPoint)
End If
End Sub
End Class
End Class
Thanks lots, it is really a nice example. I saw a graph with x and y axis, with scale and sine wave.
Is there a way to label the scale, and set a timer so that scale is time(second) based.
For example, for a second, the x-axis of the graph would scale from 12:30:02 to 12:30:03, and the next second would be 12:30:03 to 12:30:04 ?
Yes there is. I never got around to it. I'd recommend that you take a look at those drawing classes. It'll be a good task.
I wrote this about a year ago. I imagine the the HorizontalDisplacement is the amount that the entire graph is shifted to the right and X_Axis_Verloc specifies how far down the x axis is. They all have to be positioned.
On a timer...
"a" is a member variable of the main class is accessible anywhere in the main class.
You'll notice that calculation is done in a for loop
For i As Short = 0 To 649
a.PlotPoint(i, (Math.Sin(i / 57.3) * 100) + 100)
Next
The call to a.PlotPoint could easily be made in a timer tick event in the main class. "i" is x coordinate and the Y coordinate is the sine value which could be what ever you are measuring.
Protected Friend withevents tmr as new timer - up with the instantiation of "a".
then make a tick event for it.
It's easy!
Is there any difference between protected and protected friend with events? and protected enum? Sorry i very much confused about them...
i think i now understand what does every part do for the graph, only this following section that im a bit confused about, i hope that you can give me some brief or rough explanation regarding it, thanks a lot..
Public Sub PlotPoint(ByVal X As Integer, _
ByVal Y As Integer, _
Optional ByVal pn As Pen = Nothing)
Dim xp As Integer = (-Y + FrameCoord.X_Axis_VertLoc) * Gain
If pn Is Nothing Then
g.DrawLine(AxisPen, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
Else
g.DrawLine(pn, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
End If
End Sub
Protected, like Private and Public are ways to control who can reference a given variable or class, property, method procedure or object.
I posted it wrongly, so i post this again:
i think i now understand what does every part do for the graph, only this following section that im a bit confused about, i hope that you can give me some brief or rough explanation regarding it, thanks a lot..
Public Sub PlotPoint(ByVal X As Integer, _
ByVal Y As Integer, _
Optional ByVal pn As Pen = Nothing)
Dim xp As Integer = (-Y + FrameCoord.X_Axis_VertLoc) * Gain
If pn Is Nothing Then
g.DrawLine(AxisPen, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
Else
g.DrawLine(pn, New Point(X + Intersection.X, xp), _
New Point(X + 1 + Intersection.X, xp + 1))
End If
End Sub
It's going to require a lot more to describe the code than to write.
Gain was a future feature and can be removed. A pen is what you draw with. If you supply a point pen it will draw with it, otherwise it assumes you are drawing an axis and uses the axis pen. But to attentuate an Y-value to 80 percent of full scale one would set the gain to .8 .
The new points are start and stop coordinates for drawing. On the x-axis every is referenced in the X direction to the coordinates of where the x and y axes intersect so, Intersection.X can be thought of a the location when x =0 so the x location of a point is x + intersection.x +1.
I'm having to really dig in my memory and remember how the value, xp is calculated. I think I took some advantages of some coincidences meaning that the name FrameCoord.X_Axis_VertLoc may not be the best name but it was the value I needed. Essentially FrameCoord.X_Axis_VertLoc describes where 0 is for Y and that may be from the of the axis which MIGHT explain why Y is made a negative.
Im trying to play around with the timer, and wanted to plot a point from 0 to 649 with one second each but it doesnt work.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickTimer1.Interval = 1000
For i As Short = 0 To 649a.PlotPoint(i, (Math.Sin(i / 57.3) * 100) + 100)
NextEnd SubWith the help at a vb forum, ive figured out that this is how it works. The graph plotting example is very helpful to me by the way.
Private Sub cbGo_Click(ByVal sender As Object, _ Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ByVal e As System.EventArgs) Handles cbGo.Click
a.SetupFrame()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Static i As Integer = 0
i += 1
a.PlotPoint(i, (Math.Sin(i / 57.3) * 100) + 100)
If i = 649 Then
Timer1.Enabled = False
End If
End