你目前正处于脱机状态,正在等待 Internet 重新连接

如何用 Visual Basic 中使用图表 Web 组件

注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。

点击这里察看该文章的英文版: 235885
不再更新的 KB 内容免责声明
本文介绍那些 Microsoft 不再提供支持的产品。因此本文按“原样”提供,并且不再更新。
概要
这篇文章介绍了如何使用 Visual Basic 窗体上 Office 图表 Web 组件可视化相关的数据的概览。更具体地讲该代码本文中的说明了通过示例如何执行以下操作:
  • 使用数组数据的图表。
  • 使用 ADO 记录集的数据绘制图表。
  • 使用电子表格数据的图表。
  • 在单个图表空间中创建多个图表。
  • 创建 — 和 — 组合图表。

  • 设置不同的图表组件 (如坐标轴、 数据标签、 系列、 plotareas 和更多的格式。
更多信息

创建示例的步骤

  1. 在 Visual Basic 中开始一个新的标准 EXE 项目。默认情况下创建 Form1。
  2. 项目 菜单上,单击 组件。选择 Microsoft Office Web Components 9.0,然后单击 确定
  3. 将下列控件添加到窗体中,具有指示的属性:

    类型名称标题
    图表工作区ChartSpace1-
    DataSourceControlDataSourceControl1-
    命令按钮Command1使用数组
    命令按钮Command2使用 ADO 记录集
    命令按钮Command3使用电子表格


    : 大小在表单上的图表工作区控件,以便它占用尽可能多的窗体,同时仍然允许对其他控件在窗体上。
  4. 将下面的代码添加到 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 = chLegendPositionBottomEnd SubPrivate 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 SubPrivate 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					
    : 您可能需要修改连接字符串 Command2_Click 事件中的,以使它正确引用 NWIND.MDB,默认情况下安装到 Visual Basic 目录的路径:
       DataSourceControl1.ConnectionString = _        "DRIVER={Microsoft Access Driver (*.mdb)}; " & _        "DBQ=C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb"					
  5. 项目 菜单上,单击 属性。选择 生成 选项卡,取消选择 删除未使用的 ActiveX 控件有关的信息,然后单击 确定
  6. F5 键运行该程序。
  7. 单击 使用数组 按钮。创建具有两个数值轴的组合图表。图表使用数组中的数据。
  8. 单击 使用 ADO 记录集 按钮。在图表空间 ; 条形图和数据标签与分离型的饼图中则不创建两个图表。图表空间使用一个 DataSourceControl 为其数据,并在 DataSourceControl 引用 Access 数据库中的通过 ADO 连接。
  9. 单击 使用电子表格 按钮。散点图图表创建对电子表格控件使用的单元格中的公式,作为其数据。此电子表格控件是动态地添加和移除在运行时。
图表绘制图表用图形表示 owc 图表空间

警告:本文已自动翻译

属性

文章 ID:235885 - 上次审阅时间:07/13/2004 14:56:20 - 修订版本: 1.1

Microsoft Office Chart Component 9.0, run time

  • kbmt kbhowto KB235885 KbMtzh
反馈