Tips kali ini kita akan membuat visual Grafik Bar 3D dengan menggunakan Visual Basic 2008. Tampilan seperti gambar dibawah ini.
Ikuti langkah-langkah 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
Imports System.Drawing.Drawing2D
Imports System.Collections
Public Class Form1
Structure GraphData
Dim Country As String
Dim Sales As Integer
Dim BarColor As Color
Dim Country As String
Dim Sales As Integer
Dim BarColor As Color
Sub NewByVal country As String, ByVal sales AsShort, ByVal barcol As Color)
Me.Country = country
Me.Sales = sales
Me.BarColor = barcol
Me.Country = country
Me.Sales = sales
Me.BarColor = barcol
End Sub
End Structure
End Structure
’ Create Variables
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
Dim SalesData As New ArrayList
Dim HighSale As Double’ Nilai tertinggi
Dim VertScale As Double’ Ukuran tinggi Bar
Dim g As Graphics
Dim bmap As Bitmap
Dim VertLineLength As Integer
Dim BarWidth As Integer’ lebar bar
Dim BaseLineLength As Integer’ X panjang garis Horizontal
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
Dim SalesData As New ArrayList
Dim HighSale As Double’ Nilai tertinggi
Dim VertScale As Double’ Ukuran tinggi Bar
Dim g As Graphics
Dim bmap As Bitmap
Dim VertLineLength As Integer
Dim BarWidth As Integer’ lebar bar
Dim BaseLineLength As Integer’ X panjang garis Horizontal
Private Sub GetData()
SalesData.Clear()
SalesData.Add(New GraphData("Medan", 934, Color.Blue))
SalesData.Add(New GraphData("Bandung", 385, Color.DarkOrange))
SalesData.Add(New GraphData("Batam", 1029, Color.Green))
SalesData.Add(New GraphData("Jakarta", 729, Color.IndianRed))
SalesData.Add(New GraphData("Surabaya", 1472, Color.Tomato))
SalesData.Add(New GraphData("Bali", 1142, Color.Aquamarine))
End Sub
SalesData.Clear()
SalesData.Add(New GraphData("Medan", 934, Color.Blue))
SalesData.Add(New GraphData("Bandung", 385, Color.DarkOrange))
SalesData.Add(New GraphData("Batam", 1029, Color.Green))
SalesData.Add(New GraphData("Jakarta", 729, Color.IndianRed))
SalesData.Add(New GraphData("Surabaya", 1472, Color.Tomato))
SalesData.Add(New GraphData("Bali", 1142, Color.Aquamarine))
End Sub
Private Function GetGraphics() As Graphics
bmap = New Bitmap(PBBarChart.Width, PBBarChart.Height, PBBarChart.CreateGraphics)
Return Graphics.FromImage(bmap)
End Function
bmap = New Bitmap(PBBarChart.Width, PBBarChart.Height, PBBarChart.CreateGraphics)
Return Graphics.FromImage(bmap)
End Function
Private Sub DrawVerticalAxis(ByVal g As Graphics)
Dim StartPoint As New Point(LeftMargin, PBBarChart.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Black, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
VertLineLength = PBBarChart.Height - (BaseMargin + TopMargin)
For Each gd As GraphData In SalesData
If gd.Sales > HighSale Then HighSale = gd.Sales
Next
Dim NextCent As Integer = CInt(HighSale)
Do While NextCent Mod 100 <> 0
NextCent += 1
Loop
Dim TotalTicks As Integer = CInt(NextCent / 100)
Dim YPos As Integer = CInt(VertLineLength / TotalTicks)
Dim TickSP As New Point(LeftMargin - 5, StartPoint.Y - YPos)
Dim TickEP As New Point(LeftMargin, StartPoint.Y - YPos)
Dim ValueFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To TotalTicks
g.DrawLine(New Pen(Color.Black), TickSP, TickEP) ’ Tick mark
g.DrawString(CStr(i * 100), ValueFont, Brushes.Black, 2, TickSP.Y - 5)
TickSP.Y = CInt(TickSP.Y - (VertLineLength / TotalTicks))
TickEP.Y -= CInt(VertLineLength / TotalTicks)
Next
End Sub
Dim StartPoint As New Point(LeftMargin, PBBarChart.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)
Dim LinePen As New Pen(Color.Black, 2)
g.DrawLine(LinePen, StartPoint, EndPoint)
VertLineLength = PBBarChart.Height - (BaseMargin + TopMargin)
For Each gd As GraphData In SalesData
If gd.Sales > HighSale Then HighSale = gd.Sales
Next
Dim NextCent As Integer = CInt(HighSale)
Do While NextCent Mod 100 <> 0
NextCent += 1
Loop
Dim TotalTicks As Integer = CInt(NextCent / 100)
Dim YPos As Integer = CInt(VertLineLength / TotalTicks)
Dim TickSP As New Point(LeftMargin - 5, StartPoint.Y - YPos)
Dim TickEP As New Point(LeftMargin, StartPoint.Y - YPos)
Dim ValueFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To TotalTicks
g.DrawLine(New Pen(Color.Black), TickSP, TickEP) ’ Tick mark
g.DrawString(CStr(i * 100), ValueFont, Brushes.Black, 2, TickSP.Y - 5)
TickSP.Y = CInt(TickSP.Y - (VertLineLength / TotalTicks))
TickEP.Y -= CInt(VertLineLength / TotalTicks)
Next
End Sub
Private Sub Draw3DBars()
g.DrawLine(New Pen(Color.Black), LeftMargin, PBBarChart.Height - BaseMargin, _
PBBarChart.Width - RightMargin, PBBarChart.Height - BaseMargin)
BaseLineLength = PBBarChart.Width - (LeftMargin + RightMargin)
BarWidth = CInt((BaseLineLength / SalesData.Count) - BarGap)
Dim BarStartX As Integer = LeftMargin + BarGap
Dim BaseLine As Integer = PBBarChart.Height - BaseMargin
VertScale = VertLineLength / HighSale
For Each gd As GraphData In SalesData
Dim Corners(3) As Point
Corners(0) = New Point(BarStartX, BaseLine - 10)
Corners(1) = New Point(BarStartX + BarWidth - 5, BaseLine - 10)
Corners(2) = New Point(BarStartX + BarWidth, BaseLine)
Corners(3) = New Point(BarStartX + 5, BaseLine)
Dim BarHeight As Integer = CInt(gd.Sales * VertScale)
Dim barmainBrush As New HatchBrush(HatchStyle.Percent50, gd.BarColor)
Dim bartopBrush As New SolidBrush(gd.BarColor)
For i As Integer = 0 To BarHeight - 11
g.FillPolygon(barmainBrush, Corners)
Corners(0).Y -= 1
Corners(1).Y -= 1
Corners(2).Y -= 1
Corners(3).Y -= 1
Next
g.FillPolygon(bartopBrush, Corners)
BarStartX += CInt(BarWidth + BarGap)
barmainBrush.Dispose()
bartopBrush.Dispose()
Next
End Sub
g.DrawLine(New Pen(Color.Black), LeftMargin, PBBarChart.Height - BaseMargin, _
PBBarChart.Width - RightMargin, PBBarChart.Height - BaseMargin)
BaseLineLength = PBBarChart.Width - (LeftMargin + RightMargin)
BarWidth = CInt((BaseLineLength / SalesData.Count) - BarGap)
Dim BarStartX As Integer = LeftMargin + BarGap
Dim BaseLine As Integer = PBBarChart.Height - BaseMargin
VertScale = VertLineLength / HighSale
For Each gd As GraphData In SalesData
Dim Corners(3) As Point
Corners(0) = New Point(BarStartX, BaseLine - 10)
Corners(1) = New Point(BarStartX + BarWidth - 5, BaseLine - 10)
Corners(2) = New Point(BarStartX + BarWidth, BaseLine)
Corners(3) = New Point(BarStartX + 5, BaseLine)
Dim BarHeight As Integer = CInt(gd.Sales * VertScale)
Dim barmainBrush As New HatchBrush(HatchStyle.Percent50, gd.BarColor)
Dim bartopBrush As New SolidBrush(gd.BarColor)
For i As Integer = 0 To BarHeight - 11
g.FillPolygon(barmainBrush, Corners)
Corners(0).Y -= 1
Corners(1).Y -= 1
Corners(2).Y -= 1
Corners(3).Y -= 1
Next
g.FillPolygon(bartopBrush, Corners)
BarStartX += CInt(BarWidth + BarGap)
barmainBrush.Dispose()
bartopBrush.Dispose()
Next
End Sub
Private Sub WriteTheNames()
Dim TextStartX As Integer = LeftMargin + BarGap + 5
Dim TextBrsh As Brush = New SolidBrush(Color.Black)
Dim fntSize As Integer = CInt(BarWidth / 7)
Dim TextFont As New Font("Verdana", fntSize)
For Each gd As GraphData In SalesData
g.DrawString(gd.Country, TextFont, TextBrsh, TextStartX, CInt(PBBarChart.Height - (BaseMargin - 4)))
TextStartX += CInt(BarWidth + BarGap)
Next
End Sub
Dim TextStartX As Integer = LeftMargin + BarGap + 5
Dim TextBrsh As Brush = New SolidBrush(Color.Black)
Dim fntSize As Integer = CInt(BarWidth / 7)
Dim TextFont As New Font("Verdana", fntSize)
For Each gd As GraphData In SalesData
g.DrawString(gd.Country, TextFont, TextBrsh, TextStartX, CInt(PBBarChart.Height - (BaseMargin - 4)))
TextStartX += CInt(BarWidth + BarGap)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetData()
g = GetGraphics()
DrawVerticalAxis(g)
Draw3DBars()
WriteTheNames()
PBBarChart.Image = bmap
g.Dispose()
GetData()
g = GetGraphics()
DrawVerticalAxis(g)
Draw3DBars()
WriteTheNames()
PBBarChart.Image = bmap
g.Dispose()
End Sub
End Class
Setelah anda mengetik listing dari source program diatas tekan F5, maka akan tampil gambar di bawah ini:
Kemudian tekanlah tombol Bar 3D/View, maka grafik Bar 3D akan tampil seperti gambar di bawah ini:
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar