21.2 Drawing graphs on canvas

PyChart creates a new canvas object when "area.T.draw()" (see Section 6) is called for the first time, and no canvas is yet created at that moment. You can thus use you own canvas by creating a canvas before calling the first area.T.draw(), like below:

can = canvas.init("foo.pdf")
...
ar = area.T(...)
ar.draw()

You can also achieve the same effect by passing the canvas object to the area.T.draw() method explicitly:

can = canvas.init("foo.pdf")
...
ar = area.T(...)
ar.draw(can)

You can also pass a file object (or file-like object, such as StringIO) to canvas.init. In this case, you need to define the output format via the second argument.

fd = file("foo.pdf", "w")
can = canvas.init(fd, "pdf")
...
ar.draw(can)

Naturally, you can write to multiple files by passing multiple canvas objects to different area.T.draw(). For example, the below example draws the first chart to graph1.pdf and the next chart to graph2.pdf.

/home/saito/pychart/demos/twographs.py

#
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
# 
# Pychart is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Pychart is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
from pychart import *

can = canvas.init("graph1.pdf")
data = chart_data.read_csv("lines.csv")
ar = area.T(x_range = (0,100), y_range = (0,100),
            x_axis = axis.X(label="X", tic_interval=10),
            y_axis = axis.Y(label="Y", tic_interval=10))
eb = error_bar.error_bar2(tic_len=5, hline_style=line_style.gray50)
ar.add_plot(line_plot.T(label="foo", data=data, error_bar=eb, y_error_minus_col=3),
            line_plot.T(label="bar", data=data, ycol=2, error_bar=eb, y_error_minus_col=3))
ar.draw(can)
tb = text_box.T(loc=(40, 130), text="This is\nimportant!", line_style=None)
tb.add_arrow((ar.x_pos(data[6][0]), ar.y_pos(data[6][1])), "cb")
tb.draw(can)

can = canvas.init("graph2.pdf")
ar = area.T(loc=(200, 0), x_range=(0,100), y_range=(0,100),
            x_axis = axis.X(label="X", tic_interval=10),
            y_axis = axis.Y(label="Y", tic_interval=10))
ar.add_plot(line_plot.T(label="foo", data=data, data_label_format="/8{}%d"),
            line_plot.T(label="bar", data=data, ycol=2))
ar.draw(can)

# Note: can.close() is called automatically for every open canvas.