Tips kali ini membuat Grafik Plotting Data Random Real time. Tampilan visualnya seperti gambar di atas.
Ikuti langkah-langkah di bawah ini. ToolBox yang digunakan sebagai berikut:
Design Form dari ToolBox di atas seperti gambar di bawah ini:
Ketikkan source program di bawah ini dengan mengarahkan kursor pada design Form, Klik kanan lalu pilih View Code.
Source Program:
Source Program:
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim pointarray As Point() = New Point(4) {}
REM data Random di batasi dengan nilai maksimum 100
Dim r As Random = New Random()
Me.Value1.Text = r.Next(100).ToString()
Me.Value2.Text = r.Next(100).ToString()
Me.Value3.Text = r.Next(100).ToString()
Me.Value4.Text = r.Next(100).ToString()
Me.Value5.Text = r.Next(100).ToString()
REM Penskalaan Plot Point Data Random pada grafik
REM Misalkan Nilai Maksimum Random = 100
REM a = 100
REM a = 100 * 4 = 400
REM a = 400/2 = 200
REM a = 200 - 200 = 0 ini mengindikasikan Nilai a di plot pada nilai titik puncak
REM begitu nilai selanjutnya
Dim a As Single = Convert.ToInt32(Me.Value1.Text.ToString())
a = a * 4
a = a / 2
a = 200 - a
Dim b As Single = Convert.ToInt32(Me.Value2.Text.ToString())
b = b * 4
b = b / 2
b = 200 - b
Dim c As Single = Convert.ToInt32(Me.Value3.Text.ToString())
c = c * 4
c = c / 2
c = 200 - c
Dim d As Single = Convert.ToInt32(Me.Value4.Text.ToString())
d = d * 4
d = d / 2
d = 200 - d
Dim f As Single = Convert.ToInt32(Me.Value5.Text.ToString())
f = f * 4
f = f / 2
f = 200 - f
Dim newpen As Pen = New Pen(Color.Red)
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
REM posisi set point pada grafik terletak pada titik (100,100)
g.TranslateTransform(100, 100)
g.DrawLine(newpen, 1, 1, 1, 200)
g.DrawLine(newpen, 1, 200, 200, 200)
REM menggambar garis X dan Y axis
g.DrawLine(newpen, -3, 0, 3, 0)
g.DrawLine(newpen, -3, 40, 3, 40)
g.DrawLine(newpen, -3, 80, 3, 80)
g.DrawLine(newpen, -3, 120, 3, 120)
g.DrawLine(newpen, -3, 160, 3, 160)
g.DrawLine(newpen, 40, 197, 40, 203)
g.DrawLine(newpen, 80, 197, 80, 203)
g.DrawLine(newpen, 120, 197, 120, 203)
g.DrawLine(newpen, 160, 197, 160, 203)
g.DrawLine(newpen, 200, 197, 200, 203)
Dim newfont As Font = New Font("Arial", 10)
REM Label Y axis
g.DrawString("100", newfont, New SolidBrush(Color.Green), -30, -10)
g.DrawString("80", newfont, New SolidBrush(Color.Green), -25, 30)
g.DrawString("60", newfont, New SolidBrush(Color.Green), -25, 70)
g.DrawString("40", newfont, New SolidBrush(Color.Green), -25, 110)
g.DrawString("20", newfont, New SolidBrush(Color.Green), -25, 150)
REM Label X axis
g.DrawString("0", newfont, New SolidBrush(Color.Green), -18, 190)
g.DrawString("Rnd1", newfont, New SolidBrush(Color.Green), 20, 210)
g.DrawString("Rnd2", newfont, New SolidBrush(Color.Green), 60, 210)
g.DrawString("Rnd3", newfont, New SolidBrush(Color.Green), 100, 210)
g.DrawString("Rnd4", newfont, New SolidBrush(Color.Green), 140, 210)
g.DrawString("Rnd5", newfont, New SolidBrush(Color.Green), 180, 210)
g.DrawString("V. Hutabalian's Blog", newfont, New SolidBrush(Color.Yellow), 25, -90)
g.DrawString("Rnd1", newfont, New SolidBrush(Color.Green), -42, -40)
g.DrawString("Rnd2", newfont, New SolidBrush(Color.Green), 12, -40)
g.DrawString("Rnd3", newfont, New SolidBrush(Color.Green), 62, -40)
g.DrawString("Rnd4", newfont, New SolidBrush(Color.Green), 120, -40)
g.DrawString("Rnd5", newfont, New SolidBrush(Color.Green), 172, -40)
REM Penanda Plot Points
g.DrawEllipse(New Pen(Color.Blue), 40 - 3, a - 3, 6, 6)
g.DrawEllipse(New Pen(Color.Red), 80 - 3, b - 3, 6, 6)
g.DrawEllipse(New Pen(Color.Purple), 120 - 3, c - 3, 6, 6)
g.DrawEllipse(New Pen(Color.Pink), 160 - 3, d - 3, 6, 6)
g.DrawEllipse(New Pen(Color.Gold), 200 - 3, f - 3, 6, 6)
REM convert points
pointarray(0) = New Point(40, Convert.ToInt32(a))
pointarray(1) = New Point(80, Convert.ToInt32(b))
pointarray(2) = New Point(120, Convert.ToInt32(c))
pointarray(3) = New Point(160, Convert.ToInt32(d))
pointarray(4) = New Point(200, Convert.ToInt32(f))
g.DrawLines(New Pen(Color.GreenYellow), pointarray)
End Sub
Private Sub btnStar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStar.Click
If timerStart.Enabled = False Then
Me.timerStart.Start()
timerStart.Enabled = True
btnStar.Text = "&Stop"
Else
Me.timerStart.Stop()
timerStart.Enabled = False
btnStar.Text = "&Star"
End If
End Sub
Private Sub timerStart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerStart.Tick
Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.BackColor = Color.Black
End Sub
End Class
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini. Tekan tombol Start maka tampilan visualnya seperti gambar Paling atas.
Artikel Yang berkaitan tentang Dynamic Line Klik di bawah ini:
Chart CPU, Memory, Processes VB 2008
Dynamic Line Visual Basic 2008
Grafik Dynamic Line Visual Basic 2008
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar