8.3. Understanding text alignment and anchor point

When a text string position is specified that screen (or scale) position by default gets aligned with the top left corner of the strings bounding box. We say that the top left corner is the anchor point of the text string. The alignment of the anchor point can be adjusted with a call to Text::SetAlignment($aHorAlign, $vertAlign). The two arguments are given as text strings and the admissible values for each argument are:

Horizontal alignment

'left', 'center', 'right'

Vertical alignment

'bottom', 'center' (or 'middle') , 'top'

Caution

Even though from an API perspective both bitmap fonts and TTF fonts share the same user API the implementation is vastly different. There are a number of subtle differences in the way built-in bit-map fonts and TrueType fonts are rendered to the screen. This means for example that the alignment of, say the bottom of the text string, is not pixel-perfect between bitmap and TrueType fonts However, JpGraph, abstracts away 99.9% of the differences so it will be, for all practical uses of the library completely transparent to switch between the different font types.

Manually setting the alignment for the anchor point is mostly useful when adding Text object to the graph to get the wanted alignment. You can see an example of how the anchor point changes depending on how the combination of alignments are used in Figure 8.2

Figure 8.2. Illustration of anchor point alignment (textalignex1.php)

Illustration of anchor point alignment (textalignex1.php)


Note

It might seem strange to have the method name "SetAlignment()" when it really should make more sense to use the name "SetAnchor()". We agree. This naming scheme is due to historical reasons.

Note

The graph legend box also have an anchor point that is specified as the 3:rd and 4:th argument to Legend::SetPos()