Menampilkan data dalam format grafik
Kali ini saya akan share bagai mana menampilkan data dalam format grafik. Visual Basic sendiri sudah punya komponen untuk menghandle masalah ini tapi untuk artikel kali ini, saya menggunakan komponen grafik dari componentone dan untuk download komponennya bisa langsung ke SINI.
Sedangkan untuk sample kasus, kita akan menampilkan data statistik penerimaan mahasiswa baru, berikut tampilannya
berikut kode untuk pengaturan grafiknya, untuk contoh disini berupa grafik bar/batang :
Private Sub settingGrafik(ByVal Grafik As Chart2D, ByVal title As String, ByVal axesYTitle As String)
Grafik.Header.Text = title
' Change Footer Formatting
Grafik.Footer.Font.Size = 9
' Format the Y-axis to use Currency format
Grafik.ChartArea.Axes("Y").LabelFormat.Category = oc2dCategoryStandard
' Add Axes titles
Grafik.ChartArea.Axes("Y").title = axesYTitle
With Grafik.ChartArea
.Bar.ClusterWidth = 60 'lebar bar grafik
.Bar.ClusterOverlap = -100 'jarak antar bar grafik
End With
With Grafik.Header
.Font.Size = 10
.Font.Bold = True
End With
' Rotate the Y-Axis 90 degrees
Grafik.ChartArea.Axes("Y").TitleRotation = oc2dRotate90Degrees
' Add the legend to the chart
Grafik.Legend.IsShowing = True
Grafik.ChartGroups(1).ChartType = oc2dTypeBar 'jenis grafiknya diatur disini
End Sub
Selanjutnya tinggal kita tampilkan grafiknya :
Private Sub cmdPrevGrafik_Click()
Dim numPoints As Long
Dim numSeries As Long
Dim iIndex As Long
Dim iThn As Long
Dim i As Long
Dim jmlMhs As Long
Grafik.Visible = False
DoEvents
With Grafik.ChartGroups(1).Data
.numSeries = UBound(arrJurusan) + 1 'set jumlah jurusan
.numPoints(1) = Val(cmbTahun2.Text) - Val(cmbTahun1.Text) + 1
numPoints = 0
iIndex = 0
Grafik.ChartLabels.RemoveAll
For iThn = Val(cmbTahun1.Text) To Val(cmbTahun2.Text)
numPoints = numPoints + 1
numSeries = 0
For i = LBound(arrJurusan) To UBound(arrJurusan)
jmlMhs = getJumlahMhs(iThn, arrJurusan(i))
numSeries = numSeries + 1
.X(1, numPoints) = numPoints
.Y(numSeries, numPoints) = jmlMhs
Grafik.ChartLabels.Add
iIndex = iIndex + 1
With Grafik.ChartLabels(iIndex)
.AttachMethod = oc2dAttachDataIndex
.AttachDataIndex.Series = numSeries
.AttachDataIndex.Point = numPoints
.Text = jmlMhs
End With
Next i
Next iThn
End With
With Grafik.ChartGroups(1).SeriesLabels
.RemoveAll
Grafik.Legend.Text = "Jurusan"
For i = LBound(arrJurusan) To UBound(arrJurusan)
.Add arrJurusan(i)
Next
End With
With Grafik.ChartGroups(1).PointLabels
'untuk menampilkan tahun
Grafik.ChartArea.Axes("X").AnnotationMethod = oc2dAnnotatePointLabels
.RemoveAll
For i = Val(cmbTahun1.Text) To Val(cmbTahun2.Text)
.Add i
Next
Grafik.ChartArea.Axes("X").title = "T a h u n"
End With
Grafik.Visible = True
End Sub
Selamat MENCOBA
Comments