VB.NET Technology

VB.Net Projects

VB.Net Project 1

Creating Bar Chart in windows using VB.Net
Previous Home Next
adplus-dvertising

Introduction

For making bar chart we will make use of ‘DrawRectangle’ in built function. because we have to first create a big rectangle defining the boundary of bar graphs. Parameters of DrawRectangle is

  1. Pen – This defines the color and style of border

  2. Rectangle – Rectangle object to be created

Private Sub New(x As Integer, y As Integer, width As Integer, height As Integer)
End Sub

X and y are the co-ordinates of top left corner of rectangle. Width and height are width and height of rectangle.

Height = (weight of current array element *height of outer rectangle )/ maximum weight.

X coordinate is incremented by width of bars everytime a new bar is created.

Y co-ordinate is calculated by the following formula:

y = y coordinate of outer rectangle + height of outer rectangle – height of bar

Code for DrawBarChart

Private Sub DrawBarChart(ByVal e As PaintEventArgs, ByVal alWeight As Integer())
        Dim numberOfSections As Integer = alWeight.Length
        Dim lengthArea As Integer = 330
        Dim heightArea As Integer = 280
        Dim topX As Integer = 20
        Dim topY As Integer = 20
        Dim maxWeight As Integer = MaxValue(alWeight)
        Dim height As Integer() = New Integer(numberOfSections - 1) {}
        Dim total As Integer = SumOfArray(alWeight)
        Dim rnd As New Random()
        Dim brush As New SolidBrush(Color.Aquamarine)
        Dim pen As New Pen(Color.Gray)
        Dim rec As New Rectangle(topX, topY, lengthArea, heightArea)
        e.Graphics.DrawRectangle(pen, rec)
        pen.Color = Color.Black
        Dim smallX As Integer = topX
        Dim smallY As Integer = 0
        Dim smallLength As Integer = (lengthArea \ alWeight.Length)
        Dim smallHeight As Integer = 0
        For i As Integer = 0 To numberOfSections - 1
            brush.Color = Color.FromArgb(rnd.[Next](200, 255), 
	    rnd.[Next](255), rnd.[Next](255), rnd.[Next](255))
            smallHeight = ((alWeight(i) * heightArea) \ maxWeight)
            smallY = topY + heightArea - smallHeight
            Dim rectangle As New Rectangle(smallX, smallY, smallLength, smallHeight)
            e.Graphics.DrawRectangle(pen, rectangle)
            e.Graphics.FillRectangle(brush, rectangle)
            brush.Color = Color.FromKnownColor(KnownColor.Black)
            e.Graphics.DrawRectangle(pen, rectangle)
            smallX = smallX + smallLength
        Next
    End Sub
Previous Home Next