Tips kali ini kita akan membuat Visual Graphic Dynamic Line Chart dengan menggunakan Visual Basic 2008.Hasilnya seperti gambar di atas.
ToolBox yang digunakan sebagai berikut:
Rancanglah ToolBox di atas, seperti gambar di bawah ini:
Ketiklah source program di bawah ini, dengan mengarahkan kursor pada design, mengklik kanan lalu pilih View Code.
Source Program:
Public Class Form1
Private OldValue As Single = 0
Private NewValue As Single = 0
Dim XMove As Integer = 4
Dim Chunks As Integer = 10
Dim maksGraph As Single = 100
Dim minGraphs As Single = 0
Dim LeftMargin As Integer = 5
Dim RightMargin As Integer = 5
Dim BaseMargin As Integer = 2
Dim TopMargin As Integer = 2
Private Function DisplayGuidelines(ByVal PicBox As PictureBox, ByVal chunks As Integer) As Bitmap
Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
Dim total As Integer = PicBox.Height
Dim chunk As Single = total / chunks
For i As Single = chunk To total Step chunk
gr.DrawLine(Pens.Green, 0, i, PicBox.Width, i)
Next i
Return bm
gr.Dispose()
End Function
Private Sub More_Generic_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim picValues As New PictureBox
picGraph.Image = DisplayGuidelines(picGraph, Chunks)
picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, SBUserValue.Minimum, SBUserValue.Maximum)
End Sub
Private Function DisplayVerticalValues(ByVal PB As PictureBox, ByVal HowManyChunks As Single, _
ByVal MinValue As Single, ByVal MaxValue As Single) As Bitmap
Dim bmp As New Bitmap(PB.Width, PB.Height)
Dim gv As Graphics = Graphics.FromImage(bmp)
Dim TotalPixels As Integer = PB.Height
Dim SingleChunk As Single = TotalPixels / HowManyChunks
For i As Single = SingleChunk To TotalPixels Step SingleChunk
gv.DrawLine(Pens.Green, 0, i, PB.Width, i)
Next i
Dim NextMarker As Integer = MaxValue
Dim ValueRange As Integer = MaxValue - MinValue
For i As Single = 0 To TotalPixels Step SingleChunk
gv.DrawString(CStr(NextMarker), New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, 1, i)
NextMarker -= (ValueRange / HowManyChunks)
Next
Return bmp
gv.Dispose()
End Function
Private Function DisplayGuidelinesAndChart(ByVal PicBox As PictureBox, ByVal chunks As Integer, _
ByVal XMove As Integer, ByVal NewValue As Single, ByVal Min As Single, ByVal Max As Single) As Bitmap
Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
Dim total As Integer = PicBox.Height
Dim chunk As Single = total / chunks
For i As Single = chunk To total Step chunk
gr.DrawLine(Pens.Green, PicBox.Width - XMove, i, PicBox.Width, i)
Next i
If Not IsNothing(PicBox.Image) Then
gr.DrawImage(PicBox.Image, -XMove, 0)
End If
Dim ValueRange As Single = Max - Min
Dim vScale As Single = PicBox.Height / ValueRange
NewValue *= vScale
gr.TranslateTransform(0, PicBox.Height)
If Min > 0 Then gr.TranslateTransform(0, Min * vScale)
Dim p As Pen = New Pen(Color.Red , 2)
gr.DrawLine(p, PicBox.Width - 1 - XMove, -OldValue, PicBox.Width - 1, -NewValue)
OldValue = NewValue
Return bm
gr.Dispose()
End Function
Private Sub _3rd_More_Generic_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Dim picValues As New PictureBox
picGraph.Image = DisplayGuidelines(picGraph, Chunks)
picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, SBUserValue.Minimum, SBUserValue.Maximum)
End Sub
Private Sub SBUserValue_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SBUserValue.ValueChanged
picGraph.Image = DisplayGuidelinesAndChart(picGraph, Chunks, XMove, Me.SBUserValue.Value, SBUserValue.Minimum, SBUserValue.Maximum)
lblValue.Text = SBUserValue.Value.ToString
End Sub
Private Sub picGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picGraph.Paint
Dim ix As Integer = picGraph.Width
Dim iy As Integer = picGraph.Height
Dim StartPoint As New Point(LeftMargin, picGraph.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Red, 2)
Dim VertLineLength As Integer = picGraph.Height - (TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim TickSP As New Point(LeftMargin, StartPoint.Y - VertGap)
Dim TickEP As New Point(LeftMargin, StartPoint.Y - VertGap)
Dim ValueFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To 10
e.Graphics.DrawString(CStr(i * 10), ValueFont, Brushes.Yellow, 2, TickSP.Y + 1)
TickSP.Y -= VertGap
TickEP.Y -= VertGap
Next
End Sub
End Class
Setelah anda mengetik listing dari source program diatas tekan F5, maka akan tampil gambar di bawah ini:
0 komentar:
Posting Komentar