Node:font, Next:, Previous:fill_style, Up:Top



Drawing texts

You can display text strings in chart as a part of axis labels, plot labels, legends, etc. You can also display an arbitrary text in an arbitrary location using annotations (see text_box) or canvas.show function (see canvas).

Text strings may contain escape characters that control its appearance. The escape sequences all start with the letter "/". Thus, to display "/" itself, you must write "//".

Sequence Meaning
/add Specifies the angle the text is drawn. Parameter dd is a number between -360 to 360. Value 0 means left-to-right text, 90 means bottom-to-up, etc.

If you want to print numbers right after an angle specification, put {} between the angle and the number. For example, the below code shows string ""100"" at 60-degree angle.

"/a60{}100"

/hA Specifies horizontal alignment of the text. A is one of "L" (left alignment), "R" (right alignment), "C" (center alignment).
/vA Specifies vertical alignment of the text. A is one of "B" (bottom), "T" (top), "M" (middle).
/T Switch to Times font family.
/H Switch to Helvetica font family.
/C Switch to Courier font family.
/B Switch to Bookman-Demi font family.
/A Switch to AvantGarde-Book font family.
/P Switch to Palatino font family.
/S Switch to Symbol font family.
/F{family} Switch to family font family. The below example draws string "Funny" using ZapfDingbat font (which produces some meaningless output).
canvas.show(100, 200, "/F{ZapfDingbat}Funny")

The list of available fonts are the following:

Bookman-Demi Bookman-Light Courier AvantGarde-Book AvantGarde-Demi
Helvetica Helvetica-Narrow Palatino NewCenturySchlbk Times
Symbol ZapfChancery-MediumItalic ZapfChancery-Medium-Italic ZapfDingbats

/b Switch to bold typeface.
/i Switch to italic typeface.
/o Switch to oblique typeface.
/dd Set font size to dd points.
"/20{}2001 space odyssey!"

/cdd Set gray-scale to 0.dd. Gray-scale of 0 means black, 1 means white.
//, /{, /} Display `/', `}', or `{'.
{ ... } Limit the scope of escape sequences. For example, "{/10{/20Big text} and small text}" will display "Big Text" using 20-point font, and "and small text" using 10-point font.
\n Break the line.

Restriction: /h, /v, and /a must appear in the beginning of a string.

The font module defines several procedures and variables for manipulating fonts:

font.quotemeta TEXT Function
Quote letters with special meanings in pychart so that TEXT will display as-is when passed to canvas.show(). For example, font.quotemeta("foo/bar") will return "foo//bar".

font.text_height TEXT Function
Return the height of the text in points.

font.text_width TEXT Function
Return the width of the text in points.

font.get_dimension TEXT Function
Return tuple (xmin, xmax, ymin, ymax)

theme.default_family Variable
Default font family (Helvetica).

theme.default_font_size Variable
Default font size (9).

theme.default_halign Variable
Default horizontal alignment (h)

theme.default_valign Variable
Default vertical alignment (b)

theme.default_angle Variable
Default angle (0)
fonttest.png

Font usage example

Below is the source code that produces the above chart. ../demos/fonttest.py

from pychart import *
import re
can = canvas.default_canvas()

x = 100
y = 500

def show_text(str):
    global x, y
    can.show(x, y, str)
    e = "/16/C" + font.quotemeta(str)
    can.show(x+200, y, e)
    y = y - 20

show_text("/16/hLLeft align")
show_text("/16/hRRight align")
show_text("/16/hCCenter align")
show_text("/a20/16/hRAngled text")

def show_textv(str):
    global x, y
    can.show(x, y, str)
    x = x + 150

y = y - 40
x = 100
show_textv("/16/vT//16//vTTop align")
show_textv("/16/vM//16//vT/16Middle align")
show_textv("/16/vB//16//vT/16Bottom align")

y = y - 40
x = 100
show_text("/16/HHelvetica")
show_text("/16/CCourier")
show_text("/16/NHelvetica-Narrow")
show_text("/16/PPalatino-Roman")
show_text("/16/AAvantgarde")
show_text("/16/T/iTimes-Italic")
show_text("/16/F{ZapfDingbats}ZapfDingbats")