Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Data.SqlClient
Public Class Charts
Inherits System.Web.UI.Page
Dim Values As ArrayList = New ArrayList()
Dim Captions As ArrayList = New ArrayList()
Dim Connection As SqlConnection
Dim DR As SqlDataReader
Dim Command As SqlCommand
Dim TableName As String
Dim SumNum As Int32
Dim Item_Count As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TableName = Request.QueryString("table")
Connection = New SqlConnection(Application("SqlConString"))
'统计数据到两个数组
ReadData()
'生成饼图
DisplayPieChart()
End Sub
'This function returns a color for the bar and pie charts.
Private Function GetColor(ByVal itemIndex As Integer) As Color
Dim objColor As Color
Select Case itemIndex
Case 0
objColor = Color.Blue
Case 1
objColor = Color.Red
Case 2
objColor = Color.Yellow
Case 3
objColor = Color.Purple
Case 4
objColor = Color.Orange
Case 5
objColor = Color.Brown
Case 6
objColor = Color.Gray
Case 7
objColor = Color.Maroon
Case Else
objColor = Color.Green
End Select
Return objColor
End Function
Sub ReadData()
Dim SqlString As String
Dim I As Integer
Dim Num_Temp As Int16
SumNum = 0
I = 0
SqlString = "Select r1,count(r1) as cc from " + TableName + " group by r1"
Command = New SqlCommand(SqlString, Connection)
Command.Connection.Open()
DR = Command.ExecuteReader
While DR.Read
Captions.Add(Convert.ToString(DR.GetString(0)))
Values.Add(Convert.ToInt16(DR("cc")))
Item_Count += 1
SumNum = SumNum + Convert.ToInt16(DR("cc"))
End While
DR.Close()
Command.Connection.Close()
End Sub
Sub DisplayPieChart()
'定义显示的字体
Dim FontLeg As Font
Dim FontTitle As Font
FontLeg = New Font("宋体", 10)
FontTitle = New Font("宋体", 15)
'确定图片的大小,及其各个元素的大小,宽度都一样
Dim ImageWidth As Integer
Dim ImageHeight As Integer
Dim ChartHeight As Integer
Dim LegendHeight As Integer
Dim TitleHeight As Integer
'画出底图
Dim WjhBitmap As Bitmap
Dim WjhGraphic As Graphics
Dim WjhBrush As Brush
WjhBitmap = New Bitmap(ImageWidth, ImageHeight)
WjhGraphic = Graphics.FromImage(WjhBitmap)
WjhBrush = New SolidBrush(Color.Black)
WjhGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, ImageWidth, ImageHeight)
' 画出饼图
Dim PieRec As Rectangle
PieRec = New Rectangle(0, TitleHeight, ImageWidth, ChartHeight)
Dim CurrentDegree As Single = 0.0F
Dim I As Integer
For I = 0 To Item_Count - 1
WjhGraphic.FillPie(New SolidBrush(GetColor(I)), PieRec, CurrentDegree, Convert.ToSingle(Values(I) / SumNum * 360))
CurrentDegree = CurrentDegree + Convert.ToSingle(Values(I) / SumNum * 360)
Next
'画出标题
Dim TitleRecF As RectangleF
TitleRecF = New RectangleF(0, 0, ImageWidth, TitleHeight)
Dim WjhFormat As StringFormat = New StringFormat()
WjhFormat.Alignment = StringAlignment.Center
WjhFormat.LineAlignment = StringAlignment.Center
WjhGraphic.DrawString("统计结果", FontTitle, WjhBrush, TitleRecF, WjhFormat)
'画出图例
WjhGraphic.DrawRectangle(New Pen(Color.Black, 2), 0, ImageHeight - LegendHeight, ImageWidth, LegendHeight)
For I = 0 To Item_Count - 1
WjhGraphic.FillRectangle(New SolidBrush(GetColor(I)), 5, ImageHeight - LegendHeight + FontLeg.Height * I + 5, 10, 10)
WjhGraphic.DrawString(Captions(I) + " ---- " + Convert.ToString(Values(I)), FontLeg, WjhBrush, 20, ImageHeight - LegendHeight + FontLeg.Height * I + 1)
Next
'画总计
WjhGraphic.DrawString("总计: " + Str(SumNum), FontLeg, WjhBrush, 5, ImageHeight - FontLeg.Height - 5)
WjhBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
End Sub
End Class