我们工作中经常需要将数据转化成柱状图,饼图等,以方便直观的分析数据, 这里给大家介绍一个ASP中制作饼图、柱状图的组件:csDrawGraph, csdgt.zip ,因为是组件,所以我们在使用之前需要用REGSVR32.EXE 注册一下,csDrawGraph,可以在ASP中创建饼图,柱状图以及线图,其支持的格式有GIF, PNG, JPG and BMP.
chartdemo.asp
1@ language=vbscript
1<html>
2<head>
3<title>csDrawGraph Demonstration</title>
4</head>
5<body bgcolor="#FFFFFF">
6<p>This simple demonstration shows two graphs using the same data. The first is
7a bar chart:</p>
8<p align="center"><img height="300" src="chartimages.asp?Type=Bar" width="400"/>
9</p>
10<p align="left">The second is a pie chart. The background colour is set to light
11grey to show the overall size of the image.</p>
12<p align="center"><img height="300" src="chartimages.asp?Type=Pie" width="400"/>
13</p>
14</body>
15</html>
chartimages.asp
1@ language=vbscript
1
2Response.Expires = 0
3Response.Buffer = true
4Response.Clear
5Response.ContentType = "Image/Gif"
6
7Set Chart = Server.CreateObject("csDrawGraphTrial.Draw")
8
9
10Chart.AddData "NO> 1", 17, "ff0000"
11Chart.AddData "NO> 2", 28, "00ff00"
12Chart.AddData "NO> 3", 5, "0000ff"
13
14If Request.QueryString("Type") = "Pie" Then
15Chart.Title = "Sample Pie Chart"
16Chart.BGColor = "eeeeee"
17Chart.LabelBGColor = "eeeeee"
18Chart.TitleBGColor = "eeeeee"
19Response.BinaryWrite Chart.GifPie
20Else
21Chart.Title = "Sample Bar Chart"
22Response.BinaryWrite Chart.GifBar
23End If
24
25Response.End
程序很简单,再些不详细说明,下面看一个将数据库中的数据转换到图表的例子:
lines.asp:
1<html>
2<head>
3<title>Line graph showing all the results</title>
4</head>
5<body>
6<table align="center" width="400">
7<tr><td colspan="4"><img height="300" src="gif_lines.asp" width="400"/></td></tr>
8</table>
9<p>Links to the other result pages:</p>
10<p><a href="barsbyday.asp">Bar chart showing all results for any one day</a>.</p>
11<p><a href="barsbycolour.asp">Bar charts showing results for each colour separately</a>.</p>
12</body>
13</html>
gif_lines.asp:
1@ language=vbscript
1
2'利用数据库中的数据生成线图。
3'根据4个不同的值分别生成4条线。
4'在X轴上显示星期的名称。
5
6Response.Expires = 0
7Response.Buffer = true
8Response.Clear
9
10'利用下面的语句创建chart对象,版本不同会有所差异。
11'Set Chart = Server.CreateObject("csDrawGraph.Draw")
12Set Chart = Server.CreateObject("csDrawGraphTrial.Draw")
13
14ConnectionString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & _
15Server.Mappath("data.mdb")
16Set DBConn = Server.CreateObject("ADODB.Connection")
17DBConn.Open ConnectionString
18Set RS = Server.CreateObject("ADODB.Recordset")
19SQL = "SELECT * FROM Table1 ORDER BY Day"
20RS.Open SQL, DBConn
21
22While Not RS.Eof
23Chart.AddPoint CInt(RS("Day")), CInt(RS("Red")), "ff0000", "Red"
24Chart.AddPoint CInt(RS("Day")), CInt(RS("Blue")), "0000ff", "Blue"
25Chart.AddPoint CInt(RS("Day")), CInt(RS("Green")), "00ff00", "Green"
26Chart.AddPoint CInt(RS("Day")), CInt(RS("Yellow")), "ffff00", "Yellow"
27Chart.AddXValue CInt(RS("Day")), RS("DayName")
28RS.MoveNext
29Wend
30
31'关闭数据库连接
32RS.Close
33DBConn.Close
34
35'下面设置组件属性
36'X轴坐标从1开始而不是0。(XOffset = 1)
37
38Chart.Title = "All the combined results"
39Chart.TitleX = 100
40Chart.YAxisText = "Total for each day"
41Chart.OriginY = 220
42Chart.XOffset = 1
43Chart.XTop = 7
44Chart.XGrad = 1
45Chart.UseXAxisLabels = true
46Chart.LineWidth = 2
47Chart.PointSize = 3
48Chart.PointStyle = 1
49
50'最后图片以GIF格式发送到浏览器
51Response.ContentType = "image/gif"
52Response.BinaryWrite Chart.GIFLine
53Response.End