HOWTO: Use Server-Side Charting to Generate Charts Dynamically
ID: Q244049
|
The information in this article applies to:
-
Microsoft Office Chart Component 9.0
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.
- 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 = 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>
- 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 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>
- 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.
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.
© Microsoft Corporation 2000, All Rights Reserved. Contributions by Harsha Bennur, Microsoft Corporation
REFERENCES
Programming Microsoft Office Web Components by Dave Stearns ISBN: 0-7356-0794-X
For additional information about using the Chart Web Component, please click on the article numbers below to view the articles in the Microsoft Knowledge Base:
Q240263 HOWTO: Create a Combination Chart with the Chart Web Component
Q235885 HOWTO: Use the Office Chart Web Component With VB
Q243192 HOWTO: Use VBScript to Bind a Chart to a Spreadsheet Component
Additional query words:
owc realtime
Keywords : kbASP kbVBScript kbGrpDSO kbDSupport kbOfficeWebChart
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto
|