Microsoft KB Archive/244049

= How To Use Server-Side Charting to Generate Charts Dynamically =

Article ID: 244049

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Office Web Components
 * Microsoft Office Chart Component 9.0

-



This article was previously published under Q244049



SUMMARY
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.



MORE INFORMATION
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.</li> In the Visual InterDev project, right-click the global.asa file and select Get Working Copy.</li>  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 = 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> </li> Click on the Project menu, select Add Web Item, and then select Active Server Page. Name the page chart.asp .</li>  Modify the script in chart.asp to contain the following: <%@ language="vbscript" %> Realtime CPU Utilization by Configurations

<FORM action="chart.asp" method=get name=frmChooseOrg>

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">

</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

%>

</li> Save the project.</li> Right-click chart.asp in the Project Explorer and select View in browser.</li></ol>

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.

Additional Notes
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:

317316 INFO: Limitations of Office 2000 Web Components When Used Server-Side

<div class="references_section">