[reportlab-users] pdfgen and Charts

Andy Robinson andy at reportlab.com
Fri Apr 17 10:17:06 EDT 2009


2009/4/17 Peter Mott <peter at monicol.co.uk>:

> I'm using the basic pdfgen package for my project. I'd like - though it is

> not vital - to create a chart and add it to my canvas.  I can add a path p I

> have created by canvas.drawPath(p) and a textobject t by canvas.drawText(t).

> I can create a rectangle r from the graphics package - but cannot find out

> how to add it to the canvas. Can this be done?

>


Have you checked out the reportlab/graphics subpackage? There is a
charts library documented in chapter 11 of our userguide, and a
separate reference document. Most of the examples make a standalone
PDF file for each chart, but each Drawing object is also a Flowable
and can thus draw itself on a canvas.

If you have a distro without the docs, manuals are here....
http://www.reportlab.com/docs/reportlab-userguide.pdf
http://www.reportlab.com/docs/reportlab-graphics-reference.pdf


Assuming you already have a canvas object, you can construct a
Drawing, add one or more Charts and other shapes to it, then either
tell it to draw itself on the canvas, or add it to the story to be
flowed into a Platypus document...

from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
drawing = Drawing(400, 200)
data = [
(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)
]
bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data

drawing.add(bc)

....

Now draw it like this...

drawing.drawOn(myCanvas, x, y)


...

or, if you are making a flowing document with Platypus, just add the
Drawing to the story./


Best Regards,

--
Andy Robinson
CEO/Chief Architect
ReportLab Europe Ltd.
Media House, 3 Palmerston Road, Wimbledon, London SW19 1PG, UK
Tel +44-20-8545-1570


More information about the reportlab-users mailing list