Assalamu’alaikum rekan-rekan pembaca sekalian. Lama tak menulis rasanya kangen juga ternyata ya.
Tulisan berikut saya dedikasikan khusus untuk pembaca yang pernah bertanya melalui komentar di tulisan saya sebelumnya tentang Grafika Komputer
Pada tulisan kali ini kita akan membahas cara membuat grafik menggunakan listing Visual Basic. Grafik yang akan kita buat adalah Line Chart, Bar Chart dan Spiral Chart. Langsung kita 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:
Spiral Chart:
Silahkan Anda coba dan share disini ya.
Selamat mencoba dan semoga berguna
sumber : http://bangdanu.wordpress.com/2012/03/25/membuat-grafik-dengan-visual-basic/#more-826
0 komentar:
Post a Comment