[reportlab-users] How to add a title to a chart?

Andy Robinson reportlab-users@reportlab.com
Thu, 11 Sep 2003 07:06:53 +0100


> Hi-
>
> I want to add a title to a chart.  I can think of two ways to do it:
>
> 1. Use create a string object and use the x and y attributes to move it
> around it until I get it centered.
>
> 2.  Learn how platypus works and embed the chart image inside the page.
>
> How are the rest of you adding titles to charts?

We do (1).  If you ever wanted to export the chart in a different format,
you would want the title.

However, centering it is a bit easier than you suggest.  If you have a
200x100 drawing and want the string centred, do
   d = Drawing(200, 100)
   title = String(100,80, 'My title', textAnchor='middle')

The textAnchor can be one of 'start','middle' or 'end'.

Also, you can get automatic layout information if you need it.  You have to
position the 'inner rectangle' of the chart with (x,y,width,height)
properties, but you can find out how much the axis labels and so on stick
out by calling <any graphics object>.getBounds().  This may be useful if you
want to center above the combined chart-and-axzis-labels, instead of a known
x value.

The graphics objects have LOTS of properties which are extensively
tested (we use them in commercial development) but not well enough
documented for the open source code.  In general, glance at
the first few lines of the class definition for the 'attribute map'
to tell you what is available.  The things beginning with 'is' are
validators defined in reportlab\lib\validators.py, and will
tell you what values are possible.

class String(Shape):
    ...snip...
    _attrMap = AttrMap(
        x = AttrMapValue(isNumber),
        y = AttrMapValue(isNumber),
        text = AttrMapValue(isString),
        fontName = AttrMapValue(None),
        fontSize = AttrMapValue(isNumber),
        fillColor = AttrMapValue(isColorOrNone),
        textAnchor = AttrMapValue(isTextAnchor),
        )

Hope this helps,

Andy