# plotlib.py, svg plot generator by Reinier Heeres # # This program 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 of the License, or # (at your option) any later version. # # This program 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. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # Change log: # 2007-09-04: rwh, first version import types import logging _logger = logging.getLogger('PlotLib') USE_MPL = True class _PlotBase: """Class to generate an svg plot for a function. Evaluation of values is done using the EqnParser class.""" def __init__(self, parser): self.svg_data = "" self.parser = parser def get_svg(self): return self.svg_data def set_svg(self, data): self.svg_data = data def evaluate(self, eqn, var, range, points=100): x_old = self.parser.get_var(var) if type(eqn) in (types.StringType, types.UnicodeType): eqn = self.parser.parse(eqn) res = [] d = float((range[1] - range[0])) / (points - 1) x = range[0] while points > 0: self.parser.set_var(var, x) ret = self.parser.evaluate(eqn) if ret is not None: v = float(ret) else: v = 0 res.append((x, v)) x += d points -= 1 self.parser.set_var(var, x_old) return res def export_plot(self, fn): f = open(fn, "w") f.write(self.get_svg()) f.close() def produce_plot(self, vals, *args, **kwargs): '''Function to produce the actual plot, override.''' pass def plot(self, eqn, **kwargs): ''' Plot function . kwargs can contain: 'points' The last item in kwargs is interpreted as the variable that should be varied. ''' _logger.debug('plot(): %r, %r', eqn, kwargs) points = kwargs.pop('points', 100) if len(kwargs) > 1: _logger.error('Too many variables specified') return None for var, range in kwargs.iteritems(): _logger.info('Plot range for var %s: %r', var, range) vals = self.evaluate(eqn, var, range, points=points) _logger.debug('vals are %r', vals) svg = self.produce_plot(vals, xlabel=var, ylabel='f(x)') _logger.debug('SVG Data: %s', svg) self.set_svg(svg) # self.export_plot("/tmp/calculate_graph.svg") if type(svg) is types.UnicodeType: return svg.encode('utf-8') else: return svg class CustomPlot(_PlotBase): def __init__(self, parser): _PlotBase.__init__(self, parser) self.set_size(0, 0) def set_size(self, width, height): self.width = width self.height = height def create_image(self): self.svg_data = '\n' self.svg_data += '\n' self.svg_data += '\n' % (self.width, self.height) def finish_image(self): self.svg_data += '' def plot_line(self, c0, c1, col): c0 = self.rcoords_to_coords(c0) c1 = self.rcoords_to_coords(c1) self.svg_data += '\n' % (col, c0[0], c0[1], c1[0], c1[1]) def plot_polyline(self, coords, col): self.svg_data += '\n' def add_text(self, c, text, rotate=0): if type(text) is types.UnicodeType: text = text.encode('utf-8') c = self.rcoords_to_coords(c) self.svg_data += '