Tips kali ini kita akan membuat visual Grafik Pie Chart dengan menggunakan Visual Basic 2008. Ikuti langkah-langkah di bawah ini, hasil programnya seperti gambar di bawah ini.
ToolBox yang digunakan sebagai berikut:
Rancanglah 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 OnImports System.Drawing.Drawing2D
Imports System.Collections
Structure GraphData
Dim Name As String
Dim Amount As Decimal
Dim Clr As Color
Dim Pattern As HatchStyle
End Structure
Public Class Form1
Dim UserData As New ArrayList
Dim clrPicked As Drawing.Color = Color.Black
Dim hatchPicked As HatchStyle = HatchStyle.DarkHorizontal
Private Sub btnColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
Dim ColorPicker As New ColorDialog
Dim Choice As DialogResult = ColorPicker.ShowDialog
If Choice <> DialogResult.Cancel Then
clrPicked = ColorPicker.Color
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
Dim patts() As String
patts = System.Enum.GetNames(GetType(HatchStyle))
cboPattern.Items.AddRange(patts)
End Sub
Private Sub cboPattern_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPattern.SelectedIndexChanged
hatchPicked = CType(System.Enum.Parse(GetType(HatchStyle), _
cboPattern.Text), HatchStyle)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If txtName.Text = ""Then txtName.Text = "No Name"
Dim gd As New GraphData
gd.Name = txtName.Text
gd.Amount = nudValue.Value
gd.Clr = clrPicked
gd.Pattern = hatchPicked
UserData.Add(gd)
End Sub
Private Sub pnlChart_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlChart.Paint
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.HighQuality
Dim PattBrush As Brush
If UserData.Count > 0 Then
Dim rect As Rectangle = New Rectangle(20, 10, 200, 200)
Dim TotalCount As Single
For Each gd As GraphData In UserData
TotalCount += gd.Amount
Next
Dim StartAngle As Single = 0
Dim SweepAngle As Single = 0
For Each gd As GraphData In UserData
SweepAngle = 360 * gd.Amount / TotalCount
PattBrush = New HatchBrush(gd.Pattern, gd.Clr, Color.White)
g.FillPie(PattBrush, rect, StartAngle, SweepAngle)
g.DrawPie(New Pen(Color.Black), rect, StartAngle, SweepAngle)
StartAngle += SweepAngle
Next
Dim TextBrsh As Brush = New SolidBrush(Color.Black)
Dim TextFont As New Font("Verdana", 9, FontStyle.Bold)
Dim pxFromTop As Integer = 235
g.DrawString("Distribusi Nilai Pie Chart", TextFont, TextBrsh, 35, pxFromTop)
For Each gd As GraphData In UserData
pxFromTop += 20
PattBrush = New HatchBrush(gd.Pattern, gd.Clr, Color.White)
g.FillRectangle(PattBrush, 20, pxFromTop, 15, 15)
g.DrawRectangle(New Pen(Color.Black), 20, pxFromTop, 15, 15)
g.DrawString(gd.Name & " (" & gd.Amount & ")", TextFont, TextBrsh, 60, pxFromTop)
Next
TextBrsh.Dispose()
TextFont.Dispose()
End If
’PattBrush.Dispose()
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
pnlChart.Invalidate()
End Sub
Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
UserData.Clear()
pnlChart.Invalidate()
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 anda mengetik listing dari source program diatas tekan F5, maka akan tampil gambar di bawah ini:
Pertamakali masukkan Nama
Masukkan nilai (Maks 100/ dapat diatur dari propeties)
Tekan tombol Select Color untuk memilih warna
Pilih pola Warna pada Choose a Pattern
Tekan Tombol Draw/Redraw
Untuk nama selanjutnya ulangi langkah-langkah dari awal masukkan nama
Untuk menghapus Pie Chart tekan tombol Restart Draw
Grafik Pie Chart akan tampil seperti gambar di bawah ini:
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
1 komentar:
Bagus... programnya
Posting Komentar