Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'

' V. Hutabalian's Blog INPUT LISTBOX Vs PLOTTING (X,Y) Vs PICTUREBOX '


'======================================================================='
' Entry Data Pada ListBox dengan 2 masukan X dan Y '
' Berbagi ilmu Sensasi Kepuasan Tersendiri '
' Programming by: Verynandus Hutabalian '
' Publish to V. Hutabalian's Blog 7 Mei 2010 '
'+++++++++++++++++++++++========================++++++++++++++++++++++++'
Imports System.Drawing.Drawing2D
Public Class Form1
Dim LeftMargin As Integer = 35
Dim RightMargin As Integer = 15
Dim BaseMargin As Integer = 35
Dim TopMargin As Integer = 10
Dim VertLineLength As Integer
Dim BaseLineLength As Integer
Dim LineWidth As Double
Dim g As Graphics
Dim bmap As Bitmap
Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventAr
gs)
If IsNumeric(TextBox1.Text) = False Then
MsgBox("Harus Karakter Angaka", MsgBoxStyle.Exclamation)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Event
Args) Handles Button1.Click
If ListBox3.Text = "" Then
MsgBox("Pilih Data Yang Terlebih dulu di Inputkan", MsgBoxStyle.Exclamation)
End If
If ListBox3.Text = "Data X" Then
If TextBox1.Text = "" Then
MsgBox("Masukkan Data X")
Else
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Focus()
End If
End If
If ListBox3.Text = "Data Y" Then
If TextBox1.Text = "" Then
MsgBox("Masukkan Data Y")
Else
ListBox2.Items.Add(TextBox1.Text)
TextBox1.Focus()
End If
End If
End Sub
Private Sub DrawOutline()
bmap = New Bitmap(PBLineChart.Width, PBLineChart.Height, PBLineChart.CreateGraph
ics)
g = Graphics.FromImage(bmap)
Dim StartPoint As New Point(LeftMargin, PBLineChart.Height - BaseMargin)
Dim EndPoint As New Point(LeftMargin, TopMargin)

Dim LinePen As New Pen(Color.Red, 2)


g.DrawLine(LinePen, StartPoint, EndPoint)
Dim VertLineLength As Integer = PBLineChart.Height - (BaseMargin
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim TickSP As New Point(LeftMargin - 5, StartPoint.Y - VertGap)
Dim TickEP As New Point(LeftMargin, StartPoint.Y - VertGap)
Dim ValueFont As New Font("Arial", 8, FontStyle.Regular)
For i As Integer = 1 To 10
g.DrawLine(New Pen(Color.Black), TickSP, TickEP)
g.DrawString(CStr(i * 10), ValueFont, Brushes.Red, 2, TickSP.Y TickSP.Y -= VertGap
TickEP.Y -= VertGap
Next
g.DrawLine(LinePen, LeftMargin, PBLineChart.Height - BaseMargin,
th - RightMargin, PBLineChart.Height - BaseMargin)
End Sub
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e
Args) Handles btnDraw.Click
DrawOutline()
DrawHorizontalLines()
DrawVerticalGridLines()
DrawTheLine()
ShowMonths()
FinalDisplay()
End Sub
Private Sub DrawTheLine()
Dim MyPath As New GraphicsPath
Dim MyPen As Pen = New Pen(Color.Blue, 3)
g.DrawPath(MyPen, MyPath)
Dim VertScale As Double
Dim VertLineLength As Integer = PBLineChart.Height - (BaseMargin
Dim XPosStart As Integer = CInt(LeftMargin + 30)
VertScale = VertLineLength / 100
Dim XPosEnd As Integer = CInt(XPosStart + LineWidth)
Dim s(ListBox2.Items.Count) As String
Dim al As New ArrayList
ListBox2.Items.CopyTo(s, 0)
al = New ArrayList(s)
Me.ListBox2.DataSource = al
Dim YPosStart As Integer = CInt(s(0) * VertScale)
Dim YPosEnd As Integer = CInt(s(1) * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
For i As Integer = 1 To UBound(s) - 2
XPosStart = XPosEnd
XPosEnd = CInt(XPosStart + LineWidth)
YPosStart = YPosEnd
YPosEnd = CInt(s(i + 1) * VertScale)
MyPath.AddEllipse(XPosStart - 2, YPosStart - 2, 4, 4)
MyPath.AddLine(XPosStart, YPosStart, XPosEnd, YPosEnd)
Next
MyPath.AddEllipse(XPosEnd - 2, YPosEnd - 2, 4, 4)

+ TopMargin)

5)

PBLineChart.Wid
As System.Event

+ TopMargin)

g.RotateTransform(180)
g.ScaleTransform(-1, 1)
g.TranslateTransform(0, VertLineLength + 10, MatrixOrder.Append)
g.DrawPath(MyPen, MyPath)
g.ResetTransform()
End Sub
Private Sub FinalDisplay()
PBLineChart.Image = bmap
g.Dispose()
End Sub
Private Sub ShowMonths()
Dim TextStartX As Integer = CInt(LeftMargin + 18)
Dim TextBrsh As Brush = New SolidBrush(Color.Red)
Dim TextFont As New Font("Arial", 10, FontStyle.Regular)
Dim s1(ListBox1.Items.Count) As String
Dim a2 As New ArrayList
ListBox1.Items.CopyTo(s1, 0)
a2 = New ArrayList(s1)
Me.ListBox1.DataSource = a2
For i As Integer = 1 To s1.Length- 1
g.DrawString(s1(i - 1), TextFont, TextBrsh, TextStartX, _
CInt(PBLineChart.Height - (BaseMargin - 4)))
TextStartX += CInt(LineWidth)
Next
End Sub
Private Sub DrawVerticalGridLines()
Dim
Dim
Dim
Dim
Dim
Dim

ThinPen As New Pen(Color.LightGreen, 2)


StartPoint As New Point(LeftMargin, PBLineChart.Height - BaseMargin)
EndPoint As New Point(LeftMargin, TopMargin)
LinePen As New Pen(Color.Red, 2)
s(ListBox2.Items.Count) As String
s1(ListBox1.Items.Count) As String

Dim al As New ArrayList


Dim a2 As New ArrayList
ListBox2.Items.CopyTo(s, 0)
ListBox1.Items.CopyTo(s1, 0)
al = New ArrayList(s)
a2 = New ArrayList(s1)
Me.ListBox2.DataSource = al
Me.ListBox1.DataSource = a2
g.DrawLine(LinePen, StartPoint, EndPoint)
BaseLineLength = PBLineChart.Width - (LeftMargin + RightMargin)
LineWidth = (BaseLineLength / s.Length - 1)
Dim LineStartX As Integer = CInt(LeftMargin + 30)
For i As Integer = 1 To s1.Length - 1
g.DrawLine(ThinPen, LineStartX, TopMargin, LineStartX, PBLineChart.Height - (Bas
eMargin + 4))
LineStartX += CInt(LineWidth)
Next

ThinPen.Dispose()
End Sub
Private Sub DrawHorizontalLines()
Dim VertLineLength As Integer = PBLineChart.Height - (BaseMargin + TopMargin)
Dim VertGap As Integer = CInt(VertLineLength / 10)
Dim StartPoint As New Point(LeftMargin + 3, PBLineChart.Height - BaseMargin)
Dim EndPoint As New Point(PBLineChart.Width, PBLineChart.Height - BaseMargin)
Dim LineStart As New Point(StartPoint.X, StartPoint.Y - VertGap)
Dim LineEnd As New Point(EndPoint.X, StartPoint.Y - VertGap)
Dim ThinPen As New Pen(Color.LightGreen, 3)
For i As Integer = 1 To 10
g.DrawLine(ThinPen, LineStart, LineEnd)
LineStart.Y -= VertGap
LineEnd.Y -= VertGap
Next
ThinPen.Dispose()
End Sub
Private Sub PBLineChart_Paint(ByVal sender As Object, ByVal e As System.Windows.
Forms.PaintEventArgs) Handles PBLineChart.Paint
Dim ix As Integer = PBLineChart.Width
Dim iy As Integer = PBLineChart.Height
e.Graphics.DrawString("Coordinat-Y", New Font("Lucida Handwriting", 10, FontStyl
e.Regular), Brushes.Goldenrod, iy - 360, 6)
e.Graphics.DrawString("Coordinat-X", New Font("Lucida Handwriting", 10, FontStyl
e.Regular), Brushes.Goldenrod, iy + 20, 370)
e.Graphics.DrawString("V. HUTABALIAN'S Blog", New Font("Lucida Handwriting", 10,
FontStyle.Regular), Brushes.Goldenrod, iy - 40, 6)
e.Graphics.DrawString("7 Mei 2010", New Font("Lucida Handwriting", 10, FontStyle
.Regular), Brushes.Goldenrod, iy - 35, 20)
End Sub
End Class

You might also like