9.0 Introduction
9.1 Data calculated by the program
9.2 Removal of the cursor from the final display
9.3 Activities
9.4 Data read from DATA statements
9.5 Titles and other labels
9.6 Activities
9.7 Data taken from the INPUT statement
9.8 Forcing the inclusion of the origin
9.9 Activities
It is always useful to be able to display information graphically. Graphs are more visually attractive than print. They get information across more quickly and easily, and they make it much easier to see trends. This chapter shows how to draw graphs to display your own information. It relies on the procedures which we have developed and which we assume that you have now recorded: either by typing them in from Appendix 1 or as the ready-recorded tape. These procedures free you from having to worry about programming such things as a suitable range for the graph; scaling it to fit onto the screen; and drawing, marking and labelling the axes. Your only concern will be to program some lines to act as a 'driver' for our procedures and we now go on to explain how to do this.
We shall illustrate three ways of feeding in data: getting the computer to calculate the data from a formula, plus some starting conditions; providing the data, as co-ordinates for each of the points to be plotted; and from data entered at the time the program is run.
In this section we illustrate drawing a graph which gets the computer to calculate the data from a formula and some starting conditions. Our example is a graph to show the mortgage still owing on a house as a function of year (see Screen Display 9.1). The program is given in Listing 9.1. Apart from the lines which define the procedure PROCgraph, and line I 70 which we explain in the next section, the program is probably self-explanatory. Line 10 sets the foreground and background colours. Then the program is concerned with calculating values to fill two arrays which hold the co-ordinates of the points to be plotted on the graph. We decided, quite arbitrarily, to have 20 points on the graph. So we dimensioned the arrays X() and YO to 20 in line 50 and we set the limit of the REPEAT...UNTIL loop to 'year' > 19 in line 110. Lines 80 to 100 perform a calculation of the mortgage still owing, assuming a starting capital of 10000 with an annual interest rate of 15.5%. Once the program has written the values of the co-ordinates of the points to be plotted into the arrays XO and YO, and once X(0) has been given the number of the points, line 140 calls PROCgraph. This itself calls on other procedures. It consequently takes care of scaling the display to fit the screen, drawing, graduating and numbering the axes and plotting the points.
We hope and expect that, armed with this example, you will be able to draw graphs to display data of your own. When you come to do so, it may be worth bearing the following points in mind. Firstly there are other ways of filling the arrays XO and YO, and some may be more suitable than others for your purpose. Secondly PRDCgraph has to be called by a line such as:
100 PROCgraph
It expects to find arrays XO and YO which must have previously been dimensioned with a line such as:
10 DIM X(20), Y(20)
where:
20 is the number of points to be plotted,
X(0) = number of points
X(1), X(2), etc are the x co-ordinates of points to be plotted
Y(1), Y(2), etc are the corresponding y co-ordinates.
Since the existence of the cursor tends to spoil any graphical display, the program of Listing 9.1 removes it to outside the screen area once the graph is complete. It uses a VDU statement to define that text should be written at the graphics cursor. Then the graphics, and therefore the cursor, are moved off the screen. Both operations are performed by the single line 170:
170 VDU5 :MOVE 0,1050 :END
i. Run the program of Listing 9.1. Does it behave as you expect?
ii. Now modify the program of Listing 9.1. Try changing such things as the numbers of points, the rate of interest, the number of years for repayment or the original mortgage.
iii. There are many examples of graphs for which data can be calculated by the program. We hope that you will be able to use our approach to produce a graph that is particularly useful for you In some way.
In this section we show how to draw a graph for which you supply the co-ordinates of the points directly, in DA T A statements. Our example is a graph to show the variation of the air temperature (in degrees Centigrade) according to time of day (in hours) on 6th August 1982 (see Screen Display 9.2). The program is given in Listing 9.2.
Screen Display 9.1
The program works in the following way. The data is stored as a list of pairs of corresponding temperatures and times in the DA TA statements. The number of pairs of points is the first item of data. In line 30, the first item is read in. Then the X() and YO arrays are dimensioned and the rest of the points read in. A single call to PROCgraph in line 110 takes care of things like scaling. In essence this procedure expects the x and y values for all the points to be available for it in the arrays XO and YO. The number of points to be plotted must be held in X(0). The program draws the axes, graduates the axes and prints up the numerical value corresponding to the ends of the axes. It plots the points with an appropriate scaling factor. This means that virtually whatever the range of the values supplied as the co-ordinates for the points, they are suitably scaled for display. The colours for the foreground and background are set in line 10.
You can easily modify this program to display your own data. You keep most of the program as it is and only have to modify lines 130 and 140 which label the axes, and the data in the DATA statements. It is best to put the DATA statements at the end of the program. This has the advantage that you can enter the data, then check and alter as necessary using the BBC Microcomputer's excellent editing facilities. For example, we give l6 as the number of pairs of points in the first item of data in line I 70, but you can of course choose to have a different number of pairs of points.
You probably noticed that the title of the graph of Screen Display 9.2 is written with the line:
120 PRINT TAB(8,2);"AIR TEMPERATURE ON 6th AUGUST"
Screen display 9.2 (first part)
This method of writing relies on the use of the TAB function in a PRINT statement. The first value in the T AB function provides the character position along the line at which printing should start, the first character position being zero. The second gives the line number, counting the first line at the top of the screen as zero.
This method of writing can also be useful for labelling the axes. You may like to experiment with it, as an alternative to PROCnamex and PROCnamey. It always writes horizontally, whereas PROCnamex and PROCnamey write along the axes.
You can also label graphs using PROCchr or PROCmessage, as explained in Chapter 8. The large writing is particularly suitable for titles.
Screen Display 9.2 (second part)
i. Run the program of Listing 9.2. Does it behave as you expect?
ii. Try using the TAB function method, instead of PROCnamex and PROCnamey, to label the axes. Which method of naming axes do you prefer? While experimenting with naming the axes, you may like to try upper case letters and then lower case letters. Graphics artists often feel passionately about which looks better in which situation. Do you feel as strongly?
iii. Try using PROCchr or PROCmessage to give the display a titie.
iv. Try altering the data in the DATA statements to modify thedisplay.
Our next example is a graph showing how some 'doom statistic' such as 'population' or 'energy consumption' is increasing with time, and the data is fed in using INPUT statements. This produces the rather friendly-looking dialogue between the computer and the user, and it is shown at the beginning of Screen Display 9.3a. The rest of the screen display shows what happens when a user feeds in the data which is underlined. This underlining distinguishes what the user types in from what is produced by the program of Listing 9.3a.
The program is essentially very simple. It starts in line l0 by setting the foreground and background colours. Then in line 90, it asks the user how many points are to be displayed. When this is known, the arrays are dimensioned and the co-ordinates of the points requested by line 120. PROCgraph is then called in line 180 to draw the graph.
Screen Display 9.3a (first part)
Screen Displays 9.3a and 9.3b both show graphs of how some 'doom statistic' such as 'population' or 'energy consumption' is increasing with time. They both display the same data, but Screen Display 9.3b does not include the origin of the y axis, whereas Screen Display 9.3a does. The impacts of the two displays are quite different. Screen Display 9.3a does indeed seem to support the fact that doom is imminent, whereas Screen Display 9.3b suggests that the 'doom statistic' is changing very little.
Screen Display 9.3a (second part)
So for some purposes it is can be very important that one or both axes start from zero, whereas for other purposes, it can be just as important that they do not.
The simplest way to force the inclusion of the origin is to put in one extra point which is the origin itself. This was done for Screen Display 9.3b, as you can see from the initial dialogue, where the co-ordinates of point 1, input as 1974,0, are underlined.
Screen Display 9.3b
i. Modify the program of Listing 9.2 to include the origin on the graph of the variation of temperature with time of day.
ii, It is very simple indeed to modify Listing 9.3 to make it suitable for displaying something other than a 'doom statistic'. Modify lines 190 and 200 so that the labelling of the axes is appropriate for displaying other data of your choice.