How To Use the PivotTable Office Web Component with VB (转载自微软)

原创 2004年07月21日 10:39:00


How To Use the Chart Web Component with Visual Basic

This article was previously published under Q235885


This article provides an overview of how you can use the Office Chart Web Component on a Visual Basic form to visualize related data. More specifically, the code in this article illustrates by example how to do the following:
  • Use array data for a chart.
  • Use an ADO recordset for chart data.
  • Use spreadsheet data for a chart.
  • Create multiple charts in a single chartspace.
  • Create a combination chart.-and-

  • Format different chart components such as axes, datalabels, series, plotareas and more.


Steps to Create Sample

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Click Components on the Project menu. Select Microsoft Office Web Components 9.0 and click OK.
  3. Add the following controls to the form with the properties indicated:

    Type Name Caption
    ChartSpace ChartSpace1 -
    DataSourceControl DataSourceControl1 -
    CommandButton Command1 Use Arrays
    CommandButton Command2 Use ADO Recordset
    CommandButton Command3 Use Spreadsheet

    NOTE: Size the ChartSpace control on your form so that it takes up as much of the form as possible while still allowing for the other controls on the form.
  4. Add the following code to the module of Form1:
    Private Sub Command1_Click() 'Create arrays for the x-values and the y-values Dim xValues As Variant, yValues1 As Variant, yValues2 As Variant xValues = Array("Beverages", "Condiments", "Confections", _ "Dairy Products", "Grains & Cereals", _ "Meat & Poultry", "Produce", "Seafood") yValues1 = Array(104737, 50952, 78128, 117797, 52902, 80160, 47491, _ 62435) yValues2 = Array(20000, 15000, 36000, 56000, 40000, 18000, 20000, _ 33000) 'Create a new chart Dim oChart As WCChart ChartSpace1.Clear ChartSpace1.Refresh Set oChart = ChartSpace1.Charts.Add 'Add a title to the chart oChart.HasTitle = True oChart.Title.Caption = "Sales Per Category" 'Add a series to the chart with the x-values and y-values 'from the arrays and set the series type to a column chart Dim oSeries As WCSeries Set oSeries = oChart.SeriesCollection.Add With oSeries .Caption = "1995" .SetData chDimCategories, chDataLiteral, xValues .SetData chDimValues, chDataLiteral, yValues1 .Type = chChartTypeColumnClustered End With 'Add another series to the chart with the x-values and y-values 'from the arrays and set the series type to a line chart Set oSeries = oChart.SeriesCollection.Add With oSeries .Caption = "1996" .SetData chDimCategories, chDataLiteral, xValues .SetData chDimValues, chDataLiteral, yValues2 .Type = chChartTypeLineMarkers End With 'Add a value axis to the right of the chart for the second series oChart.Axes.Add oChart.Axes(chAxisPositionLeft).Scaling, _ chAxisPositionRight, chValueAxis 'Format the Value Axes oChart.Axes(chAxisPositionLeft).NumberFormat = "$#,##0" oChart.Axes(chAxisPositionRight).NumberFormat = "0" oChart.Axes(chAxisPositionLeft).MajorUnit = 20000 oChart.Axes(chAxisPositionRight).MajorUnit = 20000 'Show the legend at the bottom of the chart oChart.HasLegend = True oChart.Legend.Position = chLegendPositionBottom End Sub Private Sub Command2_Click() 'Set up the DataSourceControl for the Chartspace Dim rsd As RecordsetDef DataSourceControl1.ConnectionString = _ "DRIVER={Microsoft Access Driver (*.mdb)}; " & _ "DBQ=C:/Program Files/Microsoft Visual Studio/VB98/nwind.mdb" Set rsd = DataSourceControl1.RecordsetDefs.AddNew( _ "Select * from [Category Sales for 1995]", 3) With ChartSpace1 .Clear .Refresh .DataSource = DataSourceControl1 .DataMember = rsd.Name End With 'This Chartspace will contain 2 charts. Make the layout so that the 'charts are positioned horizontally ChartSpace1.ChartLayout = chChartLayoutHorizontal 'Create a new bar chart from the query Dim oBarChart As WCChart Set oBarChart = ChartSpace1.Charts.Add With oBarChart .Type = chChartTypeBarClustered .SetData chDimCategories, 0, 0 'Categories are first field .SetData chDimValues, 0, 1 'Values are second field 'Format the value axis for the bar chart so that it 'shows values in thousands (i.e., 45000 displays as 45) and 'in increments of 25000. Remove the gridlines With .Axes(chAxisPositionBottom) .NumberFormat = "0," .MajorUnit = 25000 .HasMajorGridlines = False End With 'Change the color of the series and the plot area .SeriesCollection(0).Interior.Color = RGB(150, 0, 150) .PlotArea.Interior.Color = RGB(240, 240, 10) End With 'Create a new exploded pie chart from the query Dim oPieChart As WCChart Set oPieChart = ChartSpace1.Charts.Add With oPieChart .Type = chChartTypePie .SetData chDimCategories, 0, 0 'Categories are first field .SetData chDimValues, 0, 1 'Values are second field .SeriesCollection(0).Explosion = 20 'Add a legend to the bottom of the pie chart .HasLegend = True .Legend.Position = chLegendPositionBottom 'Add a title to the chart .HasTitle = True .Title.Caption = "Sales by Category for 1995" .Title.Font.Bold = True .Title.Font.Size = 11 'Make the chart width 50% the size of the bar chart's width .WidthRatio = 50 'Show data labels on the slices as percentages With .SeriesCollection(0).DataLabelsCollection.Add .HasValue = False .HasPercentage = True .Font.Size = 8 .Interior.Color = RGB(255, 255, 255) End With End With End Sub Private Sub Command3_Click() 'Dynamically add a spreadsheet control to the form Dim oSheet As Spreadsheet Me.Controls.Add "OWC.Spreadsheet", "Sheet" Set oSheet = Me!Sheet 'Fill the Sheet with data With oSheet .Range("A1:A10").Formula = "=Row()" .Range("B1:B10").Formula = "=A1^2" .Range("A12").Formula = "=Max(A1:A10)" .Range("B12").Formula = "=Max(B1:B10)" End With 'Create an xy-scatter chart using the data in the spreadsheet Dim oChart As WCChart With ChartSpace1 .Clear .Refresh .DataSource = oSheet.object Set oChart = .Charts.Add oChart.Type = chChartTypeScatterSmoothLineMarkers oChart.SetData chDimXValues, 0, "a1:a10" oChart.SetData chDimYValues, 0, "b1:b10" End With With oChart 'Display the Axes Titles and 'set the major units for the axes With .Axes(chAxisPositionBottom) .HasTitle = True .Title.Caption = "X" .Title.Font.Size = 8 .MajorUnit = 1 End With With .Axes(chAxisPositionLeft) .HasTitle = True .Title.Caption = "X Squared" .Title.Font.Size = 8 .MajorUnit = 10 End With 'Set the maximum and minimum axis values .Scalings(chDimXValues).Maximum = oSheet.Range("A12").Value .Scalings(chDimXValues).Minimum = 1 .Scalings(chDimYValues).Maximum = oSheet.Range("B12").Value 'Change the marker and line styles for the series With .SeriesCollection(0) .Marker.Style = chMarkerStyleDot .Marker.Size = 6 .Line.Weight = 1 .Line.Color = RGB(255, 0, 0) End With End With 'Remove the spreadsheet Me.Controls.Remove "Sheet" End Sub 
    NOTE: You might need to modify the connection string in the Command2_Click event so that it correctly refers to the path of NWIND.MDB, which is installed to the Visual Basic directory by default:
     DataSourceControl1.ConnectionString = _ "DRIVER={Microsoft Access Driver (*.mdb)}; " & _ "DBQ=C:/Program Files/Microsoft Visual Studio/VB98/nwind.mdb" 
  5. Click Properties on the Project menu. Select the Make tab, deselect Remove Information about Unused ActiveX Controls and click OK.
  6. Press the F5 key to run the program.
  7. Click the Use Arrays button. A combination chart with two value axes is created. The chart uses data from arrays.
  8. Click the Use ADO Recordset button. Two charts are created in the Chartspace; a bar chart and an exploded pie chart with data labels. The Chartspace uses a DataSourceControl for its data and the DataSourceControl references an Access database through an ADO connection.
  9. Click the Use Spreadsheet button. A scatter chart is created that uses a Spreadsheet control with formulas in the cells as its data. The Spreadsheet control is dynamically added and removed at run time.

The information in this article applies to:

  • Microsoft Office Chart Component 9.0, run time
Last Reviewed: 7/13/2004 (1.1)
Keywords: kbhowto KB235885



问题一、Swing使用Substance皮肤教程 请点链接刚开始遇到的问题基本都有
  • lidelin10
  • lidelin10
  • 2017年12月30日 14:52
  • 69

在线文档预览方案-office web apps

我只是转载一下,免得原作者删除后没得看了! 最近在做项目时,要在手机端实现在线文档预览的功能。于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览。这些方案没有具体实现代码,也没有...
  • yjclsx
  • yjclsx
  • 2016年05月24日 14:40
  • 4485

在线文档预览方案-office web apps

在线文档预览方案-office web apps   最近在做项目时,要在手机端实现在线文档预览的功能。于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览。这些方案没有具...
  • Gavid0124
  • Gavid0124
  • 2016年08月12日 08:57
  • 1720

微软提供的三种核心服务:Windows+Office 365+Azure

微软提供的三种核心服务:Windows+Office 365+Azure 微软目前是全球最大的电脑软件提供商,消费市场、企业市场和云计算是微软最关注的领域,也是整个行业未来关注的重点,对应微软的三种核...
  • yangzhenping
  • yangzhenping
  • 2014年11月12日 11:14
  • 3150


SetMsgByName (String, String) 设置字段值 GetMsgByName(String) 取得字段值 MsgTextClear() ...
  • u010452891
  • u010452891
  • 2014年10月23日 14:28
  • 2504

Web Office Apps 按照微软官方的例子部署后 仅能预览 excel 其他格式全都无法预览

最近在研究 Web Office App  部署,
  • lanbo_xiaolong
  • lanbo_xiaolong
  • 2014年11月06日 09:01
  • 2494

自定义开发的系统整合 office Web apps

其实网上有关office web app的整合已经有相关的文章了,典型的是如何整合Office Web Apps至自己开发的系统(一) 和如何整合Office Web Apps至自己开发的系统(二)...
  • jason_dct
  • jason_dct
  • 2017年01月18日 17:14
  • 2540


转自: 如果我有一小时的时间来背诵一段材料,我会花 55 分钟搞清楚为什么要背它、它能给我带来什么价值,然后再用 5 分钟时间去记住它。 ...
  • shellching
  • shellching
  • 2017年10月18日 17:40
  • 349

Keil C51编译及连接技术(覆盖技术OVERLAY) Keil C51编译及连接技术
  • chungle2011
  • chungle2011
  • 2014年12月20日 21:22
  • 4872

Office Web Apps所需证书的申请分配部署详解

最近看到博客中不少朋友都在说Office Web Apps证书相关的问题,为此在这里我主要的来跟大家一起交流下Office Web Apps证书的相关内容。从我们目前的部署来说,想必所有朋友都知道Of...
  • zhuifeng1024
  • zhuifeng1024
  • 2014年07月13日 00:22
  • 1325
您举报文章:How To Use the PivotTable Office Web Component with VB (转载自微软)