19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

placed before the Scale method is executed.<br />

9.3 BAR CHARTS<br />

Drawing bar charts requires a variation of the line statement. If (x1, y1) <strong>and</strong> (x2, y2) are two<br />

points on the screen, then the statement<br />

picBox.Line (x1, y1) - (x2, y2), , B<br />

draws a rectangle with the two points as opposite corners. If B is replaced by BF, a solid rectangle<br />

will be drawn (see Figure 9-12).<br />

FIGURE 9-12 Line Method with B <strong>and</strong> BF Options<br />

EXAMPLE 1<br />

The 1998 populations of California <strong>and</strong> New York are 32 <strong>and</strong> 18 million, respectively. Draw a bar chart<br />

to compare the populations.<br />

SOLUTION:<br />

The following program produces the chart shown in Figure 9-13. The first five lines are the same as those<br />

of a line chart with two pieces of data. The base of the rectangle for California is centered above the point<br />

(1, 0) on the x axis <strong>and</strong> extends .3 unit to the left <strong>and</strong> right. (The number .3 was chosen arbitrarily; it had<br />

to be less than .5 so that the rectangles would not touch.) Therefore, the upper-left corner of the rectangle<br />

has coordinates (.7, 32) <strong>and</strong> the lower-right corner has coordinates (1.3, 0). Figure 9-14 shows the coordinates<br />

of the principal points of the rectangles.<br />

FIGURE 9-13 Bar Chart for Example 1<br />

Private Sub cmdDisplayPop_Click()<br />

‘Populations of California <strong>and</strong> New York<br />

picPop.Scale (-1, 40)-(3, -5) ‘Specify coordinates<br />

picPop.Line (-1, 0)-(3, 0) ‘ Draw x-axis<br />

picPop.Line (0, -5)-(0, 40) ‘ Draw y-axis<br />

picPop.Line (.7, 32)-(1.3, 0),,BF ‘Draw solid rectangle for CA<br />

picPop.Line (1.7, 18)-(2.3, 0),,BF ‘Draw solid rectangle for NY<br />

picPop.CurrentY = -1 ‘Vertical position of labels<br />

Bar Charts 273

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!