Microsoft KB Archive/249805

From BetaArchive Wiki

Article ID: 249805

Article Last Modified on 7/14/2004


  • 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


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

  1. 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
    <xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
            ' Write out the schema info
    <s:Schema id='RowsetSchema'>
        <s:ElementType name='row' content='eltOnly'>
            <s:attribute type='XValues'/>
            <s:attribute type='YValues'/>
            <s:extends type='rs:rowbase'/>
        <s:AttributeType name='XValues' rs:number='1' rs:nullable="true">
            <s:datatype dt:type='string' dt:maxLength='10'/>
        <s:AttributeType name='YValues' rs:number='2'>
            <s:datatype dt:type='i2' dt:maxLength='2' rs:precision='5' rs:fixedlength='true' rs:maybenull='false'/>
            ' 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
            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 "</xml>"
  2. Use Notepad.exe to create another file named Chart.htm in the same directory as GenerateData.asp. Add the following to Chart.htm:

    <TITLE>Sample Chart</TITLE>
    <H1>Chart based on XML Data</H1>
    <!-- OWC DSC Control -->
    <object classid="clsid:0002E530-0000-0000-C000-000000000046" id="dscSample">
    <!-- OWC Chart Control -->
    <OBJECT classid=clsid:0002E500-0000-0000-C000-000000000046 height=384 
    id=csSample style="HEIGHT: 75%; WIDTH: 100%" width=576>
    <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"
            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
        ' 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

    NOTE: In the above code change the classids according to the verion of Office installed on your computer.

  3. 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).

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.


For additional sample code using the Chart Web component, see the following articles in the Microsoft Knowledge Base:

235885 How To Use the Chart Web Component with Visual Basic

243192 How To Use VBScript to Bind a Chart to a Spreadsheet Component

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

For more information on the XML Persistence Format, see:

XML Integration Features in ADO 2.5.

Additional query words: xml, adPersistXML, RecordsetDef webchart

Keywords: kbhowto kbofficewebchart KB249805