The Computer Tutor: Data Graphs

Author(s): Gene G. Buza
Publication: Synchro-Sette
Pub Details: Synchro-Sette v1 n9 (Volume 1, Issue 9)
Date: December 1982
Pages: 13-15,20
Computer Tutor illustration, professor in cap and gown pointing at a computer
Computer Tutor illustration, professor in cap and gown pointing at a computer

Sorry I’m late Class. Too much of the holiday spirits, I guess. Boy does my head hurt. I wanted to wait till after the bell rang before I came in.

Did you all have a nice holiday? Ah, that’s good.

Today we will discuss using the computer’s graphics generator to produce chart type graphics that can help data be visualized such as sales or profits. Can anyone tell me what commands or functions in the BASIC language can be used to produce graphs on the screen?

Yes, the gentleman in the back?

That’s right, the PRINT AT commands can accomplish this and he correctly states that the BAR-GEN program that was on the JUNE/82 cassette utilized this concept.

There is another way of PLOTting and UNPLOTting graphics. Can anyone tell me what that may be? Ahah, no hands this time. What else but the PLOT and UNPLOT commands. What does PLOT and UNPLOT do; yes, the gentleman in the back again?

No, it has nothing to do with planting a garden – nor does it have anything to do with grave-digging or digging in the ground at all, for that matter. I thought two right answers in a row would be a little too much to expect.

PLOT allows the user to pick a portion of the screen that he or she wants a little white square to appear. UNPLOTing the same location would make that square disappear.

PRINT AT graphics take up 4 times as much room on the screen as PLOT/UNPLOT graphics but allow the user the option of choosing any of the Sinclair graphics characters to appear on the screen. The space taken on the screen is twice as high and twice as wide as the PLOT/UNPLOT graphics characters.

PLOT/UNPLOT graphics symbols are only square in shape but 4 times as many can appear on the screen, so a finer resolution can be achieved. ENTER the following program and RUN it:

10 FOR Y=0 TO 31
20 LET X=(1.5 ** Y)** .225
30 PRINT AT 21-X,Y; CHR$ 128
40.NEXT Y

This program uses the PRINT AT graphics capability to plot a parabolic curve on the screen along the X and Y axes. Now ENTER and RUN the following program:

10 FOR Y=0 TO 43
20 LET X=(1.5 ** Y)**.15
30 PLOT Y,X
40 NEXT Y

A similar curve is plotted with finer resolution. This method is much better suited for graphing purposes but how can we use it where we can input data and have the data represented in the curve of the graph?

The answer lies in converting the data items into the values to be plotted on the screen. First, let us examine what we have to work with – that is the PLOT co-ordinates of the screen. Turn to the section of your Owner’s Manual that has the screen co-ordinates defined.

Let us say that we had 12 months of sales that we wanted to plot on the screen. The sales figures are as follows:

JANUARY      1752
FEBRUARY     1339
MARCH        2897
APRIL        4544
MAY          3317
JUNE         2914
JULY         3776
AUGUST       5221
SEPTEMBER    3890
OCTOBER      2542
NOVEMBER     4119
DECEMBER     3556

These sales figures pretty much represent proportions of many businesses as they reflect seasonal trends involving cold weather, vacations and holidays.

It would be easy to write a program to allow the input of these figures:

10 DIM S(12)
20 FOR N=1 TO 12
30 SCROLL
40 INPUT S(N)
50 PRINT N,S(N)
60 SCROLL
70 NEXT N

This program would allow the input of all the above sales figures but the conversion of those figures into screen co-ordinates is a little more tricky.

The screen PLOT co-ordinates allow a maximum of 64 positions across the screen and 44 up and down. It is rare that any data would fall into these parameters exactly. We could use the 64 positions across to indicate the months and to format the limits of our sales but we have to have a method of making the sales automatically fall into our height parameters. This, believe it or not, is a relatively easy task. First let us write a routine that would automatically find the maximum sales value. Add the following lines to the previous program:

80 LET A=0
90 FOR N=1 TO 12
100 IF S(N)>A THEN LET A=S(N)
110 NEXT N

A simple routine that easily finds the maximum sales value. It is better to have this routine separate from the actual inputting of the sales figures because if you have a program that allows additional sales data to be added to or deleted from, the value of (A) may not represent the highest sales value. It therefore behooves the programmer to have this routine used after all the data has been correlated.

We now have found the upper limit of our sales graph. We still have to achieve a method of converting this data into legal co-ordinate screen positions. The following lines accomplish this:

120 PRINT AT 21,1; 0; AT 1,1; A
200 FORN=4 TO 48 STEP 4
210 LET Y=(S(N/4)/A)*43
220 FOR Z=2 TO Y
230 PLOT N+2,Z
240 NEXT Z
250 PRINT AT 21,N/2+1;N/4
260 NEXT N

RUN this program with the above monthly sales data and observe the bar graph chart on the screen.

Line 120 sets the maximum/minimum parameters and the routine from lines 200 to 260 prints the graphic bars. This routine along with the routine from lines 80 to 110 automatically takes the largest sales value which is assigned to the numeric variable (A) and proportions all of the rest of the sales data to it.

Other types of graphic routines exist, also. Eliminate the lines from 220 to 260 and insert the following:

230 PLOT N+2,Y
250 PRINT AT 21,N/2+1;N/4
260 NEXT N
270 STOP

If you ENTER GOTO 120, the previously entered data will remain intact, even though program lines have been changed or discarded. You should observe on the screen just the data points that correspond with the monthly sales figures. This, of course, is not as easy to visualize as the BAR graph that we first generated. We cannot easily see the direction that the sales take from month to month. We need a method of connecting these points so that they can be more easily visualized. Add the following lines to the existing program:

240 GOSUB 300
300 LET Z=(S(N/4+1)/A)*43
310 IF Z<Y THEN GOTO 400
320 LET X=Z-Y
330 FOR C=1 TO 3
340 PLOT N+C+2,Y+C*X/3
350 NEXT C
360 RETURN
400 LET X=Y-Z
410 FOR C=1 TO 3
420 PLOT N+C+2,Y-C*X/3
430 NEXT C
440 RETURN

Again, keep the variables intact by executing the command, GOTO 120. Now we see the direction the sales take from month to month with the extra plotted data points. These points are produced by the subroutines starting with line 300 and line 400.

The display ends without the 12th month’s sales being charted. This is because one of the routines is searching for the sales data for the 13th month for a comparison, and of course, the 13th month doesn’t exist in our data file. Just disregard the error for now.

The line 300 subroutine simply produces a series of 3 points of equal distance that connect the 2 main sales data points between a month’s sales that are larger than the following month’s sales and that following month’s sales data point.

The line 400 subroutine does the same thing, but only if a month’s sales are smaller than the following month’s. In other words, we have achieved a method of connecting the points to make the graph more visual. But is this the ultimate effect?

Add the following lines to the existing program:

345 GOSUB 500
425 GOSUB 600
500 FOR D=Y+((C-1)*X/3) TO
Y+(C*X/3)
510 PLOT N+C+2,D
520 NEXT D
530 RETURN
600 FOR D=Y-((C-1)*X/3) TO
Y-(C*X/3) STEP -1
610 PLOT N+C+2,D
620 NEXT D
630 RETURN

We now have the type of graph with appropriate peaks and valleys that we are used to seeing in the financial sections of our newspapers. You of course notice that the sales data points are plotted by the program before the connecting lines are plotted. To overcome this, reposition line 425 into line 415 so that the connecting line subroutine is executed before the sales data points are plotted. The same thing has to be done to reposition line 345 into line 335.

This program is a useful utility for any program where data must be graphed. As it stands, not many more than 12 data entries can be made – maybe 15 at most. The program can be modified to allow as many as 64 data entries which is what your homework assignment is.

WHAT DO YOU MEAN, NO HOMEWORK FOR THE HOLIDAYS!

Oh, you brought me a present? How nice, to think of the old Professor. Well, OK, I guess I can find a little more holiday spirit in me – Class dismissed.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.