Tips kali ini kita akan membuat visual Grafik Bar 2D dengan menggunakan Visual Basic 2008. Ikuti langkah-langkah di bawah ini, hasil programnya seperti gambar di bawah ini.
ToolBox yang digunakan sebagai berikut:
Rangcanglah ToolBox di bawah ini seperti gambar di bawah ini:
Ketiklah source program di bawah ini, dengan mengarahkan kursor pada design, mengklik kanan lalu pilih View Code.
Source Program:
Option Strict On
Imports System.Drawing.Drawing2D
Imports System.Collections
Structure GraphData
Dim Country As String
Dim Sales As Short
Sub New ByVal country As String, ByVal sales As Short
Me.Country = country
Me.Sales = sales
End Sub
End Structure
Public Class Form1
Dim SalesData As New ArrayList
Dim LeftMargin As Integer = 35
Dim RightMargin As Integer = 15
Dim BaseMargin As Integer = 35
Dim TopMargin As Integer = 10
Dim BarGap As Integer = 12
Private Sub GetData()
SalesData.Clear()
SalesData.Add(New GraphData("Jakarta", 834))
SalesData.Add(New GraphData("Bandung", 385))
SalesData.Add(New GraphData("Medan", 672))
SalesData.Add(New GraphData("Batam", 429))
SalesData.Add(New GraphData("P.Siantar", 715))
SalesData.Add(New GraphData("Solo", 942))
End Sub
Private Sub DrawChart()
Dim g As Graphics
Dim bmap As Bitmap
bmap = New Bitmap(PBBarChart.Width, PBBarChart.Height, _
PBBarChart.CreateGraphics)
g = Graphics.FromImage(bmap)
Dim StartPoint As New Point(LeftMargin, PBBarChart.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Green, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
Dim VertLineLength As Integer = PBBarChart.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("Courier New", 9, FontStyle.Regular)
For i As Integer = 1 To 10
g.DrawLine(New Pen(Color.Green), TickSP, TickEP)
g.DrawString(CStr(i * 100), ValueFont, Brushes.Green, 2, TickSP.Y - 5)
TickSP.Y -= VertGap
TickEP.Y -= VertGap
Next
g.DrawLine(LinePen, LeftMargin, PBBarChart.Height - BaseMargin, _
PBBarChart.Width - RightMargin, PBBarChart.Height - BaseMargin)
Dim BaseLineLength As Integer = _
PBBarChart.Width - (LeftMargin + RightMargin)
Dim BarWidth As Double = (BaseLineLength / SalesData.Count) - BarGap
Dim BarRect As Rectangle
Dim BarStartX As Integer = LeftMargin + BarGap
Dim BarStartY As Integer = PBBarChart.Height - (BaseMargin + 1)
Dim BarBrush As New SolidBrush(Color.Gold)
Dim VertScale As Double
VertScale = VertLineLength / 1000
For Each gd As GraphData In SalesData
Dim BarHeight As Integer = CInt(gd.Sales * VertScale)
BarRect = New Rectangle(BarStartX, BarStartY, CInt(BarWidth), _
BarHeight)
BarRect.Offset(0, -BarHeight)
g.FillRectangle(BarBrush, BarRect)
g.DrawRectangle(LinePen, BarRect)
BarStartX += CInt(BarWidth + BarGap)
Next
Dim TextStartX As Integer = LeftMargin + BarGap + 4
Dim TextBrsh As Brush = New SolidBrush(Color.GreenYellow)
Dim TextFont As New Font("Courier New ", 11, FontStyle.Regular)
For Each gd As GraphData In SalesData
g.DrawString(gd.Country, TextFont, TextBrsh, TextStartX, _
CInt(PBBarChart.Height - (BaseMargin - 4)))
TextStartX += CInt(BarWidth + BarGap)
Next
PBBarChart.Image = bmap
g.Dispose()
LinePen.Dispose()
BarBrush.Dispose()
ValueFont.Dispose()
TextFont.Dispose()
End Sub
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
GetData()
DrawChart()
EndSub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End
End Sub
End Class
Setelah anda mengetik listing dari source program diatas tekan F5, maka akan tampil gambar di bawah ini:
Kemudian tekanlah tombol View Bar 2D, maka grafik Bar 2D akan tampil seperti gambar di bawah ini:
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar