除了将 Microsoft Office 图表 Web 组件用作在窗体上承载的组件对象模型 (COM) 控件,还可以将图表 Web
组件用作不可见的内存中的对象。本文阐释了如何在服务器上使用图表 Web 组件创建表示为图形交换格式 (GIF)
图像的图表。您可以执行本文所论述的策略来生成可通过异类客户端桌面跨 Internet 或在企业内使用的图表图像。
回到顶端
通过图表 Web 组件,可以使用来自不同来源(如数组、数据库、电子表格或任何自定义数据源)的数据创建图表。一旦创建了图表,就可以使用图表组件的 ExportPicture 方法来生成该图表的 GIF 图像。
要完成此操作,可以修改 global.asa 文件并用下列步骤中阐释的代码创建 ASP 页。
回到顶端
创建项目的步骤
1. |
启动 Microsoft Visual InterDev。 |
2. |
创建一个名为 ServerChart 的新 Web 项目,然后单击下一步按钮。 |
3. |
键入将用于此 Web 项目的服务器。 |
4. |
单击完成以创建此 Web 项目。 |
5. |
右键单击服务器中的项目目录(通常为 C:\Inetpub\wwwroot\ServerChart),选择属性,然后单击安全性选项卡。 |
6. |
单击权限,然后添加以下目录权限:
IUSR_SERVERNAME:读取、写入、执行和删除
创建者 :读取、写入、执行和删除
|
7. |
单击确定以设置权限。 |
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 使用率的图表。通过选择不同的组织,可以查看不同的使用率。
根据不同的数据集创建多个图表时,每个图表都必须另存为唯一的 GIF 文件。在此示例中,Scripting Run-time 库的
FileSystemObject 将为 GIF 图像生成一个临时文件。该文件位于 ASP 页所在的同一文件夹中,以便您可以使用 Session
对象的 MapPath 方法获取该临时文件的位置。文件名将另存为一个会话变量,以便在会话结束时将其删除。
请注意,只要会话超时,IIS 就会执行 Session_OnEnd 子例程。如果客户端在超时期限(在会话开始时设置为一 (1) 分钟)内未请求页面,会话将超时。
其他说明
当前,只有一个可用于 ExportPicture 方法的筛选器适用于“GIF”图像。
ExportPicture 方法有两个参数,可允许用所生成的图表的像素指示尺寸。在提供的示例代码中,宽度和高度尺寸是硬编码的。您可以改用客户指定的尺寸。
OWC.Chart ProgID 适用于 Office Web Components 9.0。如果要使用组件的版本 10,请将 ProgID
从 OWC.Chart 更改为 OWC10.ChartSpace,或者如果要使用组件的版本 11,则将 ProgID 从 OWC.Chart
更改为 OWC11.ChartSpace。
在服务器端使用时出现的问题
您应该知道,Office 2000 Web Components
不是针对服务器端使用而设计的,如果您在有大量并行用户连接的服务器上使用这些组件,则可能会出现问题。其中许多问题已在 Office XP Web
Components 和 Office 2003 Web Components 中得到解决,对于大型服务器端解决方案,应考虑使用 Office
XP Web Components 或 Office 2003 Web Components。
有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
317316 (http://support.microsoft.com/kb/317316/) INFO:Office 2000 Web Components 在服务器端使用时的限制
回到顶端
Microsoft Office Web Components 编程,Dave Stearns 著,ISBN:0-7356-0794-X
有关使用图表 Web 组件的其他信息,请参见下面的知识库文章:
240263 (http://support.microsoft.com/kb/240263/) 如何使用图表 Web 组件创建组合图表
235885 (http://support.microsoft.com/kb/235885/) 如何将图表 Web 组件与 Visual Basic 一起使用
243192 (http://support.microsoft.com/kb/243192/) 如何使用 VBScript 将图表绑定到电子表格组件