GDI+Visual Studio .NET 2002.NET 1.0Visual Studio .NET 2003Windows 2003.NET 1.1Windows 2000Windows XP中级开发Visual StudioWindows.NETVisual Basic
绘制条形图






2.93/5 (26投票s)
2004 年 6 月 18 日
1分钟阅读

145574

1183
这篇文章是关于绘制给定值集的条形图的。
引言
这篇文章描述了一个在窗体中绘制条形图的程序,具有所有功能。使用此过程,可以通过设置 X 轴和 Y 轴在窗体中绘制任意数量的图表。
输入参数
objEnum As IDictionaryEnumerator
- 用于绘制图表的数据。intItemCount As Integer
- 要在图表中显示的项目的数量(您可以从DictionaryEnumerator
本身获取)。strGraphTitle As String
- 要为图表绘制的标题。Xaxis As Integer
- 起始 X 轴。Yaxis As Integer
- 起始 Y 轴。MaxWidth As Int16
- 图表的最大宽度(用于计算)。MaxHt As Int16
- 图表的最大高度(用于计算)。clearForm As Boolean
- 是否需要清除Form
。Optional ByVal SpaceRequired As Boolean
- 两个图表之间是否需要空格。
该图表必须在 Form
的 Paint
事件中调用,并且参数应该在 Form
的 Resize
事件中调整大小。
LoadColorArray
过程用于获得绘制图表所需的颜色。因为,如果我们使用 RGB,总的来说,图表看起来不会很好。
当您需要在同一个 Form
中显示 2 个图表时,可以使用参数 clearForm
;在这种情况下它会很有用...
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
该过程的优点
我们可以在一个窗体中拥有任意数量的图表,并且可以在运行时更改图表的设置。 使用 LoadColorArrays
过程(此处未粘贴。但可在演示项目中找到),我们可以在图表中提供良好的颜色,这将非常吸引用户。