The magazine of the Melbourne PC User Group

A logarithmic graph application in QBasic
Ron Kitchenn
ronkit@melbpc.org.au

Data are often available in the form (date, "value"), which need to be presented graphically. A typical application is the graphing of share prices over a period of time. It is often helpful to present such data on a logarithmic, (rather than a linear) scale, for which the advantages may be:

  • A better understanding of the data
  • The presentation of a greater range of values.
For example, a constant percentage rate of change is shown as a straight line on such a graph.

The QBasic LOGCHART.BAS program described below started life as part of a suite of programs written to record and analyse a portfolio of share transactions, and provided graphs of share price against time, with "value", the latter in terms of dollars and cents. For the present purpose, "value" is of the generalised form y = nn.nn, and the y axis is designated "Y", instead of dollars.

Any date of the form dd/mm/yy (including "today") may be chosen from the time span of the data file, from which to "look back" over the "values" recorded in the file. A feature of the program is the automatic selection of one of four output charts, according to the data value at the "look-back-from" date. The ranges of "value" available are 0.1 to 1, 0.3 to 3, 1 to 10 and 3 to 30. Other ranges could be added, of course.

The program

The program was written with the expectation that it would be used on a data file comprising integer "n" (the number of entries in the file) followed by n 13-byte string entries of the form dd/mm/yy (8 bytes) + nn.nn (5 bytes). 

In the following description, figures such as [1] refer to the corresponding blocks in Figure 2.


Figure 1.  Sample outout

On "run", the screen asks for the path and file name of the data file [1], and this is stored in memory [2]. Next, the user is asked to enter a "look-back-from" date (within the known range of the data, of course), or to press any key if the answer is "today". In the latter case, the computer's DATE$ (mm/dd/yy) is converted to dd/mm/yy [4].

The "value" for the entered date is now compared with the (logarithmic) middle of each of the available ranges, and a range is selected in which the difference is a minimum. This places the latest-date plot of "value" midway between the maximum and minimum positions of the selected scale. For example, the middles of each of the four (logarithmic) ranges are:

  • 0.3 in the 0.1 to 1 range
  • 1 in the 0.3 to 3 range
  • 3 in the 1 to 10 range
  • 10 in the 3 to 30 range.
Other ranges could of course be added in the program. The blocks [7.1] to [7.4] in Figure 2 apply relevant scales in the drawing of the grids on the screen.

For all of the y ("value") scales, the x (horizontal) scale is presented as 0 weeks (back from the selected look-back-from date), at the right-hand end of the screen, to 100 weeks back. Vertical dashed lines are drawn to form markers at 20, 40, 60, 80 and 100 weeks. The look-back-from date is shown, together with the file name, below the x axis.

The horizontal markers for block [7.1] are drawn in dashed equally-spaced lines at y = 0.1, 0.15, 0.25, 0.4, 0.65 and 1.0. Similarly, the corresponding lines for block [7.3] are drawn at ten times these values.

The horizontal markers for block [7.2] are drawn in dashed equally-spaced lines at y = 0.3, 0.45, 0.75, 1.2, 2.0 and 3, with the corresponding lines for block [7.4] drawn at ten times these values.

In the final block [8], the latest-date x,y plot is made first, then a line connects it to the next-latest, and so on, until the earliest plot is made. The resulting graph is retained until the program is completed by pressing any key. A sample plot of dollar values over two years in shown in Figure 1.

Rate of change

I mentioned above that in a logarithmic graph, a constant percentage change in the values of the series is represented by a straight line. It helps users of LOGCHART.BAS to know the relationship between the gradient of the line on the screen and the associated rate. Here are sample rates (in % per annum) and the gradient (in degrees):

 

% p.a. Gradient (degrees)
5 1.72
10 3.34
15 4.95
20 6.37
25 7.78
30 9.18
35 10.48
40 11.68
45 12.86
50 13.95

Availablity

LOGCHART.BAS could be incorporated as a subroutine in a parent program. It is fully commented and ican be downloaded as LOGCHART.BAS.

Reprinted from the March 1999 issue of PC Update, the magazine of Melbourne PC User Group, Australia

 

[About Melbourne PC User Group]