""" svg_graph.py Create an SVG version of a graph, e.g. Boston Chicago New York """ svg_header = """ """ svg_footer = """ """ class SvgGraph(object): def __init__(self, scale=1.0): self.scale = scale def write(self, xml, filename="graph"): """ Write the given xml to a file; default name is 'graph.svg'. """ if not filename.endswith('.svg'): filename += '.svg' svg_file = open(filename, 'w') svg_file.write(xml) svg_file.close() def window_coords(self, *xy): return map(lambda z: self.scale * z, xy) def header(self): return svg_header def footer(self): return svg_footer def line(self, x1, y1, x2, y2, color='grey', width=1): (x1, y1, x2, y2) = self.window_coords(x1, y1, x2, y2) return (""" \n""") % \ (x1, y1, x2, y2, color, width) def dot(self, x, y, color='red', radius=5): (x, y) = self.window_coords(x, y) return (""" \n""") % \ (x, y, radius, color) def text(self, string, x, y): (x, y) = self.window_coords(x, y) return (""" %s\n""") % \ (x, y, string)