objEnum As IDictionaryEnumerator - the data from which the graph has to be drawn.
intItemCount AsInteger - number of items to show in a graph (you can get from the DictionaryEnumerator itself).
strGraphTitle AsString - title has to be drawn for the graph.
Xaxis AsInteger - starting X axis.
Yaxis AsInteger - starting Y axis.
MaxWidth AsInt16 - maximum width of the graph (for calculation purpose).
MaxHt AsInt16 - maximum height of the graph (for calculation purpose).
clearForm AsBoolean - whether the Form has to clear ornot.
OptionalByVal SpaceRequired AsBoolean - whether space is required in between 2 graphs.
The graph has to be called in the Paint eventof the Form and the parameters should be resized in the Resize eventof the Form.
LoadColorArray procedure is used to have required colors to draw the graph. Because, if we use RGB, overall the graph will not be good visibly.
The parameter clearForm can be used when you need to show 2 graphs in the same Form; it will be useful then...
Collapse | Copy Code
PublicSub drawBarChart(ByVal objEnum As IDictionaryEnumerator, _
ByVal intItemCount AsInteger, ByVal strGraphTitle AsString, _
ByVal Xaxis AsInteger, ByVal Yaxis AsInteger, _
ByVal MaxWidth AsInt16, ByVal MaxHt AsInt16, _
ByVal clearForm AsBoolean, _
OptionalByVal SpaceRequired AsBoolean = False)
Dim intGraphXaxis AsInteger = Xaxis
Dim intGraphYaxis AsInteger = Yaxis
Dim intWidthMax AsInteger = MaxWidth
Dim intHeightMax AsInteger = MaxHt
Dim intSpaceHeight AsIntegerDim intMaxValue AsInteger = 0Dim intCounter AsIntegerDim intBarWidthMax
Dim intBarHeight
Dim strText AsStringTryDim grfx As Graphics = CreateGraphics()
If clearForm = TrueThen
grfx.Clear(BackColor)
EndIf
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
IfNot objEnum IsNothingThenWhile objEnum.MoveNext = TrueIf objEnum.Value > intMaxValue Then
intMaxValue = objEnum.Value
EndIfEndWhileEndIf'Get the Maximum Width of the Bar
intBarWidthMax = CInt(intWidthMax / intMaxValue)
' Obtain the Graphics object exposed by the Form.
IfNot objEnum IsNothingThen
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 += 1If SpaceRequired = TrueThen
intGraphYaxis = intGraphYaxis - intSpaceHeight
EndIfIf intCounter > objColorArray.GetUpperBound(0) Then
intCounter = 1EndIfEndWhileIf clearForm = TrueThen
grfx.Dispose()
EndIfEndIfCatch ex As Exception
Throw ex
EndTryEndSub