[cairo] Contributing pyCairoChart

Steven Chaplin stevech1097 at yahoo.com.au
Wed May 24 20:03:17 PDT 2006


On Tue, 2006-05-23 at 22:54 +0200, Martin Lesser wrote:
> Hi all,
> 
> looking for a python-module to create dynamic charts for several weeks
> with no success resulted in creating my own module based on cairo:
> 
>   http://bettercom.de/de/pycairochart
> 
> For me it works like demanded, comments and more are welcome.
> 
> Regards
> 
> Martin

It looks very good.
Here's how I would modify it to support output to PNG/PS/PDF/SVG:

Make the draw_bars/data/dots/grid/lines methods private, so the public
API is just to create a Chart, set its attributes, and output the chart.

img = CairoChart.Chart()
img.set_title()
img.set_legend()
img.set_font()
img.set_color()
img.set_data()
img.out2png('cairochart03.png')

draw_*() methods are not explicitly called, the drawing is done
automatically by the out2png() method.

Modify Chart.__init__ so it does not create the cairo surface, context
and matrix.

Rename out2png() to output() and use something like:

def output (self, _file):
    if _file.endswith('.png'):
        self.img = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                                      self.width, self.height)
    elif _file.endswith('.ps'):
        if not cairo.HAS_PS_SURFACE:
            raise SystemExit ('cairo was not compiled with PS support')
        self.img = cairo.PSSurface (_file, width_in_points, 
                                     height_in_points)
    elif _file.endswith('.pdf'):
        if not cairo.HAS_PDF_SURFACE:
            raise SystemExit ('cairo was not compiled with PDF support')
        self.img = cairo.PDFSurface (_file, width_in_points, 
                                     height_in_points)
    elif _file.endswith('.svg'):
        if not cairo.HAS_SVG_SURFACE:
            raise SystemExit ('cairo was not compiled with SVG support')
        self.img = cairo.SVGSurface (_file, width_in_points, 
                                     height_in_points)

    self.ctx = cairo.Context(self.img)
    # draw the chart ...
    # save the result:
    # for PNG: self.img.write_to_png(_file)
    # for PS/PDF/SVG: self.ctx.show_page()

Regards,
Steve

Send instant messages to your online friends http://au.messenger.yahoo.com 


More information about the cairo mailing list