Microsoft KB Archive/249805

= How To Use XML Data with the Chart Component =

Article ID: 249805

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft Office Chart Component 9.0
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 4.01 Service Pack 2

-



This article was previously published under Q249805



SUMMARY
This article illustrates how you can use the Microsoft Office Chart Component to plot data from an XML data stream. The Chart used in this sample is bound to a RecordsetDef in a Data Source Component. The RecordsetDef itself is created from Active Server Pages (ASP) script that uses the XML Rowset definition to return an XML data stream.



Steps to Create Sample
  Use Notepad.exe to create a file called GenerateData.asp in your IIS home directory (for example, inetpub\wwwroot), and add the following code to it: <%@ Language=VBScript %>

<%

' GenerateData.ASP '  Purpose: Generates data for the chart

Option Explicit

Response.Buffer = True

' Write out the XML-Data header information %> 

<%       ' Write out the schema info %>

          <s:datatype dt:type='i2' dt:maxLength='2' rs:precision='5' rs:fixedlength='true' rs:maybenull='false'/> </s:AttributeType> </s:Schema>

<%       ' Now write out some random data Dim nXVal         'Temp X value Dim anYValues        'Array of values for Y Axis Dim nct, nUpperbound, nLowerbound nUpperbound = 100 nLowerbound = 25 Randomize Response.Write "<rs:data>" For nct = 0 To 100 ' Start the row Response.Write "<z:row XValues=' Machine" ' Generate and write the X value Response.Write CInt(nct) & "'" ' Generate and wwrite the Y value Response.Write " YValues='" & CInt((nUpperbound - nLowerbound + 1) * Rnd + nLowerbound) & "'" ' Close the row tag Response.Write "/>" Next 'ct

' Close the data section Response.Write "</rs:data>" & vbcrlf ' Close the xml tag Response.Write " " %>                   </li> <li> Use Notepad.exe to create another file named Chart.htm in the same directory as GenerateData.asp. Add the following to Chart.htm: <HTML> <HEAD> <TITLE>Sample Chart</TITLE> </HEAD>

<BODY> <H1>Chart based on XML Data</H1>

<object classid="clsid:0002E530-0000-0000-C000-000000000046" id="dscSample">

<OBJECT classid=clsid:0002E500-0000-0000-C000-000000000046 height=384 id=csSample style="HEIGHT: 75%; WIDTH: 100%" width=576> </OBJECT>

<SCRIPT language=vbscript>

Sub Window_onLoad ' Initialize the DSC DSCInit dscSample ' Draw the chart DrawChart csSample, dscSample

End Sub

' Initializes the DSC by setting Connection String and RecordSetDef Sub DSCInit(dsc) ' Add a RecordsetDef with name ChartData to the dsc if len(dsc.ConnectionString) = 0 then dsc.ConnectionString = "provider=mspersist" dsc.RecordsetDefs.AddNew "GenerateData.ASP", _ dsc.Constants.dscCommandFile, "ChartData" else Window.status = "DSC ConnectionString is already set!" end if   End Sub

' Draws the chart using the RecordSetDef data Sub DrawChart(cspace, dsc) Dim c          'Constants object Dim cht        'Temp WCChart object Dim ser        'Temp WCSeries object Dim ax         'Temp WCAxis object Set c = cspace.Constants ' Clear the Chartspace cspace.Clear ' Load the chart data sources Dim cds        'Temp WCChartDataSource object

' Add a DataSource to the Chart and set it to be the dsc Set cds = cspace.ChartDataSources.Add Set cds.DataSource = dsc ' Set the Data Member to be the RecordsetDef cds.DataMember = "ChartData" cds.CacheSize = 400 ' Draw the Chart set cht = cspace.Charts.Add cht.Type = c.chChartTypeLineMarkers cht.HasLegend = True cht.Legend.Position = c.chLegendPositionTop cht.HasTitle = True cht.Title.Caption = "Machine Utilizations" ' Add a series set ser = cht.SeriesCollection.Add ser.Name = "Utilization(%)" ser.Caption = ser.Name ser.Marker.Size = 4 ' Set the Categories to the first field (YValues)in the ' RecordSetDef of the DataSource - dsc ser.SetData c.chDimCategories, 0, 0 ' Set the Values to the second field (XValues)in the ' RecordSetDef of the DataSource - dsc ser.SetData c.chDimValues, 0, 1

' Set the tick label spacing depending on the number of points plotted Set ax = cht.Axes(c.chAxisPositionBottom) ax.TickLabelSpacing = cht.SeriesCollection(0).Points.Count / 10 End Sub

</SCRIPT> </BODY> </HTML>

NOTE: In the above code change the classids according to the verion of Office installed on your computer. </li> <li>In Internet Explorer, type the URL to Chart.htm to view the results. If you created the file in your IIS home directory, you would use the URL http://MyServer/Chart.htm (replace MyServer with the name of your server).

Results: The Chart displays the XML data with Utilizations (values) plotted against the Machine names (categories).</li></ol>

Additional Notes
In order to view the XML data that is generated, navigate to GenerateData.asp in Internet Explorer and, once the page is loaded, click Source on the View menu.

Note that the data that the ASP script generates is the same data that would be returned using the Save method of an ADO Recordset when the PersistFormat member is adPersistXML.

<div class="references_section">