Membuat Grafik dengan Visual BASIC

· Uncategorized
Penulis

Tulisan berikut saya diambil dari http://bangdanu.wordpress.com/2012/03/25/membuat-grafik-dengan-visual-basic/

Pada tulisan kali ini akan membahas cara membuat grafik menggunakan listing Visual Basic. Grafik yang akan dibuat adalah Line Chart, Bar Chart dan Spiral Chart. Langsung coba yuk.

Langkah pertama, buatlah sebuah form dengan tampilan seperti pada gambar berikut ini.

Langkah berikutnya adalah dengan mengetikkan listing berikut ini pada form diatas.

Option Explicit
Dim N As Integer
Dim X(199) As Single
Dim Y(199) As Single
Dim YD(199) As Single

Private Sub LineChart(objectname As Control, N As Integer, X() As Single, Y() As Single)
Dim Xmin As Single, Xmax As Single
Dim Ymin As Single, Ymax As Single
Dim I As Integer
Xmin = X(0): Xmax = X(0)
Ymin = Y(0): Ymax = Y(0)
For I = 1 To N – 1
If X(I) Xmax Then Xmax = X(I)
If Y(I) Ymax Then Ymax = Y(I)
Next I
Ymin = (1 – 0.05 * Sgn(Ymin)) * Ymin
Ymax = (1 + 0.05 * Sgn(Ymax)) * Ymax
objectname.Scale (Xmin, Ymax)-(Xmax, Ymin)
objectname.Cls
objectname.PSet (X(0), Y(0))
For I = 1 To N – 1
objectname.Line -(X(I), Y(I))
Next I
End Sub

Private Sub Form_Load()
Dim I As Integer
Const PI = 3.14159
N = 200
For I = 1 To N – 1
X(I) = I
Y(I) = Exp(-0.01 * I) * Sin(PI * I / 10)
YD(I) = Exp(-0.01 * I) * (PI * Cos(PI * I / 10) / 10 – 0.01 * Sin(PI * I / 10))
Next I
End Sub

Private Sub mnuPlotBar_Click()
Call BarChart(PicPlot, N, X, Y)
End Sub

Private Sub mnuPlotExit_Click()
End
End Sub

Private Sub mnuPlotLine_Click()
Call LineChart(PicPlot, N, X, Y)
End Sub

Private Sub mnuPlotSpiral_Click()
Call LineChart(PicPlot, N, Y, YD)
End Sub

Private Sub BarChart(objectname As Control, N As Integer, X() As Single, Y() As Single)
Dim Xmin As Single, Xmax As Single
Dim Ymin As Single, Ymax As Single
Dim I As Integer
Xmin = X(0): Xmax = X(0)
Ymin = Y(0): Ymax = Y(0)
For I = 1 To N – 1
If X(I) Xmax Then Xmax = X(I)
If Y(I) Ymax Then Ymax = Y(I)
Next I
Ymin = (1 – 0.05 * Sgn(Ymin)) * Ymin
Ymax = (1 + 0.05 * Sgn(Ymax)) * Ymax
objectname.Scale (Xmin, Ymax)-(Xmax, Ymin)
objectname.Cls
For I = 1 To N – 1
objectname.Line (X(I), 0)-(X(I), Y(I)), vbBlue
Next I
End Sub

Kalau sudah selesai silahkan disimpan dan dijalankan. Bagaimana hasilnya?
Line Chart:

Bar Chart:

Spiral Chart:

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: