[jToolkit-cvs] jToolkit/widgets chart.py,NONE,1.1
Brought to you by:
davidfraser,
friedelwolff
From: <dav...@us...> - 2004-02-10 07:27:56
|
Update of /cvsroot/jtoolkit/jToolkit/widgets In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8577 Added Files: chart.py Log Message: New chart widget which creates and displays a PNG of a chart --- NEW FILE: chart.py --- # A simple wrapper class for the gdchart module. This code demonstrates one # way to keep separate dictionaries of option values, in this case, one copy # per object. import gdchart import cStringIO from jToolkit.widgets import widgets # Save the default option values in a module-level dictionary. This way, each # new instance of the Chart class can begin life with its own copy of the # default option values. gdchart.defaults = gdchart.option() # This class is just a container for option values. class SimpleChart(widgets.PlainContents): def __init__(self, charttable, xcolumn, ycolumns): self.charttable = charttable self.xcolumn = xcolumn self.ycolumns = ycolumns # Get a copy of the default options self.options = gdchart.defaults.copy() self.content_type = 'image/png' widgets.PlainContents.__init__(self, []) self.getdata() def getdata(self): chartdata = self.charttable.gettablerows() self.xdata = [str(row[self.xcolumn]) for row in chartdata] self.ydata = [[row[ycolumn] for row in chartdata] for ycolumn in self.ycolumns] def option(self, **args): # Save option values in the object's dictionary. self.options.update(args) def draw(self): # Put options into effect. Derived class implementations of draw() # should call this method before calling gdchart.chart(). apply(gdchart.option, (), self.options) def gethtml(self): self.draw() tempfile = cStringIO.StringIO() gdchart.chart(gdchart.GDC_LINE, (250, 250), tempfile, self.xdata, *self.ydata) return tempfile.getvalue() if __name__ == '__main__': # An example derived class. class PieChart(Chart): def __init__(self): Chart.__init__(self) def draw(self, size, file, labels, *data): Chart.draw(self) args = (gdchart.GDC_3DPIE, size, file, labels) + data apply(gdchart.chart, args) c = PieChart() c.option(pie_color=(0xff8080, 0x80ff80, 0x8080ff, 0xffff80, 0xff80ff, 0x80ffff)) c.option(edge_color=0x0, line_color=0xffffff) c.option(label_line=1) c.option(explode=(0,0,0,0,0,15)) c.option(title='Have Some Pie') cities = ('Chicago', 'New York', 'L.A.', 'Atlanta', 'Paris, MD\n(USA)', 'London') values = (1.9, 1.3, 1.2, 0.75, 0.1, 2.0) c.draw((250, 250), 'pie.png', cities, values) print 'See pie.png' |