|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Immediate SolutionsAdding A Chart Control To A ProgramThe 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? Its time to add a Microsoft chart control to your program, and doing that is easyjust follow these steps:
To select the type of chart you want, you set the chart controls ChartType property. Here are the possible settings for that property:
Now that youve added a new chart control to your program, its 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 ControlYouve added a chart control to your form, and its displaying databut its 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). Thats fine as far as it goes, but now its time to enter your own data in that chart. There are several ways of doing so, and well look at them here. Using The ChartData Property As mentioned in this chapters overview, the data in a chart control is stored in an internal data grid (in fact, its 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 controls 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. Lets take a look at an example. Here, well just create a simple bar chart (ChartType = VtChChartType2dBar, the default). Start by adding a new chart control, MSChart1 (thats 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; well 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
Thats it. Now run the program as you see in Figure 12.3. Weve created our first simple chart. The code for this program is located in the chart folder on this books 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 controls Data property to enter data. To use the Data property to fill the chart controls data grid, you set the row and column you want to place data in using the chart controls 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 were not passing an array to the chart control here, you must give that control the proper dimensions of the array youre setting up, which means you must set the RowCount and ColumnCount properties. If youre just entering sequential data points, you can set the chart controls 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 thats itthis code produces the same result you see in Figure 12.3.
|
|
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. |