|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Using The SetData Method You can use the data grid SetData method to place data in the chart control. Heres how you use SetData : DataGrid.SetData (row, column, dataPoint, nullFlag) Heres what the various arguments you pass to SetData mean:
All the data in our simple chart is in the same row, so we fill the data grid in the chart control using SetData this way (note that we access the data grid with the chart controls DataGrid property here): Private Sub Form_Load() MSChart1.DataGrid.SetData 1, 1, 1, False MSChart1.DataGrid.SetData 1, 2, 2, False MSChart1.DataGrid.SetData 1, 3, 3, False MSChart1.DataGrid.SetData 1, 4, 4, False MSChart1.Row = 1 MSChart1.RowLabel = "Data" End Sub This code produces the same result as before, shown in Figure 12.3. Working With A Multiple Data SeriesThe Testing Department is calling again. Your graph of total imported wheat by month looks very nice, but now that the company has diversified, you need to show the imports of rice, corn, wheat, lentils, and rye all on the same chart. Can you do that? You certainly can, using a data series. When you fill the chart controls data grid, you just add a new column for each crop, and a new line will appear in your graph. How does that work? Lets see an example. Here, well graph rice, corn, wheat, lentils, and rye imports for the months of January and February. Each set of data, rice, corn, wheat, lentils, and rye makes up a series, and each column in the data grid will hold the data for one series. We add a new row to make a new x-axis point for each item in the series. In this example, well have two rows, one for January and one for February, and five columns, one each for rice, corn, wheat, lentils, and rye. In fact, we add one row to hold row labels and one column to hold column labels. The row labels (January and February) will appear on the x-axis, and the column labels (rice, corn, wheat, lentils, and rye) will appear in the charts legend so the user can figure out what all the different-color lines (the data series) in the chart mean. Heres the way the data grid will be set up when were done:
Heres how that looks in code: Private Sub Form_Load() Dim X(1 To 3, 1 To 6) As Variant X(1, 2) = "Rice" X(1, 3) = "Corn" X(1, 4) = "Lentils" X(1, 5) = "Wheat" X(1, 6) = "Rye" X(2, 1) = "January" X(2, 2) = 2 X(2, 3) = 3 X(2, 4) = 4 X(2, 5) = 5 X(2, 6) = 6 X(3, 1) = "February" X(3, 2) = 4 X(3, 3) = 6 X(3, 4) = 8 X(3, 5) = 10 X(3, 6) = 12 MSChart1.ChartData = X End Sub When you set the chart controls ChartType property to VtChChartType2dLine and the ShowLegend property to True so the legend is displayed, the result appears as shown in Figure 12.4. You can see the various data series represented there, and the legend at right explains what each line means.
You can also use a data series with 3D graphssetting ChartType to VtChChartType3dStep creates the 3D step chart in Figure 12.5.
The code for this example is located in the chartseries folder on this books accompanying CD-ROM.
Setting Chart And Axis Titles And Chart ColorsIn the previous topic, weve seen how to create row labels and use a legend in a chart. However, theres much more hereyou can also set a charts title, as well as give titles to the entire x- and y-axes. To set a charts titles, you can open the chart controls property pages, and you do that at design time by right-clicking the chart control and selecting Properties in the menu that appears. You can then click the Text tab in the property pages and set the text for the charts title, as well as the titles of the two axes. If you click the Fonts tab, you can set the fonts used in those titles. As an example, weve added axis titles to the chart in Figure 12.6.
You can also set the colors used in a series in a chart in the property pagesjust click the Series Color tab in the property pages, and you can set the color used for each series (that is, each column in the data grid). Creating Pie ChartsThe Testing Department is calling again: bar charts are nice, but how about some pie charts in your new program, SuperDuperDataCrunch? You think, How do you do that? You set the chart controls ChartType property to VtChChartType2dPie. The chart control will display as many pie charts as you set up rows in the data grid (minus one row for the use of labels). For example, well set up two pie charts here, January and February, each with five pie slices, rice, corn, lentils, wheat, and rye: Private Sub Form_Load() Dim X(1 To 3, 1 To 6) As Variant X(1, 2) = "Rice" X(1, 3) = "Corn" X(1, 4) = "Lentils" X(1, 5) = "Wheat" X(1, 6) = "Rye" X(2, 1) = "January" X(2, 2) = 2 X(2, 3) = 3 X(2, 4) = 4 X(2, 5) = 5 X(2, 6) = 6 X(3, 1) = "February" X(3, 2) = 4 X(3, 3) = 6 X(3, 4) = 8 X(3, 5) = 10 X(3, 6) = 12 MSChart1.ChartData = X End Sub The result appears in Figure 12.7. Now were creating pie charts in Visual Basic.
|
|||||||||||||||||||||||||||||||||||||||||||||
|
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. |