在ASP.net中使用OWC繪制統(tǒng)計圖表
2024-07-10 12:58:02
供稿:網(wǎng)友
在使用asp.net進行中,經(jīng)常需要將各種統(tǒng)計數(shù)據(jù)以圖形的方式顯示出來。如果僅僅是柱狀圖,可以采用畫表格或者將某種特定顏色的gif圖像縮放寬度和高度的方法來表示,許多投票程序多采用這種方法。但如果要求輸出結果是餅狀圖或者是連續(xù)的波形圖,就有些困難了。本文特向大家介紹使用owc圖形組件輕松實現(xiàn)繪制統(tǒng)計圖表的方法。
owc(microsoft office web components)是 microsoft office 使用的數(shù)據(jù)綁定 activex 控件,用于向 web 頁添加圖表功能。owc支持microsoft excel 2000中大部分的二維圖表(如折線圖、柱形圖、股價圖等)和極坐標圖表(如餅圖和雷達圖),并支持組合圖表,如兩軸線-柱圖,數(shù)據(jù)表會隨同圖表發(fā)布,圖表隨著數(shù)據(jù)的變化而改變。owc能將處理結果做為標準gif輸出并下載到瀏覽器中顯示。
首先,使用adodb.recordset讀出數(shù)據(jù)集合,并與owc組件進行數(shù)據(jù)綁定:
dim owcchartspace as owc.chartspace = new owc.chartspace()
dim owcchart as owc.wcchart = owcchartspace.charts.add
dim connado as new adodb.connection()
dim recordsetado as new adodb.recordset()
dim connectionstring as string
connectionstring = "provider=microsoft.jet.oledb.4.0;" & _
"data source=" & server.mappath("grades.mdb")
connado.open(connectionstring)
recordsetado.activeconnection = connado
recordsetado.cursortype = adodb.cursortypeenum.adopenstatic
recordsetado.cursorlocation = adodb.cursorlocationenum.aduseclient
dim strsql as string
strsql = "select city, month, temperature from test order by city,ids"
recordsetado.open(strsql, connado)
owcchartspace.datasource = recordsetado
然后,指定owc的顯示類型:
owcchart.type = owc.chartcharttypeenum.chcharttypesmoothlinemarkers
在運行時向owc中輸入數(shù)據(jù)有多種方法。所有這些方法都要用到 setdata 方法來真正將數(shù)據(jù)寫入 chart 組件,因此將詳細介紹此方法。setdata 方法應用于 wcchart、wcerrorbars 和 wcseries 對象并能向這三種對象輸入數(shù)據(jù)。
setdata 方法有三個參數(shù):dimension、datasourceindex 和 datareference。dimension 參數(shù)引用圖表中被填充數(shù)據(jù)的一部分??捎玫木S度常數(shù)為 seriesnames、categories、values、yvalues、xvalues、openvalues、closevalues、highvalues、lowvalues、bubblevalues、rvalues 和 thetavalues。這些常數(shù)每一種都引用了圖表的一部分;例如,seriesnames 常數(shù)引用了每個序列名,openvalues 和 closevalues 常數(shù)引用股票圖的開盤價和收盤價等等。每個常數(shù)都是作為諸如 chdimseriesnames、chdimcategories、chdimvalues等窗體中的枚舉常數(shù)而傳遞給該方法的。
這里,我們使用setdata 方法給owc賦值:
owcchart.setdata(owc.chartdimensionsenum.chdimseriesnames, 0, "city")
dim owcseries as owc.wcseries
for each owcseries in owcchart.seriescollection
owcseries.setdata(owc.chartdimensionsenum.chdimcategories, 0, "month")
owcseries.setdata(owc.chartdimensionsenum.chdimvalues, 0, "temperature")
next
最后,將owc處理結果轉換成gif圖象輸出到瀏覽器中顯示:
randomize()
dim nfilenamesuffix as integer
dim sfilenamesuffix as string
nfilenamesuffix = 100000 * rnd()
sfilenamesuffix = system.convert.tostring(nfilenamesuffix)
owcchartspace.exportpicture(mappath("owc/price_")
+ sfilenamesuffix + ".gif", "gif", 800, 600)
image1.imageurl = "owc/price_" + sfilenamesuffix + ".gif"
如果變換owc的顯示類型,即修改一下owcchart.type,使之變成:
owcchart.type = owc.chartcharttypeenum.chcharttypecolumnclustered
在asp.net中使用owc可以幫助我們快速構造統(tǒng)計圖表,而且由于owc生成的結果是gif圖象,可以兼容客戶端所有版本的瀏覽器,適用范圍很廣。本程序在windows 2000 server、iis5.0和ie6.0環(huán)境下運行成功。