Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Immediate Solutions

Adding A Chart Control To A Program

The Testing Department is calling again. Your new program, SuperDuperDataCrunch, is great, but why does it display the data as a long stream of numbers in a text box? Well, you ask, what else would you suggest? They say, how about a chart?

It’s time to add a Microsoft chart control to your program, and doing that is easy—just follow these steps:

1.  Select the Project[vbar]Components menu item.
2.  Select the Controls tab in the Components box that opens.
3.  Select the Microsoft Chart Control entry in the Components box, and click on OK to close the Components box.
4.  Draw a new chart control on your form.

To select the type of chart you want, you set the chart control’s ChartType property. Here are the possible settings for that property:

  VtChChartType3dBar—3D bar chart
  VtChChartType2dBar—2D bar chart
  VtChChartType3dLine—3D line chart
  VtChChartType2dLine—2D line chart
  VtChChartType3dArea—3D area chart
  VtChChartType2dArea—2D area chart
  VtChChartType3dStep—3D step chart
  VtChChartType2dStep—2D step chart
  VtChChartType3dCombination—3D combination chart
  VtChChartType2dCombination—2D combination chart
  VtChChartType2dPie—2D pie chart
  VtChChartType2dXY—2D XY chart


TIP:  Note that the ChartType property actually appears with a small initial letter, chartType, in the current build of Visual Basic, although when you check the Visual Basic documentation, it has a large initial letter, ChartType. Because Visual Basic corrects capitalization automatically, you can type this property in either way, but it’s an oddity you might note.

Now that you’ve added a new chart control to your program, it’s time to fill it with data. There are several ways of doing so, and they can get pretty involved. See the next topic in this chapter for the details.

Adding Data To A Chart Control

You’ve added a chart control to your form, and it’s displaying data—but it’s not your data. How do you fix that?

When you add a chart control to a form, it displays random data (which is good if you want to change chart types and see what the possibilities look like). That’s fine as far as it goes, but now it’s time to enter your own data in that chart. There are several ways of doing so, and we’ll look at them here.

Using The ChartData Property

As mentioned in this chapter’s overview, the data in a chart control is stored in an internal data grid (in fact, it’s stored in a Visual Basic data grid control, one of the data-bound controls, inside the chart control). Probably the quickest way of filling a chart control is by filling that data grid directly, and we can access the data grid directly with the chart control’s ChartData property.

You can either get or set the data grid in a chart control with this property, because it refers directly to an array of variants. Let’s take a look at an example. Here, we’ll just create a simple bar chart (ChartType = VtChChartType2dBar, the default).

Start by adding a new chart control, MSChart1 (that’s the default name Visual Basic will give it) to your program. Next, we declare an array of variants to hold our data:

Private Sub Form_Load()
    Dim X(1 To 5) As Variant
...

The first entry in the array is a label that will appear on the x-axis; we’ll just label it “Data”:

Private Sub Form_Load()

    Dim X(1 To 5) As Variant
    X(1) = "Data"
...

Next, we add the data itself we want to display:

Private Sub Form_Load()

    Dim X(1 To 5) As Variant
    X(1) = "Data"
    X(2) = 1
    X(3) = 2
    X(4) = 3
    X(5) = 4
...

Finally, we install the array in MSChart1 using the ChartData property:

Private Sub Form_Load()

    Dim X(1 To 5) As Variant
    X(1) = "Data"
    X(2) = 1
    X(3) = 2
    X(4) = 3
    X(5) = 4

    MSChart1.ChartData = X

End Sub

That’s it. Now run the program as you see in Figure 12.3. We’ve created our first simple chart.

The code for this program is located in the chart folder on this book’s accompanying CD-ROM.

Another way of installing data in a chart is to use the Data property.

Using The Data Property

You can also use the chart control’s Data property to enter data. To use the Data property to fill the chart control’s data grid, you set the row and column you want to place data in using the chart control’s Row and Column properties, and then you just set the Data property to the value you want at that location, like this: MSChart1.Data = 5. Note that because we’re not passing an array to the chart control here, you must give that control the proper dimensions of the array you’re setting up, which means you must set the RowCount and ColumnCount properties.

If you’re just entering sequential data points, you can set the chart control’s AutoIncrement property to True, and then enter the sequential points into the Data property, one after another:

Private Sub Form_Load()
    MSChart1.Data = 1
    MSChart1.Data = 2
    MSChart1.Data = 3
    MSChart1.Data = 4
...

The Data property can only take numeric data, so to set the text that will appear on the x-axis for our data, we use the RowLabel property to label row 1 like this:

Private Sub Form_Load()
    MSChart1.Data = 1
    MSChart1.Data = 2
    MSChart1.Data = 3
    MSChart1.Data = 4
    MSChart1.Row = 1
    MSChart1.RowLabel = "Data"
End Sub

And that’s it—this code produces the same result you see in Figure 12.3.


Figure 12.3  Creating a simple chart.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.