[翻译] blong.著Roll your own ASP.NET Chart Control
[代码使用]
为了使用WebChart控件,你必须首先编译它。然后,引用到你的项目中。并在Toolbox上,添加WebChart控件。
在设计时加入控件是非常简单的,和其它普通ASP.NET控件一样。然而,你必须在Web.config文件中,插入下面的XML标志:
Code:
<SYSTEM.WEB>
<httpModules>
<add name= "WebChartImageStream" type= "blong.WebControls.WebChartImageStream, blong" />
</httpModules>
<SYSTEM.WEB />
<httpModules>
<add name= "WebChartImageStream" type= "blong.WebControls.WebChartImageStream, blong" />
</httpModules>
<SYSTEM.WEB />
下面的代码显示如何动态地创建一个饼状图:
Code:
Imports System.Data.SqlClient
Imports blong.WebControls
Protected Sub MakeChart()
' Define Objects
Dim Chart As WebChart
Dim i As Int32
Dim cn As New SqlConnection(Session.Item("strConn"))
Dim cmd As New SqlCommand("WebHitsPerMonth", cn)
Dim dReader As SqlDataReader
cn.Open()
cmd.CommandType = CommandType.StoredProcedure
dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
' Override Default Constructor
' Copyright Symbol: Hold ALT + 0681
Chart = New WebChart()
With Chart
.ID = "CodeChart"
.Type = WebChart.ChartType.Pie
' Override WebChartItems for Pie Data
Dim highVal As Single
Dim ExplodeIndex As Int32
While dReader.Read
If Not IsDBNull(dReader("HitYear")) Then
.WebChartItems.Add(New WebChartItem(dReader("HitMonth") & _
" " & dReader("HitYear"), dReader("HitMonthCount"), False))
If dReader("HitMonthCount") > highVal Then
highVal = dReader("HitMonthCount")
ExplodeIndex = i - 1
End If
End If
i += 1
End While
'Explode High Value
.WebChartItems(ExplodeIndex).Explode = True
.ShowLegend = True
.Diameter = WebChart.PieDiameter.Larger
.ExplodeOffset = 25
.Rotate = 70
.Thickness = WebChart.PieThickness.Medium
.Title = "My Run Time Chart"
'Specify output format
.Format = WebChart.ChartFormat.Png
End With
' Add control to web form
Me.Controls.Add(Chart)
End Sub
Imports blong.WebControls
Protected Sub MakeChart()
' Define Objects
Dim Chart As WebChart
Dim i As Int32
Dim cn As New SqlConnection(Session.Item("strConn"))
Dim cmd As New SqlCommand("WebHitsPerMonth", cn)
Dim dReader As SqlDataReader
cn.Open()
cmd.CommandType = CommandType.StoredProcedure
dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
' Override Default Constructor
' Copyright Symbol: Hold ALT + 0681
Chart = New WebChart()
With Chart
.ID = "CodeChart"
.Type = WebChart.ChartType.Pie
' Override WebChartItems for Pie Data
Dim highVal As Single
Dim ExplodeIndex As Int32
While dReader.Read
If Not IsDBNull(dReader("HitYear")) Then
.WebChartItems.Add(New WebChartItem(dReader("HitMonth") & _
" " & dReader("HitYear"), dReader("HitMonthCount"), False))
If dReader("HitMonthCount") > highVal Then
highVal = dReader("HitMonthCount")
ExplodeIndex = i - 1
End If
End If
i += 1
End While
'Explode High Value
.WebChartItems(ExplodeIndex).Explode = True
.ShowLegend = True
.Diameter = WebChart.PieDiameter.Larger
.ExplodeOffset = 25
.Rotate = 70
.Thickness = WebChart.PieThickness.Medium
.Title = "My Run Time Chart"
'Specify output format
.Format = WebChart.ChartFormat.Png
End With
' Add control to web form
Me.Controls.Add(Chart)
End Sub