324 | Chapter 8: Observer Pattern
Figure 8-8 shows what the bar chart looks like when values have been assigned to it.
Once drawn, a graphic does not automatically clear the stage when a new chart
replaces it. Theclear( )method simply clears current memory, but not the stage. To
clear the stage, you need to useremoveChild( ). That requires an added child. So in
the main program, we used a flag to make sure that a graphic element had been
added, and then on the next data set, the flag’s cleared and the first drawing
removed.
The line graph
The line graph has the same issues with having proportionate points for drawing
lines as the bar chart has for making rectangles. Fortunately, they’re solved in the
same way. An added feature in the line graph is some kind of background grid to
give some better sense to the lines showing on the screen. The lines, without some
kind of grid, just look like disembodied lines floating in space. So in addition to pro-
viding a drawing system to draw lines in the right positions, this class includes a
background grid. Following the dictum of information designer Edward Tufte, the
grid employs the minimum effective difference, so we used a thin light gray line. The
line graph uses a wider blue line, so while the grid provides a context for the line
graph, it doesn’t get in the way. Open up a new ActionScript file and enter the code
in Example 8-27, using the caption as the filename.
addChild(bar);
}
}
}
Figure 8-8. Bar chart
Example 8-27. QuarterGraph.as
package
{
//Line graph maker
import flash.display.Graphics;
import flash.display.Shape;
Example 8-26. QuarterBar.as (continued)