How to use the pivottable office web component with vb (reproduced from Microsoft)

zhaozj2021-02-16  74

Original address: http://support.microsoft.com/default.aspx? Scid = kb; EN-US; 235542

How To Use the Chart Web Component with Visual Basic

Suitable

THIS ARTICLE WAS Previously Published Under Q235885

SUMMARYThis 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.

More information

Steps to create Sample

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

TypeNameCaptionChartSpaceChartSpace1-DataSourceControlDataSourceControl1-CommandButtonCommand1Use ArraysCommandButtonCommand2Use ADO RecordsetCommandButtonCommand3Use SpreadsheetNOTE:. 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 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.captio n = "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 (ie, 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 = chalegendpositionbottom' Add a title to the chart .haastiti = 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 la bels 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 (chaxisposition) .hastitle = true .title.caption = "x" .title.font.size = 8 .majorUnit = 1 End with with .axes (chaxispositionL). 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 style for the series with .seriescollection (0) .MARKER.Style = ChmarkerStydot .marker. Size = 6.Line.Weight = 1 .line.color = RGB (255, 0) end with 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"

Click Properties on the Project menu. Select the Make tab, deselect Remove Information about Unused ActiveX Controls and click OK. Press the F5 key to run the program. Click the Use Arrays button. A combination chart with two value axes is created. The chart .. uses data from arrays 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 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

转载请注明原文地址:https://www.9cbs.com/read-14647.html

New Post(0)