在 ASP.NET 中生成条形图






4.10/5 (18投票s)
本文演示了如何使用 System.Drawing 命名空间在 ASP.NET 中创建图表。
引言
以下示例演示了如何为网页上可用的任何业务信息生成条形图。它使用 .NET System.Drawing
命名空间中提供的类来生成图表。
下面示例中创建的条形图说明了公司从一月到十二月每个月的利润。
生成条形图
要在 X 轴和 Y 轴上显示的数据存储在 ArrayList
中,然后从这些 ArrayList
中读取数据以创建所需的条形图。
首先,ArrayList
填充如下
Dim aMonths As ArrayList = New ArrayList()
aProfits As ArrayList = New ArrayList()
aMonths.Add("January")
aMonths.Add("February")
aMonths.Add("March")
aMonths.Add("April")
aMonths.Add("May")
aMonths.Add("June")
aMonths.Add("July")
aMonths.Add("August")
aMonths.Add("September")
aMonths.Add("October")
aMonths.Add("November")
aMonths.Add("December")
aProfits.Add(240500)
aProfits.Add(220950)
aProfits.Add(283500)
aProfits.Add(340000)
aProfits.Add(325750)
aProfits.Add(123456)
aProfits.Add(235621)
aProfits.Add(345235)
aProfits.Add(290451)
aProfits.Add(152345)
aProfits.Add(653456)
aProfits.Add(785620)
数据填充完毕后,可以通过调用方法 DrawBarGraph
生成图表
DrawBarGraph("Profits!", aMonths, aProfits)
DrawBarGraph
定义如下
Sub DrawBarGraph(ByVal strTitle As String, _
ByVal aX As ArrayList, ByVal aY As ArrayList)
Const iColWidth As Integer = 60, iColSpace As Integer = 25, _
iMaxHeight As Integer = 400, iHeightSpace As Integer = 25, _
iXLegendSpace As Integer = 30, iTitleSpace As Integer = 50
Dim iMaxWidth As Integer = (iColWidth + iColSpace) * aX.Count + iColSpace, _
iMaxColHeight As Integer = 0, _
iTotalHeight As Integer = iMaxHeight + iXLegendSpace + iTitleSpace
Dim objBitmap As Bitmap = New Bitmap(iMaxWidth, iTotalHeight)
Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
objGraphics.FillRectangle(New SolidBrush(Color.White), _
0, 0, iMaxWidth, iTotalHeight)
objGraphics.FillRectangle(New SolidBrush(Color.Ivory), _
0, 0, iMaxWidth, iMaxHeight)
' find the maximum value
Dim iValue As Integer
For Each iValue In aY
If iValue > iMaxColHeight Then iMaxColHeight = iValue
Next
Dim iBarX As Integer = iColSpace, iCurrentHeight As Integer
Dim objBrush As SolidBrush = New SolidBrush(Color.FromArgb(70, 20, 20))
Dim fontLegend As Font = New Font("Arial", 11), _
fontValues As Font = New Font("Arial", 8), _
fontTitle As Font = New Font("Arial", 24)
' loop through and draw each bar
Dim iLoop As Integer
For iLoop = 0 To aX.Count - 1
iCurrentHeight = ((Convert.ToDouble(aY(iLoop)) / _
Convert.ToDouble(iMaxColHeight)) * _
Convert.ToDouble(iMaxHeight - iHeightSpace))
objGraphics.FillRectangle(objBrush, iBarX, _
iMaxHeight - iCurrentHeight, iColWidth, iCurrentHeight)
objGraphics.DrawString(aX(iLoop), fontLegend, _
objBrush, iBarX, iMaxHeight)
objGraphics.DrawString(Format(aY(iLoop), "#,###"), _
fontValues, objBrush, iBarX, iMaxHeight - iCurrentHeight - 15)
iBarX += (iColSpace + iColWidth)
Next
objGraphics.DrawString(strTitle, fontTitle, objBrush, _
(iMaxWidth / 2) - strTitle.Length * 6, iMaxHeight + iXLegendSpace)
'objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF)
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
objGraphics.Dispose()
objBitmap.Dispose()
End Sub
这段代码将生成条形图并在屏幕上显示。此外,如果需要,可以将条形图保存为图像,只需取消注释上述代码中的下一行即可
objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF)