322 | Chapter 8: Observer Pattern
In this application, all you’re really going to need are the labels that show up indicat-
ing the quarterly values. However, in some future application, you may need the
actual numeric data, and so the class stores both the label and data. A literal label
goes at the top, and so the first label,Quarterly Results, is unchanged and serves as a
label for theUIListcomponent. Figure 8-7 shows what the list bo xlooks like when it
displays data:
As noted, even though you can’t see the actual data, it’s also stored in the compo-
nent. In some applications, there may be a need to access the data in the list box.
Bar chart display
Bar charts have an issue that list boxes don’t: the relative display of data. A com-
puter screen has a finite number of pixels to display the bars in a bar chart. If your
values are relatively small—say between 1 and 100—you have plenty of room to dis-
play your charts, but as the values become bigger, you run out of vertical and hori-
zontal screen real estate. Because the quarterly values have to be displayed as relative
to one another and not all possible values, the job’s somewhat easier. All you have to
do is to find the largest value in the group of four in the array. That value can then be
treated as a factor representing the largest value in the bar chart. For example, sup-
pose you are using vertical bar charts with a maximum of 200 pixels and you have
the following four values:
- 320
- 432
- 121
•89
The maximum value is 432. That value must be represented by no more than 200
vertical pixels. Using the formula:
(currentValue ÷ MaxValue) x maxPixels
you can work out what each value should be. As you can see, the highest value
would be 200 pixels, and the lesser values in the group would be proportionately
smaller:
432÷432 = 1 x 200 = 200
121÷432 =.28 x 200 =56
Figure 8-7. Data in list box