Tips kali ini membuat Grafik Plotting Data dari Acces 2007 di visualisasikan menggunakan PictureBox. 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:
Imports System.Data.OleDb
Imports System.Drawing.Drawing2D
Public Class Form1
REM Margin/tepi grafik bagian sisi atas, kiri, kanan dan bawah
Dim LeftMargin As Integer = 35
Dim RightMargin As Integer = 15
Dim BaseMargin As Integer = 35
Dim TopMargin As Integer = 10
REM panjang grafik vertical axis
Dim VertLineLength As Integer
REM panjang grafik horizontal axis
Dim BaseLineLength As Integer
REM rentang lebar plotting data
Dim LineWidth As Double
Dim g As Graphics
Dim bmap As Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
REM warna background grafik
PictureBox1.BackColor = Color.Black
REM menampilkan data base
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Data_Base2007.accdb;Persist Security Info=False;")
REM untuk koneksi access 2003 dan access 2000 gunakan listing di bawah ini
REM Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Data_Base2007.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
Private Sub DrawHorizontalLines()
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim StartPoint As New Point(LeftMargin + 3, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(PictureBox1.Width, PictureBox1.Height - BaseMargin)
Dim LineStart As New Point(StartPoint.X, StartPoint.Y - VertGap)
Dim LineEnd As New Point(EndPoint.X, StartPoint.Y - VertGap)
Dim ThinPen As New Pen(Color.Green, 2)
For i As Integer = 1 To 10
g.DrawLine(ThinPen, LineStart, LineEnd)
LineStart.Y -= VertGap
LineEnd.Y -= VertGap
Next
ThinPen.Dispose()
End Sub
Private Sub FinalDisplay()
PictureBox1.Image = bmap
g.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DrawOutline()
DrawHorizontalLines()
DrawVerticalGridLines()
DrawTheLine()
ShowGridData()
FinalDisplay()
End Sub
Private Sub DrawOutline()
bmap = New Bitmap(PictureBox1.Width, PictureBox1.Height, PictureBox1.CreateGraphics)
g = Graphics.FromImage(bmap)
Dim StartPoint As New Point(LeftMargin, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Red, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim TickSP As New Point(LeftMargin - 5, 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
g.DrawLine(New Pen(Color.Black), TickSP, TickEP)
g.DrawString(CStr(i * 10), ValueFont, Brushes.Red, 2, TickSP.Y - 5)
TickSP.Y -= VertGap
TickEP.Y -= VertGap
Next
g.DrawLine(LinePen, LeftMargin, PictureBox1.Height - BaseMargin, PictureBox1.Width - RightMargin, PictureBox1.Height - BaseMargin)
End Sub
Private Sub DrawTheLine()
Dim MyPath As New GraphicsPath
Dim MyPen As Pen = New Pen(Color.Blue, 2)
g.DrawPath(MyPen, MyPath)
Dim VertScale As Double
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim ix As Integer = PictureBox1.Width
Dim iy As Integer = PictureBox1.Height
Dim Values(ix) As Double
Dim XPosStart As Integer = CInt(LeftMargin + 30)
VertScale = VertLineLength / 100
Dim XPosEnd As Integer = CInt(XPosStart + LineWidth)
Dim YPosStart As Integer = CInt(DataGridView1.Item(0, 0).Value * VertScale)
Dim YPosEnd As Integer = CInt(DataGridView1.Item(0, 1).Value * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
For i As Integer = 1 To 10 - 1
XPosStart = XPosEnd
XPosEnd = CInt(XPosStart + LineWidth)
YPosStart = YPosEnd
YPosEnd = CInt((DataGridView1.Item(0, i + 1).Value) * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
Next
MyPath.AddEllipse(XPosEnd - 2, YPosEnd - 2, 4, 4)
g.RotateTransform(180)
g.ScaleTransform(-1, 1)
g.TranslateTransform(0, VertLineLength + 10, MatrixOrder.Append)
g.DrawPath(MyPen, MyPath)
g.ResetTransform()
End Sub
Private Sub ShowGridData()
Dim TextStartX As Integer = CInt(LeftMargin + 18)
Dim TextBrsh As Brush = New SolidBrush(Color.Red)
Dim TextFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To 11
g.DrawString(i, TextFont, TextBrsh, TextStartX, _
CInt(PictureBox1.Height - (BaseMargin - 4)))
TextStartX += CInt(LineWidth)
Next
End Sub
Private Sub DrawVerticalGridLines()
Dim ThinPen As New Pen(Color.Green, 2)
Dim StartPoint As New Point(LeftMargin, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Red, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
BaseLineLength = PictureBox1.Width - (LeftMargin + RightMargin)
LineWidth = (BaseLineLength / 11)
Dim LineStartX As Integer = CInt(LeftMargin + 30)
For i As Integer = 0 To 10
g.DrawLine(ThinPen, LineStartX, TopMargin, LineStartX, PictureBox1.Height - (BaseMargin + 4))
LineStartX += CInt(LineWidth)
Next
ThinPen.Dispose()
End Sub
End Class
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini, kemudian tekan Tampilkan Grafik Data 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
Indikator Graph CPU Usage History ===>>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.Data.OleDb
Imports System.Drawing.Drawing2D
Public Class Form1
REM Margin/tepi grafik bagian sisi atas, kiri, kanan dan bawah
Dim LeftMargin As Integer = 35
Dim RightMargin As Integer = 15
Dim BaseMargin As Integer = 35
Dim TopMargin As Integer = 10
REM panjang grafik vertical axis
Dim VertLineLength As Integer
REM panjang grafik horizontal axis
Dim BaseLineLength As Integer
REM rentang lebar plotting data
Dim LineWidth As Double
Dim g As Graphics
Dim bmap As Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
REM warna background grafik
PictureBox1.BackColor = Color.Black
REM menampilkan data base
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Data_Base2007.accdb;Persist Security Info=False;")
REM untuk koneksi access 2003 dan access 2000 gunakan listing di bawah ini
REM Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Data_Base2007.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
Private Sub DrawHorizontalLines()
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim StartPoint As New Point(LeftMargin + 3, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(PictureBox1.Width, PictureBox1.Height - BaseMargin)
Dim LineStart As New Point(StartPoint.X, StartPoint.Y - VertGap)
Dim LineEnd As New Point(EndPoint.X, StartPoint.Y - VertGap)
Dim ThinPen As New Pen(Color.Green, 2)
For i As Integer = 1 To 10
g.DrawLine(ThinPen, LineStart, LineEnd)
LineStart.Y -= VertGap
LineEnd.Y -= VertGap
Next
ThinPen.Dispose()
End Sub
Private Sub FinalDisplay()
PictureBox1.Image = bmap
g.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DrawOutline()
DrawHorizontalLines()
DrawVerticalGridLines()
DrawTheLine()
ShowGridData()
FinalDisplay()
End Sub
Private Sub DrawOutline()
bmap = New Bitmap(PictureBox1.Width, PictureBox1.Height, PictureBox1.CreateGraphics)
g = Graphics.FromImage(bmap)
Dim StartPoint As New Point(LeftMargin, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Red, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim TickSP As New Point(LeftMargin - 5, 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
g.DrawLine(New Pen(Color.Black), TickSP, TickEP)
g.DrawString(CStr(i * 10), ValueFont, Brushes.Red, 2, TickSP.Y - 5)
TickSP.Y -= VertGap
TickEP.Y -= VertGap
Next
g.DrawLine(LinePen, LeftMargin, PictureBox1.Height - BaseMargin, PictureBox1.Width - RightMargin, PictureBox1.Height - BaseMargin)
End Sub
Private Sub DrawTheLine()
Dim MyPath As New GraphicsPath
Dim MyPen As Pen = New Pen(Color.Blue, 2)
g.DrawPath(MyPen, MyPath)
Dim VertScale As Double
Dim VertLineLength As Integer = PictureBox1.Height - (BaseMargin + TopMargin)
Dim ix As Integer = PictureBox1.Width
Dim iy As Integer = PictureBox1.Height
Dim Values(ix) As Double
Dim XPosStart As Integer = CInt(LeftMargin + 30)
VertScale = VertLineLength / 100
Dim XPosEnd As Integer = CInt(XPosStart + LineWidth)
Dim YPosStart As Integer = CInt(DataGridView1.Item(0, 0).Value * VertScale)
Dim YPosEnd As Integer = CInt(DataGridView1.Item(0, 1).Value * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
For i As Integer = 1 To 10 - 1
XPosStart = XPosEnd
XPosEnd = CInt(XPosStart + LineWidth)
YPosStart = YPosEnd
YPosEnd = CInt((DataGridView1.Item(0, i + 1).Value) * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
Next
MyPath.AddEllipse(XPosEnd - 2, YPosEnd - 2, 4, 4)
g.RotateTransform(180)
g.ScaleTransform(-1, 1)
g.TranslateTransform(0, VertLineLength + 10, MatrixOrder.Append)
g.DrawPath(MyPen, MyPath)
g.ResetTransform()
End Sub
Private Sub ShowGridData()
Dim TextStartX As Integer = CInt(LeftMargin + 18)
Dim TextBrsh As Brush = New SolidBrush(Color.Red)
Dim TextFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To 11
g.DrawString(i, TextFont, TextBrsh, TextStartX, _
CInt(PictureBox1.Height - (BaseMargin - 4)))
TextStartX += CInt(LineWidth)
Next
End Sub
Private Sub DrawVerticalGridLines()
Dim ThinPen As New Pen(Color.Green, 2)
Dim StartPoint As New Point(LeftMargin, PictureBox1.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Red, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
BaseLineLength = PictureBox1.Width - (LeftMargin + RightMargin)
LineWidth = (BaseLineLength / 11)
Dim LineStartX As Integer = CInt(LeftMargin + 30)
For i As Integer = 0 To 10
g.DrawLine(ThinPen, LineStartX, TopMargin, LineStartX, PictureBox1.Height - (BaseMargin + 4))
LineStartX += CInt(LineWidth)
Next
ThinPen.Dispose()
End Sub
End Class
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini, kemudian tekan Tampilkan Grafik Data 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
Indikator Graph CPU Usage History ===>>Klik disini
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar