Next: , Previous: Compute a Remainder, Up: print-Y-axis

C.2.2 Construct a Y Axis Element

When we print the vertical axis, we want to insert strings such as ‘5 - and ‘10 -  every five lines. Moreover, we want the numbers and dashes to line up, so shorter numbers must be padded with leading spaces. If some of the strings use two digit numbers, the strings with single digit numbers must include a leading blank space before the number.

To figure out the length of the number, the length function is used. But the length function works only with a string, not with a number. So the number has to be converted from being a number to being a string. This is done with the number-to-string function. For example,

     (length (number-to-string 35))
          ⇒ 2
     
     (length (number-to-string 100))
          ⇒ 3

(number-to-string is also called int-to-string; you will see this alternative name in various sources.)

In addition, in each label, each number is followed by a string such as ‘ - , which we will call the Y-axis-tic marker. This variable is defined with defvar:

     (defvar Y-axis-tic " - "
        "String that follows number in a Y axis label.")

The length of the Y label is the sum of the length of the Y axis tic mark and the length of the number of the top of the graph.

     (length (concat (number-to-string height) Y-axis-tic)))

This value will be calculated by the print-graph function in its varlist as full-Y-label-width and passed on. (Note that we did not think to include this in the varlist when we first proposed it.)

To make a complete vertical axis label, a tic mark is concatenated with a number; and the two together may be preceded by one or more spaces depending on how long the number is. The label consists of three parts: the (optional) leading spaces, the number, and the tic mark. The function is passed the value of the number for the specific row, and the value of the width of the top line, which is calculated (just once) by print-graph.

     (defun Y-axis-element (number full-Y-label-width)
       "Construct a NUMBERed label element.
     A numbered element looks like this `  5 - ',
     and is padded as needed so all line up with
     the element for the largest number."
       (let* ((leading-spaces
              (- full-Y-label-width
                 (length
                  (concat (number-to-string number)
                          Y-axis-tic)))))
         (concat
          (make-string leading-spaces ? )
          (number-to-string number)
          Y-axis-tic)))

The Y-axis-element function concatenates together the leading spaces, if any; the number, as a string; and the tic mark.

To figure out how many leading spaces the label will need, the function subtracts the actual length of the label—the length of the number plus the length of the tic mark—from the desired label width.

Blank spaces are inserted using the make-string function. This function takes two arguments: the first tells it how long the string will be and the second is a symbol for the character to insert, in a special format. The format is a question mark followed by a blank space, like this, ‘? ’. See Character Type, for a description of the syntax for characters. (Of course, you might want to replace the blank space by some other character ... You know what to do.)

The number-to-string function is used in the concatenation expression, to convert the number to a string that is concatenated with the leading spaces and the tic mark.