Node:Y Axis Element, 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."
(- full-Y-label-width
(length
(concat (number-to-string number)
Y-axis-tic)))))
(concat