In addition to using the Microsoft Office Chart Web Component as a Component Object Model (COM) control hosted on a form, it is possible to use the Chart Web Component as a non-visible, in-memory object. This article illustrates how you can use the Chart Web Component on a server to create a chart represented as a Graphics Interchange Format (GIF) image. You can implement the strategy discussed in this article to generate chart images that can be used across the Internet or in enterprises with heterogeneous client desktops.
With the Chart Web Component, you can create charts using data from various sources, such as arrays, a databases, spreadsheets, or any custom data sources. Once you create a chart, you can use the ExportPicture method of the Chart Component to generate a GIF image of that chart.
To accomplish this, you can modify the global.asa file and create an ASP page with the code illustrated in the following steps.
Steps to Create Project
- Start Microsoft Visual InterDev.
- Create a new Web Project called ServerChart and click the Next button.
- Type in the server that is to be used for this Web Project.
- Click Finish to create the Web Project.
- Right-click your project directory on the server (typically this is C:\Inetpub\wwwroot\ServerChart), select Properties and then click the Security tab.
- Click on Permissions, and add the following directory permissions:
IUSR_SERVERNAME : Read, Write, Execute and Delete
Creator : Read, Write, Execute and Delete
- Click OK to set the permissions.
- In the Visual InterDev project, right-click the global.asa file and select Get Working Copy.
- Modify the global.asa file to contain the following script:
<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 = 1End SubSub 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 NextEnd Sub</SCRIPT>
- Click on the Project menu, select Add Web Item, and then select Active Server Page. Name the page chart.asp.
- Modify the script in chart.asp to contain the following:
<%@ 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 valuenData = Request.QueryString("sOrg")'When the page loads the first time, set ndata to 5if len(nData) = 0 then nData = 5' Generate random categories and values for the chart' These values can come from some existing data sourcefor i = 1 to 5 Categories(i) = "Machine" & CStr(i) Vals(i) = nData * Rnd(100)next' Create a Chart ObjectSet oChart = CreateObject("OWC.Chart")Set c = oChart.Constants' Set the different parameters for the ChartSpaceoChart.Border.Color = c.chColorNone' Get Organization number and use it to set the CaptionnOrg = nData/5sCaption = "Current Utilizations for Org"sCaption = sCaption & CStr(nOrg)' Add a chart and set parameters for the chartoChart.Charts.AddoChart.Charts(0).Type = oChart.Constants.chChartTypeColumnClusteredoChart.Charts(0).SeriesCollection.AddoChart.Charts(0).SeriesCollection(0).Caption = sCaptionoChart.Charts(0).SeriesCollection(0).SetData c.chDimCategories, c.chDataLiteral, CategoriesoChart.Charts(0).SeriesCollection(0).SetData c.chDimValues, c.chDataLiteral, ValsoChart.Charts(0).HasLegend = TrueoChart.Charts(0).HasTitle = True' Get a temporary filename to save chart in that filesFname = Session("FSO").GetTempName & session.SessionID & ".gif"' Export the chart to the temporary fileoChart.ExportPicture server.MapPath(sFname), "gif", 600, 512' Create a link to the generated fileResponse.Write "<img src='" & sFname & "'>"' Store the file with its path in the session object for cleanupSession("sTempFile" & Session("n")) = Server.MapPath(sFname)' Increment the number of filesSession("n") = Session("n") + 1%></body></html>
- Save the project.
- Right-click chart.asp in the Project Explorer and select View in browser.
The page appears with a chart containing CPU utilizations for various computers. By selecting different organizations, you can view different utilizations.
When you create multiple charts based on different sets of data, each chart must be saved as a unique GIF file. In this sample, the Scripting Run-time library's FileSystemObject generates a temporary file for the GIF image. The file is placed in the same folder as the ASP page so that you can use the MapPath method of the Session object to get the location of the temporary file. The filename is saved as a session variable so that it is deleted when the session ends.
Note that IIS executes the Session_OnEnd subroutine whenever the session has timed out. The session times out if the client has not requested a page within the timeout duration, which is set to one (1) minute at the beginning of the session.
Currently, the only filter available to the ExportPicture method is for "GIF" images.
The ExportPicture method has two arguments that allow you to indicate the dimensions in pixels of the generated chart. In the sample code provided, the width and height dimensions are hard-coded. Instead, you can use dimensions that your client specifies.
The OWC.Chart ProgID applies to Office Web Components 9.0. If you want to use version 10 of the components, change the ProgID from OWC.Chart to OWC10.ChartSpace or if you want to use version 11 of the components, change the ProgID from OWC.Chart to OWC11.ChartSpace.
Problems with Server-Side Use
You should be aware that the Office 2000 Web Components are not designed for server-side use, and you may encounter problems if you use the components on a server with a high number of concurrent user connections. Many of these issue are addressed in the Office XP Web Components and Office 2003 Web Components, and you should consider using the Office XP Web Components or Office 2003 Web Components for large-scale server-side solutions.
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
INFO: Limitations of Office 2000 Web Components When Used Server-Side
Programming Microsoft Office Web Components by Dave Stearns ISBN: 0-7356-0794-X
For additional information about using the Chart Web Component, see the following Knowledge Base articles:
How To Create a Combination Chart with the Chart Web Component
How To Use the Office Chart Web Component With VB
How To Use VBScript to Bind a Chart to a Spreadsheet Component