DAVID'S BAYER

BLOGNYA ANAK STMIK

Membuat Grafik dengan Visual BASIC

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:

July 21, 2012 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: