Tips kali ini membuat Grafik Plotting Data CPU Usage Real time. Tampilan visualnya seperti gambar di atas.
Ikuti langkah-langkah di bawah ini. ToolBox yang digunakan sebagai berikut:
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini, kemudian tekan Start tampilannya seperti gambar paling atas.
Artikel terkait mengenai Tips ini:
Grafik 2D PictureBox V Basic 2008 ===>>Klik disini
Dynamic Line Plotting Data Random ===>>Klik disini
Chart CPU, Memory, Processes VB 2008 ===>>Klik disini
ThreadGraph Dynamic Line Visual Basic 2008 ===>>Klik disini
Graphic Dynamic Line VB 2008 ===>>Klik disini
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
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:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Imports System.Threading
Public Class Form1
Dim XMove As Integer = 4
Private m_Ymid As Integer
Private m_Y As Integer
Private new_y As Single = 0
Private old_y As Single = 0
Private Const GRID_STEP As Integer = 20
Private m_GraphThread As Thread
Dim count As Integer = 0
Dim count1 As Integer = 0
Dim maksGraph As Single = 100
Dim minGraphs As Single = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_Ymid = picGraph.Height
m_Y = m_Ymid
REM membuat objek grafik
Dim wid As Integer = picGraph.Width
Dim hgt As Integer = picGraph.Height
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
REM menggambar/ membentuk pola garis
gr.Clear(Color.Black)
For i As Integer = m_Ymid To picGraph.Height Step GRID_STEP
gr.DrawLine(Pens.Green, 0, i, wid, i)
Next i
For i As Integer = m_Ymid To 0 Step -GRID_STEP
gr.DrawLine(Pens.Green, 0, i, wid, i)
Next i
picGraph.Image = bm
End Sub
Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click
If m_GraphThread Is Nothing Then
REM Keadaan fungsi/ prosedur thread. Indikasi ketika di mulai
AddStatus("Starting V. Hutabalian'S Blog")
m_GraphThread = New Thread(AddressOf DrawGraph)
m_GraphThread.Priority = ThreadPriority.BelowNormal
m_GraphThread.IsBackground = True
m_GraphThread.Start()
AddStatus("Dynamic Line Indikator CPU Usage Start")
btnGraph.Text = "Stop"
Timer1.Enabled = True
Else
REM fungsi/Prosedur thread pada saat berjalan kemudian berhenti diindikasikan
AddStatus("Stopping V. Hutabalian'S Blog")
m_GraphThread.Abort()
m_GraphThread = Nothing
Timer1.Enabled = False
AddStatus("Dynamic Line Indikator CPU Usage stopped ")
btnGraph.Text = "Start"
End If
End Sub
Private Sub DrawGraph()
Try
Do
NewValue()
REM plot nilai random
PlotValue(count, m_Y)
count = m_Y
Loop
Catch ex As Exception
AddStatus("[Thread] " & ex.Message)
End Try
End Sub
Private Delegate Sub PlotValueDelegate(ByVal old_y As Integer, ByVal new_y As Integer)
Private Sub PlotValue(ByVal old_y As Integer, ByVal new_y As Integer)
REM tampilan grafik garis data random pada prosedur thread
If Me.InvokeRequired Then
REM tampilan plot nilai random lama dan baru
Dim args As Object() = {-old_y, -new_y}
Dim plot_value_delegate As PlotValueDelegate
plot_value_delegate = AddressOf PlotValue
Me.Invoke(plot_value_delegate, args)
Exit Sub
End If
REM tampilkan bentuk grafik
Dim wid As Integer = picGraph.Width
Dim hgt As Integer = picGraph.Height
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
If Not IsNothing(picGraph.Image) Then
gr.DrawImage(picGraph.Image, -XMove, 0)
End If
End Sub
Private Sub NewValue()
REM berikan jedah waktu setiap pengambilan nilai
Dim stop_time As Date = Now.AddMilliseconds(100)
Do While Now < m_y =" count" count =" 0">= picGraph.Height - 1 Then count = picGraph.Height - 1
End Sub
REM berikan konfirmasi
Private Delegate Sub AddStatusDelegate(ByVal txt As String)
Private Sub AddStatus(ByVal txt As String)
If Me.InvokeRequired Then
Dim args As Object() = {txt}
Dim add_status_delegate As AddStatusDelegate
add_status_delegate = AddressOf AddStatus
Me.Invoke(add_status_delegate, args)
Exit Sub
End If
txtStatus.Text &= vbCrLf & txt
txtStatus.Select(txtStatus.Text.Length, 0)
txtStatus.ScrollToCaret()
End Sub
Private Sub tmrUpdateClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUpdateClock.Tick
lblTime.Text = Now.ToString("T")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
picGraph.Image = DisplayGuidelinesAndChart(picGraph, m_Y, XMove, Me.count, minGraphs, maksGraph)
Dim sngCPU As Single = PeformCPU.NextValue() / 100
Label1.Text = "CPU: " & sngCPU.ToString("p")
count = sngCPU * 100
End Sub
Private Function DisplayGuidelinesAndChart(ByVal PicBox As PictureBox, ByVal chunks As Integer, _
ByVal XMove As Integer, ByVal New_y 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
For i As Integer = m_Ymid To picGraph.Height Step GRID_STEP
gr.DrawLine(Pens.Green, 0, i, picGraph.Width, i)
Next i
For i As Integer = m_Ymid To 0 Step -GRID_STEP
gr.DrawLine(Pens.Green, 0, i, picGraph.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
New_y *= 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, -old_y, PicBox.Width - 1, -new_y)
old_y = New_y
Return bm
gr.Dispose()
End Function
Private Sub picGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picGraph.Paint
Dim LeftMargin As Integer = 5
Dim RightMargin As Integer = 5
Dim BaseMargin As Integer = 2
Dim TopMargin As Integer = 2
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End
End Sub
End Class
Source Program:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Imports System.Threading
Public Class Form1
Dim XMove As Integer = 4
Private m_Ymid As Integer
Private m_Y As Integer
Private new_y As Single = 0
Private old_y As Single = 0
Private Const GRID_STEP As Integer = 20
Private m_GraphThread As Thread
Dim count As Integer = 0
Dim count1 As Integer = 0
Dim maksGraph As Single = 100
Dim minGraphs As Single = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_Ymid = picGraph.Height
m_Y = m_Ymid
REM membuat objek grafik
Dim wid As Integer = picGraph.Width
Dim hgt As Integer = picGraph.Height
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
REM menggambar/ membentuk pola garis
gr.Clear(Color.Black)
For i As Integer = m_Ymid To picGraph.Height Step GRID_STEP
gr.DrawLine(Pens.Green, 0, i, wid, i)
Next i
For i As Integer = m_Ymid To 0 Step -GRID_STEP
gr.DrawLine(Pens.Green, 0, i, wid, i)
Next i
picGraph.Image = bm
End Sub
Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click
If m_GraphThread Is Nothing Then
REM Keadaan fungsi/ prosedur thread. Indikasi ketika di mulai
AddStatus("Starting V. Hutabalian'S Blog")
m_GraphThread = New Thread(AddressOf DrawGraph)
m_GraphThread.Priority = ThreadPriority.BelowNormal
m_GraphThread.IsBackground = True
m_GraphThread.Start()
AddStatus("Dynamic Line Indikator CPU Usage Start")
btnGraph.Text = "Stop"
Timer1.Enabled = True
Else
REM fungsi/Prosedur thread pada saat berjalan kemudian berhenti diindikasikan
AddStatus("Stopping V. Hutabalian'S Blog")
m_GraphThread.Abort()
m_GraphThread = Nothing
Timer1.Enabled = False
AddStatus("Dynamic Line Indikator CPU Usage stopped ")
btnGraph.Text = "Start"
End If
End Sub
Private Sub DrawGraph()
Try
Do
NewValue()
REM plot nilai random
PlotValue(count, m_Y)
count = m_Y
Loop
Catch ex As Exception
AddStatus("[Thread] " & ex.Message)
End Try
End Sub
Private Delegate Sub PlotValueDelegate(ByVal old_y As Integer, ByVal new_y As Integer)
Private Sub PlotValue(ByVal old_y As Integer, ByVal new_y As Integer)
REM tampilan grafik garis data random pada prosedur thread
If Me.InvokeRequired Then
REM tampilan plot nilai random lama dan baru
Dim args As Object() = {-old_y, -new_y}
Dim plot_value_delegate As PlotValueDelegate
plot_value_delegate = AddressOf PlotValue
Me.Invoke(plot_value_delegate, args)
Exit Sub
End If
REM tampilkan bentuk grafik
Dim wid As Integer = picGraph.Width
Dim hgt As Integer = picGraph.Height
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
If Not IsNothing(picGraph.Image) Then
gr.DrawImage(picGraph.Image, -XMove, 0)
End If
End Sub
Private Sub NewValue()
REM berikan jedah waktu setiap pengambilan nilai
Dim stop_time As Date = Now.AddMilliseconds(100)
Do While Now < m_y =" count" count =" 0">= picGraph.Height - 1 Then count = picGraph.Height - 1
End Sub
REM berikan konfirmasi
Private Delegate Sub AddStatusDelegate(ByVal txt As String)
Private Sub AddStatus(ByVal txt As String)
If Me.InvokeRequired Then
Dim args As Object() = {txt}
Dim add_status_delegate As AddStatusDelegate
add_status_delegate = AddressOf AddStatus
Me.Invoke(add_status_delegate, args)
Exit Sub
End If
txtStatus.Text &= vbCrLf & txt
txtStatus.Select(txtStatus.Text.Length, 0)
txtStatus.ScrollToCaret()
End Sub
Private Sub tmrUpdateClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUpdateClock.Tick
lblTime.Text = Now.ToString("T")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
picGraph.Image = DisplayGuidelinesAndChart(picGraph, m_Y, XMove, Me.count, minGraphs, maksGraph)
Dim sngCPU As Single = PeformCPU.NextValue() / 100
Label1.Text = "CPU: " & sngCPU.ToString("p")
count = sngCPU * 100
End Sub
Private Function DisplayGuidelinesAndChart(ByVal PicBox As PictureBox, ByVal chunks As Integer, _
ByVal XMove As Integer, ByVal New_y 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
For i As Integer = m_Ymid To picGraph.Height Step GRID_STEP
gr.DrawLine(Pens.Green, 0, i, picGraph.Width, i)
Next i
For i As Integer = m_Ymid To 0 Step -GRID_STEP
gr.DrawLine(Pens.Green, 0, i, picGraph.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
New_y *= 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, -old_y, PicBox.Width - 1, -new_y)
old_y = New_y
Return bm
gr.Dispose()
End Function
Private Sub picGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picGraph.Paint
Dim LeftMargin As Integer = 5
Dim RightMargin As Integer = 5
Dim BaseMargin As Integer = 2
Dim TopMargin As Integer = 2
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End
End Sub
End Class
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini, kemudian tekan Start tampilannya seperti gambar paling atas.
Artikel terkait mengenai Tips ini:
Grafik 2D PictureBox V Basic 2008 ===>>Klik disini
Dynamic Line Plotting Data Random ===>>Klik disini
Chart CPU, Memory, Processes VB 2008 ===>>Klik disini
ThreadGraph Dynamic Line Visual Basic 2008 ===>>Klik disini
Graphic Dynamic Line VB 2008 ===>>Klik disini
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar