Back to index

moin  1.9.0~rc2
chart.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Charts
00004 
00005     This is a wrapper for the "gdchart" module.
00006 
00007     Example:
00008         import random
00009         c = Chart()
00010         c.addData(ChartData([random.gauss(0, 5.0) for i in range(20)], color='blue'))
00011         c.option(title = 'gdchart Demo')
00012         c.draw(Chart.GDC_LINE, (600, 300), 'test.gif')
00013 
00014     @copyright: 2002-2004 Juergen Hermann <jh@web.de>
00015     @license: GNU GPL, see COPYING for details.
00016 """
00017 
00018 import gdchart
00019 from MoinMoin.util.web import Color
00020 
00021 
00022 class ChartData:
00023     """ Data set for one line in a chart, including
00024         properties like the color of that line.
00025     """
00026     def __init__(self, data, color='black'):
00027         """
00028         Create a data set.
00029 
00030         @param data: tuple / list of numbers
00031         @param color: rendering color (triple, '#RRGGBB' or color name)
00032         """
00033         self.data = data
00034         self.color = color
00035 
00036 
00037 class Chart:
00038     """ Wrapper for "gdchart".
00039 
00040         All GDC* constants are available as class attributes.
00041     """
00042 
00043     DEFAULTS = gdchart.option()
00044 
00045     def __init__(self):
00046         # Get a copy of the default options
00047         self.options = self.DEFAULTS.copy()
00048         self.datasets = []
00049 
00050         self.option(
00051             bg_color=0xffffff,
00052             line_color=0x000000,
00053         )
00054 
00055     def addData(self, data):
00056         self.datasets.append(data)
00057 
00058     def option(self, **args):
00059         # Save option values in the object's dictionary.
00060         self.options.update(args)
00061 
00062     def draw(self, style, size, output, labels=None):
00063         args = []
00064         colors = []
00065         for dataset in self.datasets:
00066             if isinstance(dataset, ChartData):
00067                 args.append(dataset.data)
00068                 colors.append(dataset.color)
00069             else:
00070                 args.append(dataset)
00071                 colors.append('black')
00072 
00073         # Default for X axis labels (numbered 1..n)
00074         if not labels:
00075             labels = [str(i) for i in range(1, len(args[0])+1)]
00076 
00077         # set colors for the data sets
00078         if colors:
00079             self.option(set_color=[int(Color(c)) for c in colors])
00080 
00081         # pass options to gdchart and render the chart
00082         gdchart.option(**self.options)
00083 
00084         # limit label length in order to workaround bugs in gdchart
00085         labels = [x[:32] for x in labels]
00086         gdchart.chart(*((style, size, output, labels) + tuple(args)))
00087 
00088 
00089 # copy GDC constants to Chart's namespace
00090 for key, val in vars(gdchart).items():
00091     if key.startswith('GDC'):
00092         setattr(Chart, key, val)
00093 
00094 
00095 if __name__ == "__main__":
00096     import os, sys, random
00097     c = Chart()
00098     c.addData(ChartData([random.randrange(0, i+1) for i in range(20)], color='green'))
00099     c.addData(ChartData([random.gauss(30, 5.0) for i in range(20)], color='blue'))
00100     c.option(
00101         title='gdchart Demo',
00102         xtitle='X axis',
00103         ytitle='random values',
00104     )
00105     c.draw(Chart.GDC_LINE, (600, 300), 'test.gif')
00106     if sys.platform == "win32":
00107         os.system("explorer test.gif")
00108     else:
00109         os.system("display test.gif &")
00110