GDI+, Bar Charting, Structures, Bitmap Object, ArrayLists Develop a VB.NET proje
ID: 3575195 • Letter: G
Question
GDI+, Bar Charting, Structures, Bitmap Object, ArrayLists
Develop a VB.NET project, using GDI+ (Graphic Device Interface +) to draw a bar chart. The x-axis will represent a country and a y-axis will represent sales data.
Notes:
Use the GDI+ libraries to create 2-D graphic primitives
Use a Picture Box control to display a Bitmap image of the bar chart
Set appropriate margins, gaps, and scaling where the bitmap bar chart scales if the picture box control orientation changes
Use an ArrayList to store the country and population data (you can make these numbers up)
Use a Structure to declare sales data and country data
Example Output
1000 100 Belgium Greece Portugal Spain Turkey UKExplanation / Answer
Public Sub drawBarChart(ByVal objEnum As IDictionaryEnumerator, _
ByVal intItemCount As Integer, ByVal strGraphTitle As String, _
ByVal Xaxis As Integer, ByVal Yaxis As Integer, _
ByVal MaxWidth As Int16, ByVal MaxHt As Int16, _
ByVal clearForm As Boolean, _
Optional ByVal SpaceRequired As Boolean = False)
Dim intGraphXaxis As Integer = Xaxis
Dim intGraphYaxis As Integer = Yaxis
Dim intWidthMax As Integer = MaxWidth
Dim intHeightMax As Integer = MaxHt
Dim intSpaceHeight As Integer
Dim intMaxValue As Integer = 0
Dim intCounter As Integer
Dim intBarWidthMax
Dim intBarHeight
Dim strText As String
Try
Dim grfx As Graphics = CreateGraphics()
If clearForm = True Then
grfx.Clear(BackColor)
End If
grfx.DrawString(strGraphTitle, New Font("VERDANA", 12.0, _
FontStyle.Bold, GraphicsUnit.Point), _
Brushes.DeepPink, intGraphXaxis + (intWidthMax / 4), _
(intGraphYaxis - intHeightMax) - 40)
'Get the Height of the Bar
intBarHeight = CInt(intHeightMax / intItemCount)
'Get the space Height of the Bar
intSpaceHeight = _
CInt((intHeightMax / (intItemCount - 1)) - intBarHeight)
'Find Maximum of the input value
If Not objEnum Is Nothing Then
While objEnum.MoveNext = True
If objEnum.Value > intMaxValue Then
intMaxValue = objEnum.Value
End If
End While
End If
'Get the Maximum Width of the Bar
intBarWidthMax = CInt(intWidthMax / intMaxValue)
' Obtain the Graphics object exposed by the Form.
If Not objEnum Is Nothing Then
intCounter = 1
objEnum.Reset()
'Draw X axis and Y axis lines
grfx.DrawLine(Pens.Black, intGraphXaxis, _
intGraphYaxis, intGraphXaxis + intWidthMax, _
intGraphYaxis)
grfx.DrawLine(Pens.Black, intGraphXaxis, _
intGraphYaxis, intGraphXaxis, _
(intGraphYaxis - intHeightMax) - 25)
While objEnum.MoveNext = True
'Get new Y axis
intGraphYaxis = intGraphYaxis - intBarHeight
'Draw Rectangle
grfx.DrawRectangle(Pens.Black, _
New Rectangle(intGraphXaxis, intGraphYaxis, _
intBarWidthMax * objEnum.Value, intBarHeight))
'Fill Rectangle
grfx.FillRectangle(objColorArray(intCounter), _
New Rectangle(intGraphXaxis, intGraphYaxis, _
intBarWidthMax * objEnum.Value, intBarHeight))
'Display Text and value
strText = "(" & objEnum.Key & "," & objEnum.Value & ")"
grfx.DrawString(strText, New Font("VERDANA", 8.0, _
FontStyle.Regular, GraphicsUnit.Point), _
Brushes.Black, intGraphXaxis + _
(intBarWidthMax * objEnum.Value), intGraphYaxis)
intCounter += 1
If SpaceRequired = True Then
intGraphYaxis = intGraphYaxis - intSpaceHeight
End If
If intCounter > objColorArray.GetUpperBound(0) Then
intCounter = 1
End If
End While
If clearForm = True Then
grfx.Dispose()
End If
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.