天天看点

OWC资料收集-如何将 Office 图表组件绑定到数据透视表 如何将 Office 图表组件绑定到数据透视表

如何将 Office 图表组件绑定到数据透视表

<script type="text/javascript">function loadTOCNode(){}</script> 注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。

文章编号 : 286320
最后修改 : 2006年10月23日
修订 : 4.3

概要

<script type="text/javascript">loadTOCNode(1, 'summary');</script> 本文演示绑如何 Office 图表组件在运行时定到 Office PivotTable 组件。 示例代码使用 FoodMart 示例附带用于 Microsoft SQL Server 7.0 和 SQL Server 2000 分析服务数据。

回到顶端

更多信息

<script type="text/javascript">loadTOCNode(1, 'moreinformation');</script> 绑 Chart 组件以便进行绘制数据透视表中显示总计定到数据透视。 当图表绑定到数据透视, 展开或折叠成员以显示更多或更少详细 ChartSpace 中透视轴上。 同样, 筛选, 透视坐标轴上成员时数据绑定 ChartSpace 中自动绘制反映更改。

默认情况下, 绑定 Chartspace 显示字段按钮和放置区域, 使您能够修改布局和内容 chart(s) 以相同的方式, 就像数据透视。 ChartSpace 中都有的拖放区域随图表类型应用 ; 例如, 放区域进行群集条形图与饼图的那些。 是由字段按钮和拖放区域是否出现在 ChartSpace 的 ChartSpace 对象 DisplayFieldButtons 属性。

以下示例代码演示了连接到 SQL Server FoodMart 示例数据库中数据绑定到数据透视图表。 代码显示您在运行时如何构建数据透视表 ; 它还说明如何通过 HasMultipleCharts 属性、 PlotAllAggregates 属性和 ChartSpace 对象的 SetData 方法数据中是绘制绑定图表中方式可操作。 有关使用这些属性和方法绑定图表, 请单击下列文章编号以查看 Microsoft 知识库中相应: 288907 (http://support.microsoft.com/kb/288907/) 绑 INFO: 定到数据源 Office XP 图表组件

示例代码

<script type="text/javascript">loadTOCNode(3, 'moreinformation');</script>

1. 以下代码粘贴到任何文本编辑器如记事本并将文件保存为 PivotChart.htm。 修改作业对于 sServerName 变量到名为您的 SQLServer。
<html>

<body>
<p align="Center">
<button id="btnOneChart" style="width:200">All Totals On One Chart</button>&#xa0;&#xa0;
<button id="btnMultiChart" style="width:200">Each Total on a Separate Chart</button>&#xa0;&#xa0;
<button id="btnBreakOut" style="width:200">Break Out Series into Quarters</button>
<br/><br/>
<object classid="clsid:0002E556-0000-0000-C000-000000000046" id="CSpace" width="95%" height="55%">
</object>
<br/><br/>
<object classid="clsid:0002E552-0000-0000-C000-000000000046" id="PTable" width="95%" height="35%">
<param name="AutoFit" value="False"/>
</object>
</p>
<script language="VBScript">

Dim c
Dim sServerName
Set c = CSpace.Constants
sServerName = "YourSQLServer"
btnBreakOut.disabled = True
BuildPivot

Function BuildPivot()
    
  'Connect the PivotTable to the data source and bind the chart to the PivotTable.
   PTable.ConnectionString = "Provider=msolap;Initial Catalog=FoodMart 2000;Data Source=" & sServerName
   PTable.DataMember = "Sales"
   Set CSpace.DataSource = PTable

   'Build the PivotTable.
   Set oView = PTable.ActiveView
   With oView

      'Add Dimensions to the column and row axes.
      .ColumnAxis.InsertFieldSet .FieldSets("Time")
      .RowAxis.InsertFieldSet .FieldSets("Product")

      'Add three totals to the data area.
      .DataAxis.InsertTotal .Totals("Store Sales")
      .Totals("Store Sales").NumberFormat = "_($* #,##0_)"
      .DataAxis.InsertTotal .Totals("Profit")
      .Totals("Profit").NumberFormat = "_($* #,##0_)"
      .DataAxis.InsertTotal .Totals("Store Cost")
      .Totals("Store Cost").NumberFormat = "_($* #,##0_)"
      
      'Expand the Year on the Column axis so that Quarterly information is displayed.
      .Fieldsets("Time").Fields("Year").Expanded = True
      .Fieldsets("Product").Fields("Product Family").Expanded = True

      'Hide the Product Category, SubCategory, Brand Name, and Product
      'Name in the Product hierarchy so that those levels do not appear on
      'the report.
      .FieldSets("Product").Fields("Product Category").IsIncluded=False
      .FieldSets("Product").Fields("Product Subcategory").IsIncluded=False
      .FieldSets("Product").Fields("Brand Name").IsIncluded = False
      .FieldSets("Product").Fields("Product Name").IsIncluded = False
      
      'Hide the Month in the Time hierarchy so that it does not appear
      'on the report.
      .Fieldsets("Time").Fields("Month").IsIncluded=False

      'Remove the Product Department subtotals that would appear when 
      'expanding a Product Family member.
      .Fieldsets("Product").Fields("Product Department").SubTotals(1) = False
      
      'Filter the row and column axes to limit the data shown in the PivotTable.
      .RowAxis.FieldSets("Product").Fields(0).IncludedMembers = Array("Drink")
      .ColumnAxis.FieldSets("Time").Fields("Quarter").IncludedMembers = Array("Q1", "Q2")

    End With    
   
   CSpace.Charts(0).Type = c.chChartTypeBarClustered
   CSpace.Border.Color = c.chColorNone
   
End Function

Function btnOneChart_OnClick()
    'Show totals as series on one chart.  
    BuildPivot
    CSpace.HasMultipleCharts = False  
    CSpace.PlotAllAggregates = c.chPlotAggregatesSeries
    CSpace.HasChartSpaceLegend = True  
    btnBreakOut.disabled = False
End Function

Function btnMultiChart_OnClick()
    'Each total on a separate chart.
    BuildPivot
    CSpace.HasMultipleCharts = True
    CSpace.PlotAllAggregates = c.chPlotAggregatesCharts
    CSpace.HasChartSpaceLegend = False  
    CSpace.ChartWrapCount = 3
    CSpace.HasChartSpaceLegend = True     
    NormalizeAxes
    btnBreakOut.disabled = False
End Function

Function btnBreakOut_OnClick()
    CSpace.HasMultipleCharts = True  
    CSpace.SetData c.chDimCharts, 0, PTable.ActiveView.ColumnAxis.FieldSets("Time").Fields("Quarter")
    CSpace.HasChartSpaceLegend = False      
    NormalizeAxes
    btnBreakOut.disabled = True
End Function

Function NormalizeAxes()
   'Normalize the maximum for the value axis of all charts in the 
   'ChartSpace.
   If CSpace.Charts.Count>0 Then
      Dim ValueMax
      For each oCht in CSpace.Charts
         max = oCht.Axes(c.chAxisPositionValue).Scaling.Maximum
         If ValueMax < max Then ValueMax = max
      Next   
      For each oCht in CSpace.Charts
         oCht.Axes(c.chAxisPositionValue).Scaling.Maximum = ValueMax
      Next
   End If
End Function

</script>

</html>
					      
注意 对于上述代码, classid 属性指到 OfficeXP 组件。 使用以下替换 classid 以添加对 Office 2003 组件, 引用:
替换 CSpace 对象 classid 与 C000 0000 0002E55D - 000000000046 - - - 0000
替换 PTable 对象 classid 与 C000 0000 0002E55A - 000000000046 - - - 0000
2. 启动 MicrosoftInternetExplorer 并浏览到 PivotChart.htm。 InternetExplorer 呈现包含数据透视表和图表 Web页。 以便它包含行和列坐标轴上筛选数据和数据轴上总计页面加载, 时脚本生成数据透视表。 绑初始脚本也定到数据透视表 ChartSpace ; 因绑定, Chartspace 显示字段按钮和除去区域以及没有 ChartSpace 中一个图表而没有数据还是绘制。
3. 单击 所有汇总 On 一个图表 以生成数据透视表并将其绑定到 ChartSpace。 结果是 Chartspace 中单个图表。 作为一系列图表中绘制每个数据透视表中显示总计。 在数据透视表, Q 1 和 Q2 显示总计用于 StoreSales、 Profit 和存储成本 ; 因此, 图表包含六个系列。
4. 单击 单独的图表上 ForEach Total 。 现在包含三个图表 ChartSpace: 一个用于 StoreSales、 对于 Profit, 一个图表和另一个用于存储成本图表。 为 Q 1 和一系列用于 Q2 各个图表包含系列。
5. 单击再次以创建一个图表有六个系列 All Totals On 一个图表 。 然后单击 插入 Quarters 中断不足系列 。 这导致两个图表: 为 Q 1 和另一个用于 Q2 一个图表。 每个图表包含一系列对于各个汇总每个图表 (也就是说, 三系列)。
6. 单击 每 On 单独的图表总 以创建三个图表 (一个图表对于各个汇总)。 然后单击 插入 Quarters 中断不足系列 。 因为总计已被分为单独绘制为 Q 1 和 Q2 ChartSpace 现在包含六个图表。

(c) Microsoft Corporation 2001, 保留所有权利。 由 Lori B, Turner MicrosoftCorporation 供稿。

回到顶端

参考

<script type="text/javascript">loadTOCNode(1, 'references');</script> 有关详细信息, 请参阅下列 Microsoft Web 站点 使用 MicrosoftOfficeWebComponents

http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

MicrosoftOffice 开发中心

http://msdn.microsoft.com/office/ (http://msdn.microsoft.com/office/)

继续阅读