我們工作中經(jīng)常需要將數(shù)據(jù)轉(zhuǎn)化成柱狀圖,餅圖等,以方便直觀的分析數(shù)據(jù), 這里給大家介紹一個(gè)asp中制作餅圖、柱狀圖的組件:csdrawgraph,csdgt.zip,因?yàn)槭墙M件,所以我們在使用之前需要用regsvr32.exe 注冊一下,csdrawgraph,可以在asp中創(chuàng)建餅圖,柱狀圖以及線圖,其支持的格式有g(shù)if, png, jpg and bmp.
看看如下的柱狀圖,餅圖如何生成的例子:
[align=center][/align]
chartdemo.asp
<%@ language=vbscript %>
csdrawgraph demonstrationthis simple demonstration shows two graphs using the same data. the first is
a bar chart:
the second is a pie chart. the background colour is set to light
grey to show the overall size of the image.
chartimages.asp
<%@ language=vbscript %>
<%
response.expires = 0
response.buffer = true
response.clear
response.contenttype = "image/gif"
set chart = server.createobject("csdrawgraphtrial.draw")
chart.adddata "no> 1", 17, "ff0000"
chart.adddata "no> 2", 28, "00ff00"
chart.adddata "no> 3", 5, "0000ff"
if request.querystring("type") = "pie" then
chart.title = "sample pie chart"
chart.bgcolor = "eeeeee"
chart.labelbgcolor = "eeeeee"
chart.titlebgcolor = "eeeeee"
response.binarywrite chart.gifpie
else
chart.title = "sample bar chart"
response.binarywrite chart.gifbar
end if
response.end
%>
程序很簡單,再些不詳細(xì)說明,下面看一個(gè)將
數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)換到圖表的例子:
lines.asp:
line graph showing all the results
links to the other result pages:
bar chart showing all results for any one day.
bar charts showing results for each colour separately.
gif_lines.asp:
<%@ language=vbscript %>
<%
'利用數(shù)據(jù)庫中的數(shù)據(jù)生成線圖。
'根據(jù)4個(gè)不同的值分別生成4條線。
'在x軸上顯示星期的名稱。
response.expires = 0
response.buffer = true
response.clear
'利用下面的語句創(chuàng)建chart對象,版本不同會(huì)有所差異。
'set chart = server.createobject("csdrawgraph.draw")
set chart = server.createobject("csdrawgraphtrial.draw")
connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" & _
server.mappath("data.mdb")
set dbconn = server.createobject("adodb.connection")
dbconn.open connectionstring
set rs = server.createobject("adodb.recordset")
sql = "select * from table1 order by day"
rs.open sql, dbconn
while not rs.eof
chart.addpoint cint(rs("day")), cint(rs("red")), "ff0000", "red"
chart.addpoint cint(rs("day")), cint(rs("blue")), "0000ff", "blue"
chart.addpoint cint(rs("day")), cint(rs("green")), "00ff00", "green"
chart.addpoint cint(rs("day")), cint(rs("yellow")), "ffff00", "yellow"
chart.addxvalue cint(rs("day")), rs("dayname")
rs.movenext
wend
'關(guān)閉數(shù)據(jù)庫連接
rs.close
dbconn.close
'下面設(shè)置組件屬性
'x軸坐標(biāo)從1開始而不是0。(xoffset = 1)
chart.title = "all the combined results"
chart.titlex = 100
chart.yaxistext = "total for each day"
chart.originy = 220
chart.xoffset = 1
chart.xtop = 7
chart.xgrad = 1
chart.usexaxislabels = true
chart.linewidth = 2
chart.pointsize = 3
chart.pointstyle = 1
'最后圖片以gif格式發(fā)送到瀏覽器
response.contenttype = "image/gif"
response.binarywrite chart.gifline
response.end
%>
結(jié)果如圖所示:
[align=center][/align]
csdrawgraph的下載地址:http://chestysoft.com/download/csdgt.zip
該文章在 2010/7/3 14:23:21 編輯過