Microsoft KB Archive/286212

= HOWTO: Use an XML DataSource with the Office Chart Component =

Article ID: 286212

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Office Web Components

-



This article was previously published under Q286212



SUMMARY
This article illustrates how you can bind the Office Chart component to an ActiveX Data Objects (ADO) recordset that is persisted in Extensible Markup Language (XML).



MORE INFORMATION
You can bind to a recordset that is persisted in XML in one of two ways:


 * Use the Microsoft OLEDB Persistence Provider to bind to an XML stream that contains the persisted recordset. This technique is useful when the XML is generated with Active Server Pages (ASP) or when the XML is stored in a file. -or-


 * Set the DataSource property for the ChartSpace to an ADO recordset that is opened from an ADO stream that contains the XML data. You can use this approach to access a persisted recordset that is stored in an XML data island on the same Web page that hosts the Chart component.

Both of these techniques are demonstrated in the following samples.

Using XML Data Generated with ASP for a Chart DataSource
  Save the following code as GetXMLData.asp in the virtual root folder of your Web server. By default, the virtual root folder is C:\Inetpub\Wwwroot. If needed, modify the path to the sample Northwind.mdb Access database to match your installation of Office. <% Language=&quot;VBScript&quot; %>

<%   Response.ContentType = &quot;text/xml&quot; Response.Buffer = true Dim oRS, XMLDom Set oRS = CreateObject(&quot;ADODB.Recordset&quot;) oRS.Open &quot;select * from [Category Sales for 1997]&quot;, _ &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _ &quot;Data Source=c:\program files\microsoft office\office10\samples\northwind.mdb&quot;, 3, 1 set XMLDom = CreateObject(&quot;MSXML.DOMDocument&quot;) oRS.Save XMLDom, 1 Response.Write XMLDom.XML oRS.Close

Response.Flush Response.End %>                     Save the following HTML code as ChartXML1.htm in the virtual root folder of your Web server. Change YourWebServer in the script to the name of your Web server.





'Bind the chart to the recordset. Dim c  Set c = CSpace.Constants CSpace.ConnectionString = &quot;Provider=MSPersist&quot; CSpace.CommandText = &quot;http://YourWebServer/getxmldata.asp&quot; CSpace.HasMultipleCharts = False CSpace.PlotAllAggregates = c.chPlotAggregatesSeries CSpace.SetData c.chDimCategories, c.chDataBound, &quot;CategoryName&quot; CSpace.SetData c.chDimValues, c.chDataBound, &quot;CategorySales&quot;

 Start Microsoft Internet Explorer and browse to http://YourWebServer/ChartXML1.htm, where YourWebServer is the name of your Web server. When the Web page loads, the script binds the chart to the XML stream that is returned from GetXMLData.asp. The resulting chart contains one series that plots category sales information from Northwind.mdb.

Using an XML Data Island for a Chart DataSource
  Save the following code as ChartXML2.htm:



<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'> <s:Schema id='RowsetSchema'> <s:ElementType name='row' content='eltOnly'> <s:AttributeType name='CategoryName' rs:number='1' rs:maydefer='true' rs:writeunknown='true'> <s:datatype dt:type='string' dt:maxLength='15'/> </s:AttributeType> <s:AttributeType name='CategorySales' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'> <s:datatype dt:type='number' rs:dbtype='currency' dt:maxLength='8' rs:precision='19' rs:fixedlength='true'/> </s:AttributeType> <s:extends type='rs:rowbase'/> </s:ElementType> </s:Schema> <rs:data> <z:row CategoryName='Beverages' CategorySales='102074.29'/> <z:row CategoryName='Condiments' CategorySales='55277.56'/> <z:row CategoryName='Confections' CategorySales='80894.11'/> <z:row CategoryName='Dairy Products' CategorySales='114749.75'/> <z:row CategoryName='Grains/Cereals' CategorySales='55948.82'/> <z:row CategoryName='Meat/Poultry' CategorySales='81338.06'/> <z:row CategoryName='Produce' CategorySales='53019.98'/> <z:row CategoryName='Seafood' CategorySales='65544.19'/> </rs:data>

<script language=&quot;VBScript&quot;>

'Obtain a string that represents the XML data island. Dim oXML, sXML Set oXML = document.body.getElementsByTagName(&quot;xml&quot;) sXML = oXML(0).OuterHTML

'Load the XML &quot;string&quot; into an ADO recordset. Dim oRS, oStream Set oRS = CreateObject(&quot;ADODB.Recordset&quot;) Set oStream = CreateObject(&quot;ADODB.Stream&quot;) oStream.Open oStream.WriteText sXML oStream.Position = 0 oRS.Open oStream

'Bind the chart to the recordset. Dim c  Set c = CSpace.Constants Set CSpace.DataSource = oRS CSpace.HasMultipleCharts = False CSpace.PlotAllAggregates = c.chPlotAggregatesSeries CSpace.SetData c.chDimCategories, c.chDataBound, &quot;CategoryName&quot; CSpace.SetData c.chDimValues, c.chDataBound, &quot;CategorySales&quot;

</li> Start Internet Explorer and browse to ChartXML2.htm. When the Web page loads, the script binds the chart to the recordset that is persisted in the XML data island and plots one series for category sales.</li></ol>

<div class="references_section">