除了將 Microsoft Office 圖表 Web 組件用作在窗體上承載的組件對象模型 (COM) 控件,還可以將圖表 Web
組件用作不可見的內(nèi)存中的對象。本文闡釋了如何在服務(wù)器上使用圖表 Web 組件創(chuàng)建表示為圖形交換格式 (GIF)
圖像的圖表。您可以執(zhí)行本文所論述的策略來生成可通過異類客戶端桌面跨 Internet 或在企業(yè)內(nèi)使用的圖表圖像。
回到頂端
通過圖表 Web 組件,可以使用來自不同來源(如數(shù)組、數(shù)據(jù)庫、電子表格或任何自定義數(shù)據(jù)源)的數(shù)據(jù)創(chuàng)建圖表。一旦創(chuàng)建了圖表,就可以使用圖表組件的 ExportPicture 方法來生成該圖表的 GIF 圖像。
要完成此操作,可以修改 global.asa 文件并用下列步驟中闡釋的代碼創(chuàng)建 ASP 頁。
回到頂端
創(chuàng)建項目的步驟
| 1. |
啟動 Microsoft Visual InterDev。 |
| 2. |
創(chuàng)建一個名為 ServerChart 的新 Web 項目,然后單擊下一步按鈕。 |
| 3. |
鍵入將用于此 Web 項目的服務(wù)器。 |
| 4. |
單擊完成以創(chuàng)建此 Web 項目。 |
| 5. |
右鍵單擊服務(wù)器中的項目目錄(通常為 C:\Inetpub\wwwroot\ServerChart),選擇屬性,然后單擊安全性選項卡。 |
| 6. |
單擊權(quán)限,然后添加以下目錄權(quán)限:
IUSR_SERVERNAME:讀取、寫入、執(zhí)行和刪除
創(chuàng)建者 :讀取、寫入、執(zhí)行和刪除
|
| 7. |
單擊確定以設(shè)置權(quán)限。 |
| 8. |
在該 Visual InterDev 項目中,右鍵單擊 global.asa 文件并選擇獲取工作副本。 |
| 9. |
修改 global.asa 文件以包含下面的腳本:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
‘ Create a FileSystemObject to provide files in the script
Set Session("FSO") = CreateObject("Scripting.FileSystemObject")
‘ Create a variable that has the number of files created in this session
Session("n") = 0
‘ Set timeout to be 1 minute
Session.Timeout = 1
End Sub
Sub Session_OnEnd
‘ Delete the files created in this session
Dim x
For x = 0 to Session("n")-1
Session("FSO").DeleteFile Session("sTempFile" & x), True
Next
End Sub
</SCRIPT>
|
| 10. |
單擊項目菜單,選擇添加 Web 項,然后選擇 Active Server Page。將該頁命名為 chart.asp。 |
| 11. |
修改 chart.asp 中的腳本以包含以下代碼:
<%@ language="vbscript" %>
<html>
<body>
<h1>Realtime CPU Utilization by Configurations</h1>
<FORM action="chart.asp" method=get name=frmChooseOrg>
<p> Select an Organization to see values for their machines:
<SELECT name=sOrg>
<OPTION SELECTED value= 5>Org1</OPTION>
<OPTION value= 10>Org2</OPTION>
<OPTION value= 15>Org3</OPTION>
<OPTION value= 20>Org4</OPTION>
</SELECT>
<INPUT type="submit" value="Go"></p>
</FORM>
<%
Dim oChart, c, Categories(5), Vals(5), i, sCaption, nData, nOrg
‘ Get the input value
nData = Request.QueryString("sOrg")
‘When the page loads the first time, set ndata to 5
if len(nData) = 0 then nData = 5
‘ Generate random categories and values for the chart
‘ These values can come from some existing data source
for i = 1 to 5
Categories(i) = "Machine" & CStr(i)
Vals(i) = nData * Rnd(100)
next
‘ Create a Chart Object
Set oChart = CreateObject("OWC.Chart")
Set c = oChart.Constants
‘ Set the different parameters for the ChartSpace
oChart.Border.Color = c.chColorNone
‘ Get Organization number and use it to set the Caption
nOrg = nData/5
sCaption = "Current Utilizations for Org"
sCaption = sCaption & CStr(nOrg)
‘ Add a chart and set parameters for the chart
oChart.Charts.Add
oChart.Charts(0).Type = oChart.Constants.chChartTypeColumnClustered
oChart.Charts(0).SeriesCollection.Add
oChart.Charts(0).SeriesCollection(0).Caption = sCaption
oChart.Charts(0).SeriesCollection(0).SetData c.chDimCategories, c.chDataLiteral, Categories
oChart.Charts(0).SeriesCollection(0).SetData c.chDimValues, c.chDataLiteral, Vals
oChart.Charts(0).HasLegend = True
oChart.Charts(0).HasTitle = True
‘ Get a temporary filename to save chart in that file
sFname = Session("FSO").GetTempName & session.SessionID & ".gif"
‘ Export the chart to the temporary file
oChart.ExportPicture server.MapPath(sFname), "gif", 600, 512
‘ Create a link to the generated file
Response.Write "<img src=‘" & sFname & "‘>"
‘ Store the file with its path in the session object for cleanup
Session("sTempFile" & Session("n")) = Server.MapPath(sFname)
‘ Increment the number of files
Session("n") = Session("n") + 1
%>
</body>
</html>
|
| 12. |
保存該項目。 |
| 13. |
在項目資源管理器中,右鍵單擊“chart.asp”并在瀏覽器中選擇查看。 |
將顯示一個頁面,其中具有一個包含不同計算機的 CPU 使用率的圖表。通過選擇不同的組織,可以查看不同的使用率。
根據(jù)不同的數(shù)據(jù)集創(chuàng)建多個圖表時,每個圖表都必須另存為唯一的 GIF 文件。在此示例中,Scripting Run-time 庫的
FileSystemObject 將為 GIF 圖像生成一個臨時文件。該文件位于 ASP 頁所在的同一文件夾中,以便您可以使用 Session
對象的 MapPath 方法獲取該臨時文件的位置。文件名將另存為一個會話變量,以便在會話結(jié)束時將其刪除。
請注意,只要會話超時,IIS 就會執(zhí)行 Session_OnEnd 子例程。如果客戶端在超時期限(在會話開始時設(shè)置為一 (1) 分鐘)內(nèi)未請求頁面,會話將超時。
其他說明
當(dāng)前,只有一個可用于 ExportPicture 方法的篩選器適用于“GIF”圖像。
ExportPicture 方法有兩個參數(shù),可允許用所生成的圖表的像素指示尺寸。在提供的示例代碼中,寬度和高度尺寸是硬編碼的。您可以改用客戶指定的尺寸。
OWC.Chart ProgID 適用于 Office Web Components 9.0。如果要使用組件的版本 10,請將 ProgID
從 OWC.Chart 更改為 OWC10.ChartSpace,或者如果要使用組件的版本 11,則將 ProgID 從 OWC.Chart
更改為 OWC11.ChartSpace。
在服務(wù)器端使用時出現(xiàn)的問題
您應(yīng)該知道,Office 2000 Web Components
不是針對服務(wù)器端使用而設(shè)計的,如果您在有大量并行用戶連接的服務(wù)器上使用這些組件,則可能會出現(xiàn)問題。其中許多問題已在 Office XP Web
Components 和 Office 2003 Web Components 中得到解決,對于大型服務(wù)器端解決方案,應(yīng)考慮使用 Office
XP Web Components 或 Office 2003 Web Components。
有關(guān)其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應(yīng)的文章:
317316 (http://support.microsoft.com/kb/317316/) INFO:Office 2000 Web Components 在服務(wù)器端使用時的限制
回到頂端
Microsoft Office Web Components 編程,Dave Stearns 著,ISBN:0-7356-0794-X
有關(guān)使用圖表 Web 組件的其他信息,請參見下面的知識庫文章:
240263 (http://support.microsoft.com/kb/240263/) 如何使用圖表 Web 組件創(chuàng)建組合圖表
235885 (http://support.microsoft.com/kb/235885/) 如何將圖表 Web 組件與 Visual Basic 一起使用
243192 (http://support.microsoft.com/kb/243192/) 如何使用 VBScript 將圖表綁定到電子表格組件