[jToolkit-cvs] jToolkit/widgets chart.py,1.9,1.10
Brought to you by:
davidfraser,
friedelwolff
From: <dav...@us...> - 2004-02-10 08:59:59
|
Update of /cvsroot/jtoolkit/jToolkit/widgets In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23519 Modified Files: chart.py Log Message: Changed to use matplotlib instead of gdchart Index: chart.py =================================================================== RCS file: /cvsroot/jtoolkit/jToolkit/widgets/chart.py,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** chart.py 10 Feb 2004 08:29:40 -0000 1.9 --- chart.py 10 Feb 2004 08:56:46 -0000 1.10 *************** *** 3,15 **** # per object. ! import gdchart ! import cStringIO from jToolkit.widgets import widgets from jToolkit.data import dates - # 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() #We need this lock for gdchart, as it's not thread-safe --- 3,11 ---- # per object. ! from matplotlib.matlab import * ! import cStringIO, tempfile, os, math from jToolkit.widgets import widgets from jToolkit.data import dates #We need this lock for gdchart, as it's not thread-safe *************** *** 17,20 **** --- 13,20 ---- gdchartLock = Lock() + #Graph type constants + LINE_CHART = 0 + BAR_CHART = 1 + # This class is just a container for option values. class Chart(widgets.PlainContents): *************** *** 23,31 **** self.filter = filter if not hasattr(self, 'chartType'): ! self.chartType = gdchart.GDC_LINE ! if not hasattr(self, 'chartSize'): ! self.chartSize = (250,250) ! # Get a copy of the default options ! self.options = gdchart.defaults.copy() self.content_type = 'image/png' widgets.PlainContents.__init__(self, []) --- 23,33 ---- self.filter = filter if not hasattr(self, 'chartType'): ! self.chartType = LINE_CHART ! if not hasattr(self, 'dpi'): ! self.dpi = 50 ! ! self.figure = new_figure_manager(1,(8,6),72) ! ! self.options = {} self.content_type = 'image/png' widgets.PlainContents.__init__(self, []) *************** *** 37,40 **** --- 39,46 ---- self.ydata = [] + def getimage(self): + """Subclasses of this should implement this uniquely""" + return '' + def option(self, **args): # Save option values in the object's dictionary. *************** *** 44,56 **** # 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): gdchartLock.acquire() self.draw() ! tempfile = cStringIO.StringIO() ! gdchart.chart(self.chartType, self.chartSize, tempfile, self.xdata, *self.ydata) gdchartLock.release() ! return tempfile.getvalue() class LineChart(Chart): --- 50,75 ---- # Put options into effect. Derived class implementations of draw() # should call this method before calling gdchart.chart(). ! for key in self.options.keys(): ! set(self.figure.get_current_axis(),key,self.options[key]) ! ! def getimagefromtempfile(self): ! #Save temporary file ! # tempfilename = os.path.join(tempfile.gettempdir(),'temp.png') ! tempfilename = 'temp.png' ! self.figure.figure.print_figure(tempfilename,self.dpi) ! ! #Read and report ! f = open(tempfilename,'rb') ! img = f.read() ! f.close() ! return img def gethtml(self): gdchartLock.acquire() self.draw() ! self.drawimage() ! img = self.getimagefromtempfile() gdchartLock.release() ! return img class LineChart(Chart): *************** *** 70,80 **** self.ydata = [[row[ycolumn] for row in chartdata] for ycolumn in self.ycolumns] class CurrentValueLegendChart(Chart): """This class creates a bar chart which acts as a legend and a current value reporter""" def __init__(self, charttable, xcolumns, colours, filter=None): self.xcolumns = xcolumns ! self.chartType = gdchart.GDC_BAR Chart.__init__(self, charttable, filter) ! self.option(ext_color=colours,ylabel_density=10,title="Legend") def getdata(self): --- 89,129 ---- self.ydata = [[row[ycolumn] for row in chartdata] for ycolumn in self.ycolumns] + def drawimage(self): + #For now, x axis will be evenly spaced + #TODO: x axis spacing should be according to time + + #Set the min/max of each axis + ymin = sys.maxint + ymax = -sys.maxint+1 + for dataset in self.ydata: + for value in dataset: + if value < ymin: + ymin = value + if value > ymax: + ymax = value + + self.figure.get_current_axis().set_xlim([0,len(self.xdata)+1]) + self.figure.get_current_axis().set_ylim([math.floor(ymin),math.ceil(ymax)]) + + #Set the x labels + set(self.figure.get_current_axis(), 'xticks', arange(len(self.xdata))) + set(self.figure.get_current_axis(), 'xticklabels', self.xdata) + + #Plot each dataset + for dataset in self.ydata: + self.figure.get_current_axis().plot(dataset) + + class CurrentValueLegendChart(Chart): """This class creates a bar chart which acts as a legend and a current value reporter""" def __init__(self, charttable, xcolumns, colours, filter=None): self.xcolumns = xcolumns ! self.chartType = BAR_CHART Chart.__init__(self, charttable, filter) ! ! #Turn colours into an array exactly len(self.xcolumns) long ! repeatcolours = len(self.xcolumns) / len(colours) ! endcolours = len(self.xcolumns) % len(colours) ! self.colours = colours*repeatcolours + colours[:endcolours] def getdata(self): *************** *** 84,86 **** self.ydata = [[finalrow[xcolumn] for xcolumn in self.xcolumns]] ! \ No newline at end of file --- 133,151 ---- self.ydata = [[finalrow[xcolumn] for xcolumn in self.xcolumns]] ! def drawimage(self): ! #Set the min/max of each axis ! ymin = sys.maxint ! ymax = -sys.maxint+1 ! for value in self.ydata[0]: ! if value < ymin: ! ymin = value ! if value > ymax: ! ymax = value ! ! self.figure.get_current_axis().set_xlim([0,len(self.xdata)+1]) ! self.figure.get_current_axis().set_ylim([math.floor(ymin),math.ceil(ymax)]) ! set(self.figure.get_current_axis(),'xticks',arange(len(self.xdata))+0.25) ! set(self.figure.get_current_axis(),'xticklabels',self.xdata) ! ! self.figure.get_current_axis().bar(arange(len(self.xdata)),self.ydata[0],0.5,color=self.colours) ! |