paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - SVGChart library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2011 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | import math |
paul@0 | 10 | |
paul@0 | 11 | template = u"""<?xml version="1.0" encoding="%(encoding)s"?> |
paul@0 | 12 | <?xml-stylesheet href="%(styles_url)s" type="text/css"?> |
paul@0 | 13 | <svg xmlns="http://www.w3.org/2000/svg" version="1.1" |
paul@0 | 14 | width="%(width)dpx" height="%(height)dpx" viewBox="%(viewBox)s"> |
paul@0 | 15 | |
paul@0 | 16 | <g> |
paul@0 | 17 | <defs> |
paul@0 | 18 | <linearGradient id="label-gradient" x1="0%%" x2="100%%" y1="0%%" y2="100%%"> |
paul@0 | 19 | <stop offset="20%%" stop-color="#fff" stop-opacity="0" /> |
paul@0 | 20 | <stop offset="80%%" stop-color="#fff" stop-opacity="1" /> |
paul@0 | 21 | </linearGradient> |
paul@0 | 22 | </defs> |
paul@0 | 23 | %(chart)s |
paul@0 | 24 | </g> |
paul@0 | 25 | </svg> |
paul@0 | 26 | """ |
paul@0 | 27 | |
paul@0 | 28 | path_template = u"""<path class="%(class)s" d="%(data)s" /> |
paul@0 | 29 | """ |
paul@0 | 30 | |
paul@0 | 31 | circle_template = u"""<circle class="%(class)s" cx="%(x)f" cy="%(y)f" r="%(r)f" /> |
paul@0 | 32 | """ |
paul@0 | 33 | |
paul@0 | 34 | label_template = u"""<text class="%(class)s" |
paul@0 | 35 | x="%(x)f" y="%(y)f" font-size="%(font-size)s" %(rotate)s>%(label)s</text> |
paul@0 | 36 | """ |
paul@0 | 37 | |
paul@0 | 38 | rect_template = u"""<rect x="%(x)f" y="%(y)f" width="%(width)f" height="%(height)f" class="%(class)s" |
paul@0 | 39 | transform="%(transform)s" /> |
paul@0 | 40 | """ |
paul@0 | 41 | |
paul@0 | 42 | labelled_point_template = u"""<g class="point"> |
paul@0 | 43 | %(rect)s |
paul@0 | 44 | %(circle)s |
paul@0 | 45 | %(large-circle)s |
paul@0 | 46 | %(label)s |
paul@0 | 47 | %(x-line)s |
paul@0 | 48 | %(y-axis-label)s |
paul@0 | 49 | %(y-line)s |
paul@0 | 50 | %(x-axis-label)s |
paul@0 | 51 | </g> |
paul@0 | 52 | """ |
paul@0 | 53 | |
paul@0 | 54 | class Plot: |
paul@0 | 55 | |
paul@0 | 56 | "Support for plotting points and lines." |
paul@0 | 57 | |
paul@0 | 58 | def __init__(self, xmin, ymin, xmax, ymax, xmultiplier, ymultiplier): |
paul@0 | 59 | self.xmin = xmin |
paul@0 | 60 | self.ymin = ymin |
paul@0 | 61 | self.xmax = xmax |
paul@0 | 62 | self.ymax = ymax |
paul@0 | 63 | self.xmultiplier = xmultiplier |
paul@0 | 64 | self.ymultiplier = ymultiplier |
paul@0 | 65 | |
paul@0 | 66 | def get_width(self): |
paul@0 | 67 | return abs(self.xmax - self.xmin) |
paul@0 | 68 | |
paul@0 | 69 | def get_scaled_width(self): |
paul@0 | 70 | return abs((self.xmax - self.xmin) * self.xmultiplier) |
paul@0 | 71 | |
paul@0 | 72 | def get_height(self): |
paul@0 | 73 | return abs(self.ymax - self.ymin) |
paul@0 | 74 | |
paul@0 | 75 | def get_scaled_height(self): |
paul@0 | 76 | return abs((self.ymax - self.ymin) * self.ymultiplier) |
paul@0 | 77 | |
paul@0 | 78 | def get_xbase(self): |
paul@0 | 79 | return min(self.xmin * self.xmultiplier, self.xmax * self.xmultiplier) |
paul@0 | 80 | |
paul@0 | 81 | def get_ybase(self): |
paul@0 | 82 | return min(self.ymin * self.ymultiplier, self.ymax * self.ymultiplier) |
paul@0 | 83 | |
paul@0 | 84 | def xpc(self, percent): |
paul@0 | 85 | return percent * self.get_width() / 100.0 |
paul@0 | 86 | |
paul@0 | 87 | def ypc(self, percent): |
paul@0 | 88 | return percent * self.get_height() / 100.0 |
paul@0 | 89 | |
paul@0 | 90 | def scaled_xpc(self, percent): |
paul@0 | 91 | return percent * self.get_scaled_width() / 100.0 |
paul@0 | 92 | |
paul@0 | 93 | def scaled_ypc(self, percent): |
paul@0 | 94 | return percent * self.get_scaled_height() / 100.0 |
paul@0 | 95 | |
paul@0 | 96 | def scale_x(self, x): |
paul@0 | 97 | return x * self.xmultiplier |
paul@0 | 98 | |
paul@0 | 99 | def scale_y(self, y): |
paul@0 | 100 | return y * self.ymultiplier |
paul@0 | 101 | |
paul@0 | 102 | # Plotting methods. |
paul@0 | 103 | |
paul@0 | 104 | def get_points(self, points, attributes=None): |
paul@0 | 105 | circles = [] |
paul@0 | 106 | for x, y, r in points: |
paul@0 | 107 | circles.append(self.get_point(x, y, r, attributes)) |
paul@0 | 108 | return "".join(circles) |
paul@0 | 109 | |
paul@0 | 110 | def get_point(self, x, y, r, attributes=None): |
paul@0 | 111 | x, y = self._scale(x, y) |
paul@0 | 112 | attrs = {"x" : x, "y" : y, "r" : r, "class" : ""} |
paul@0 | 113 | if attributes: |
paul@0 | 114 | attrs.update(attributes) |
paul@0 | 115 | return circle_template % attrs |
paul@0 | 116 | |
paul@0 | 117 | def get_rect(self, xmin, ymin, xmax, ymax, attributes=None): |
paul@0 | 118 | xmin, ymin = self._scale(xmin, ymin) |
paul@0 | 119 | xmax, ymax = self._scale(xmax, ymax) |
paul@0 | 120 | attrs = {"x" : xmin, "y" : ymin, "width" : abs(xmax - xmin), "height" : abs(ymax - ymin)} |
paul@0 | 121 | |
paul@0 | 122 | # Flip the rectangle around (xmin, ymin) if appropriate. |
paul@0 | 123 | |
paul@0 | 124 | if xmax < xmin and ymax < ymin: |
paul@0 | 125 | attrs["transform"] = "translate(%d %d) matrix(-1 0 0 -1 0 0) translate(%d %d)" % (xmin, ymin, -xmin, -ymin) |
paul@0 | 126 | elif xmax < xmin: |
paul@0 | 127 | attrs["transform"] = "translate(%d %d) matrix(-1 0 0 1 0 0) translate(%d %d)" % (xmin, ymin, -xmin, -ymin) |
paul@0 | 128 | elif ymax < ymin: |
paul@0 | 129 | attrs["transform"] = "translate(%d %d) matrix(1 0 0 -1 0 0) translate(%d %d)" % (xmin, ymin, -xmin, -ymin) |
paul@0 | 130 | else: |
paul@0 | 131 | attrs["transform"] = "" |
paul@0 | 132 | |
paul@0 | 133 | if attributes: |
paul@0 | 134 | attrs.update(attributes) |
paul@0 | 135 | return rect_template % attrs |
paul@0 | 136 | |
paul@0 | 137 | def get_line(self, segments, attributes=None): |
paul@0 | 138 | scaled = [] |
paul@0 | 139 | for from_, to_ in segments: |
paul@0 | 140 | from_x, from_y = from_[:2] |
paul@0 | 141 | to_x, to_y = to_[:2] |
paul@0 | 142 | scaled.append((self._scale(from_x, from_y), self._scale(to_x, to_y))) |
paul@0 | 143 | attrs = {"data" : make_path_data(scaled), "class" : ""} |
paul@0 | 144 | if attributes: |
paul@0 | 145 | attrs.update(attributes) |
paul@0 | 146 | return path_template % attrs |
paul@0 | 147 | |
paul@0 | 148 | def get_label(self, x, y, text, attributes=None): |
paul@0 | 149 | x, y = self._scale(x, y) |
paul@0 | 150 | attrs = {"x" : x, "y" : y, "label" : text, "class" : "", "rotate" : ""} |
paul@0 | 151 | if attributes: |
paul@0 | 152 | attrs.update(attributes) |
paul@0 | 153 | if attrs["rotate"]: |
paul@0 | 154 | attrs["rotate"] = 'transform="translate(%d %d) rotate(%d) translate(%d %d)"' % (x, y, attrs["rotate"], -x, -y) |
paul@0 | 155 | return label_template % attrs |
paul@0 | 156 | |
paul@0 | 157 | def _scale(self, x, y): |
paul@0 | 158 | return self.scale_x(x), self.scale_y(y) |
paul@0 | 159 | |
paul@0 | 160 | class Axis: |
paul@0 | 161 | |
paul@0 | 162 | "Support for an axis with labels." |
paul@0 | 163 | |
paul@0 | 164 | example_sequence = range(0, 100, 2) |
paul@0 | 165 | |
paul@0 | 166 | def __init__(self, plot, vertical=1, position=0, sequence=example_sequence, divisions=10, minorwidth=1, majorwidth=2): |
paul@0 | 167 | |
paul@0 | 168 | "Initialise an axis." |
paul@0 | 169 | |
paul@0 | 170 | self.plot = plot |
paul@0 | 171 | self.vertical = vertical |
paul@0 | 172 | self.position = position |
paul@0 | 173 | self.sequence = sequence |
paul@0 | 174 | self.divisions = divisions |
paul@0 | 175 | self.minorwidth = minorwidth |
paul@0 | 176 | self.majorwidth = majorwidth |
paul@0 | 177 | |
paul@0 | 178 | def get_axis(self, attributes=None): |
paul@0 | 179 | |
paul@0 | 180 | "Draw the axis itself using the given, optional 'attributes'." |
paul@0 | 181 | |
paul@0 | 182 | l = [((self.position, self.sequence[0]), (self.position, self.sequence[-1]))] |
paul@0 | 183 | |
paul@0 | 184 | # For each step, mark the axis with minor and major markings. |
paul@0 | 185 | |
paul@0 | 186 | for i, value in enumerate(self.sequence): |
paul@0 | 187 | if i % self.divisions == 0: |
paul@0 | 188 | markwidth = self.majorwidth |
paul@0 | 189 | else: |
paul@0 | 190 | markwidth = self.minorwidth |
paul@0 | 191 | |
paul@0 | 192 | l.append(( |
paul@0 | 193 | (self.position - markwidth, value), |
paul@0 | 194 | (self.position + markwidth, value) |
paul@0 | 195 | )) |
paul@0 | 196 | |
paul@0 | 197 | # Transpose coordinates for horizontal axes, if necessary. |
paul@0 | 198 | |
paul@0 | 199 | l = [self._transpose_segment(segment) for segment in l] |
paul@0 | 200 | |
paul@0 | 201 | # Return the path. |
paul@0 | 202 | |
paul@0 | 203 | return self.plot.get_line(l, attributes) |
paul@0 | 204 | |
paul@0 | 205 | def get_labels(self, attributes=None, labeller=None, rotate=None): |
paul@0 | 206 | |
paul@0 | 207 | """ |
paul@0 | 208 | Plot the labels using the given, optional 'attributes' and 'labeller' |
paul@0 | 209 | callable. |
paul@0 | 210 | """ |
paul@0 | 211 | |
paul@0 | 212 | labeller = labeller or (lambda x: x) |
paul@0 | 213 | |
paul@0 | 214 | # Label each major marking. |
paul@0 | 215 | |
paul@0 | 216 | l = [] |
paul@0 | 217 | |
paul@0 | 218 | for i, value in enumerate(self.sequence): |
paul@0 | 219 | if i % self.divisions == 0: |
paul@0 | 220 | l.append(( |
paul@0 | 221 | (self.position - self.majorwidth * 2, value), |
paul@0 | 222 | labeller(value) |
paul@0 | 223 | )) |
paul@0 | 224 | |
paul@0 | 225 | # Transpose coordinates for horizontal axes, if necessary. |
paul@0 | 226 | |
paul@0 | 227 | l = [self._transpose_label(label) for label in l] |
paul@0 | 228 | |
paul@0 | 229 | # Return the labels. |
paul@0 | 230 | |
paul@0 | 231 | labels = [] |
paul@0 | 232 | for (x, y), label in l: |
paul@0 | 233 | attrs = {} |
paul@0 | 234 | if attributes: |
paul@0 | 235 | attrs.update(attributes) |
paul@0 | 236 | attrs["class"] = attrs.get("class", "") + (self.vertical and " y-axis" or " x-axis") |
paul@0 | 237 | if rotate: |
paul@0 | 238 | attrs["rotate"] = rotate |
paul@0 | 239 | labels.append(self.plot.get_label(x, y, label, attrs)) |
paul@0 | 240 | |
paul@0 | 241 | return "".join(labels) |
paul@0 | 242 | |
paul@0 | 243 | def _transpose_label(self, label): |
paul@0 | 244 | if self.vertical: |
paul@0 | 245 | return label |
paul@0 | 246 | (from_x, from_y), text = label |
paul@0 | 247 | return (from_y, from_x), text |
paul@0 | 248 | |
paul@0 | 249 | def _transpose_segment(self, segment): |
paul@0 | 250 | if self.vertical: |
paul@0 | 251 | return segment |
paul@0 | 252 | (from_x, from_y), (to_x, to_y) = segment |
paul@0 | 253 | return (from_y, from_x), (to_y, to_x) |
paul@0 | 254 | |
paul@0 | 255 | # Plotting functions. |
paul@0 | 256 | |
paul@0 | 257 | def get_labelled_points(plot, x_axis, y_axis, points, offset_x, offset_y, font_height, axis_label_x=0, axis_label_y=0, attributes=None): |
paul@0 | 258 | |
paul@0 | 259 | circles = [] |
paul@0 | 260 | for x, y, r, text, y_axis_label, x_axis_label in points: |
paul@0 | 261 | circles.append(get_labelled_point(plot, x_axis, y_axis, x, y, r, text, offset_x, offset_y, font_height, |
paul@0 | 262 | y_axis_label, x_axis_label, axis_label_x, axis_label_y, attributes)) |
paul@0 | 263 | return "".join(circles) |
paul@0 | 264 | |
paul@0 | 265 | def get_labelled_point(plot, x_axis, y_axis, x, y, r, text, offset_x, offset_y, font_height, y_axis_label="", x_axis_label="", |
paul@0 | 266 | axis_label_x=0, axis_label_y=0, attributes=None): |
paul@0 | 267 | |
paul@0 | 268 | if x > x_axis.position: |
paul@0 | 269 | axis_label_x = x_axis.position - axis_label_x |
paul@0 | 270 | y_axis_label_class = "west" |
paul@0 | 271 | else: |
paul@0 | 272 | axis_label_x = x_axis.position + axis_label_x |
paul@0 | 273 | y_axis_label_class = "east" |
paul@0 | 274 | |
paul@0 | 275 | if y > y_axis.position: |
paul@0 | 276 | axis_label_y = y_axis.position - axis_label_y |
paul@0 | 277 | x_axis_label_class = "south" |
paul@0 | 278 | else: |
paul@0 | 279 | axis_label_y = y_axis.position + axis_label_y |
paul@0 | 280 | x_axis_label_class = "north" |
paul@0 | 281 | |
paul@0 | 282 | circle = plot.get_point(x, y, r, attributes) |
paul@0 | 283 | label = plot.get_label(x + offset_x, y + offset_y, text, attributes) |
paul@0 | 284 | |
paul@0 | 285 | # Make a special active area for the point details. |
paul@0 | 286 | |
paul@0 | 287 | if text: |
paul@0 | 288 | attrs = {"class" : ""} |
paul@0 | 289 | if attributes: |
paul@0 | 290 | attrs.update(attributes) |
paul@0 | 291 | attrs["class"] += " active" |
paul@0 | 292 | rect = plot.get_rect(x, y - offset_y, axis_label_x, y + offset_y, attrs) + \ |
paul@0 | 293 | plot.get_rect(x - offset_x, y, x + offset_x, axis_label_y, attrs) |
paul@0 | 294 | else: |
paul@0 | 295 | rect = "" |
paul@0 | 296 | |
paul@0 | 297 | # Make a special large point. |
paul@0 | 298 | |
paul@0 | 299 | attrs = {"class" : ""} |
paul@0 | 300 | if attributes: |
paul@0 | 301 | attrs.update(attributes) |
paul@0 | 302 | attrs["class"] += " large" |
paul@0 | 303 | large_circle = plot.get_point(x, y, r * 2, attrs) |
paul@0 | 304 | |
paul@0 | 305 | # Make lines intersecting the y-axis and x-axis. |
paul@0 | 306 | |
paul@0 | 307 | attrs = {"class" : ""} |
paul@0 | 308 | if attributes: |
paul@0 | 309 | attrs.update(attributes) |
paul@0 | 310 | attrs["class"] += " to-axis" |
paul@0 | 311 | x_line = plot.get_line([((axis_label_x, y), (x, y))], attrs) |
paul@0 | 312 | y_line = plot.get_line([((x, axis_label_y), (x, y))], attrs) |
paul@0 | 313 | |
paul@0 | 314 | # Make axis labels. |
paul@0 | 315 | |
paul@0 | 316 | attrs = {} |
paul@0 | 317 | if attributes: |
paul@0 | 318 | attrs.update(attributes) |
paul@0 | 319 | classes = attrs.get("class", "") |
paul@0 | 320 | |
paul@0 | 321 | if y_axis_label: |
paul@0 | 322 | attrs["class"] = classes + " axis-label %s" % y_axis_label_class |
paul@0 | 323 | y_axis_label = plot.get_label(axis_label_x, y, y_axis_label, attrs) |
paul@0 | 324 | else: |
paul@0 | 325 | y_axis_label = "" |
paul@0 | 326 | |
paul@0 | 327 | if x_axis_label: |
paul@0 | 328 | attrs["class"] = classes + " axis-label %s" % x_axis_label_class |
paul@0 | 329 | x_axis_label = plot.get_label(x, axis_label_y, x_axis_label, attrs) |
paul@0 | 330 | else: |
paul@0 | 331 | x_axis_label = "" |
paul@0 | 332 | |
paul@0 | 333 | # Combine the different parts. |
paul@0 | 334 | |
paul@0 | 335 | attrs = {"circle" : circle, "label" : label, "large-circle" : large_circle, "rect" : rect, |
paul@0 | 336 | "x-line" : x_line, "x-axis-label" : x_axis_label, "y-line" : y_line, "y-axis-label" : y_axis_label} |
paul@0 | 337 | if attributes: |
paul@0 | 338 | attrs.update(attributes) |
paul@0 | 339 | return labelled_point_template % attrs |
paul@0 | 340 | |
paul@0 | 341 | # Utility functions. |
paul@0 | 342 | |
paul@0 | 343 | def make_path_data(segments): |
paul@0 | 344 | parts = [] |
paul@0 | 345 | for (from_x, from_y), (to_x, to_y) in segments: |
paul@0 | 346 | parts.append("M %f,%f" % (from_x, from_y)) |
paul@0 | 347 | parts.append("L %f,%f" % (to_x, to_y)) |
paul@0 | 348 | return " ".join(parts) + " z" |
paul@0 | 349 | |
paul@0 | 350 | def segments_from_points(points): |
paul@0 | 351 | segments = [] |
paul@0 | 352 | last = None |
paul@0 | 353 | for point in points: |
paul@0 | 354 | if last is not None: |
paul@0 | 355 | segments.append((last, point)) |
paul@0 | 356 | last = point |
paul@0 | 357 | return segments |
paul@0 | 358 | |
paul@0 | 359 | def round(x, interval, direction=1): |
paul@0 | 360 | q, r = divmod(x, interval) |
paul@0 | 361 | if direction == -1 and q < 0: |
paul@0 | 362 | direction = 0 |
paul@0 | 363 | return (q + direction) * interval |
paul@0 | 364 | |
paul@0 | 365 | def frange(start, stop, step): |
paul@0 | 366 | value = start |
paul@0 | 367 | l = [] |
paul@0 | 368 | while value < stop: |
paul@0 | 369 | l.append(value) |
paul@0 | 370 | value += step |
paul@0 | 371 | return l |
paul@0 | 372 | |
paul@0 | 373 | def get_chart(data, chart_width=900, chart_height=None, xorigin=0, yorigin=0, xdivisions=10, ydivisions=10, encoding="utf-8", |
paul@0 | 374 | styles_url=""): |
paul@0 | 375 | |
paul@0 | 376 | new_data = [] |
paul@0 | 377 | for t in data: |
paul@0 | 378 | x, y = t[:2] |
paul@0 | 379 | new_data.append([float(x), float(y)] + t[2:]) |
paul@0 | 380 | data = new_data |
paul@0 | 381 | |
paul@0 | 382 | # Get the data properties. |
paul@0 | 383 | |
paul@0 | 384 | min_x = min([t[0] for t in data]) |
paul@0 | 385 | min_y = min([t[1] for t in data]) |
paul@0 | 386 | max_x = max([t[0] for t in data]) |
paul@0 | 387 | max_y = max([t[1] for t in data]) |
paul@0 | 388 | |
paul@0 | 389 | # Get the chart properties. |
paul@0 | 390 | |
paul@0 | 391 | xinterval = math.pow(10, math.floor(math.log10(max(abs(max_x), abs(min_x))))) |
paul@0 | 392 | yinterval = math.pow(10, math.floor(math.log10(max(abs(max_y), abs(min_y))))) |
paul@0 | 393 | |
paul@0 | 394 | xmin, xmax = round(min_x, xinterval, -1), round(max_x, xinterval) |
paul@0 | 395 | ymin, ymax = round(min_y, yinterval, -1), round(max_y, yinterval) |
paul@0 | 396 | |
paul@0 | 397 | xstep = float(xinterval) / xdivisions |
paul@0 | 398 | ystep = float(yinterval) / ydivisions |
paul@0 | 399 | |
paul@0 | 400 | xsequence = frange(min(xorigin, xmin), max(xorigin, xmax) + xstep, xstep) |
paul@0 | 401 | ysequence = frange(min(xorigin, ymin), max(yorigin, ymax) + ystep, ystep) |
paul@0 | 402 | |
paul@0 | 403 | xmultiplier = float(ymax) / xmax |
paul@0 | 404 | ymultiplier = -1 |
paul@0 | 405 | |
paul@0 | 406 | # Initialise the chart components. |
paul@0 | 407 | |
paul@0 | 408 | plot = Plot(xmin, ymin, xmax, ymax, xmultiplier, ymultiplier) |
paul@0 | 409 | |
paul@0 | 410 | axis_label_x = plot.xpc(20) |
paul@0 | 411 | axis_label_y = plot.ypc(15) |
paul@0 | 412 | |
paul@0 | 413 | width = plot.get_width() |
paul@0 | 414 | height = plot.get_height() |
paul@0 | 415 | |
paul@0 | 416 | x_axis = Axis(plot, 0, yorigin, xsequence, xdivisions, plot.ypc(0.5), plot.ypc(1)) |
paul@0 | 417 | y_axis = Axis(plot, 1, xorigin, ysequence, ydivisions, plot.xpc(0.5), plot.xpc(1)) |
paul@0 | 418 | |
paul@0 | 419 | # Render the chart. |
paul@0 | 420 | |
paul@0 | 421 | all_elements = [] |
paul@0 | 422 | |
paul@0 | 423 | # Point radius. |
paul@0 | 424 | |
paul@0 | 425 | radius = plot.scaled_xpc(1) |
paul@0 | 426 | |
paul@0 | 427 | # Label position. |
paul@0 | 428 | |
paul@0 | 429 | labelx, labely = plot.xpc(2), -plot.ypc(2) |
paul@0 | 430 | font_height = -plot.ypc(4) |
paul@0 | 431 | |
paul@0 | 432 | text_styles = {"font-size" : plot.scaled_xpc(4)} |
paul@0 | 433 | |
paul@0 | 434 | # Render the axes. |
paul@0 | 435 | # NOTE: Should detect the precision in the data and label appropriately. |
paul@0 | 436 | |
paul@0 | 437 | labeller = (lambda x: "%.2f" % x) |
paul@0 | 438 | styles = {"class" : "axis"} |
paul@0 | 439 | |
paul@0 | 440 | all_elements.append(y_axis.get_axis(styles)) |
paul@0 | 441 | all_elements.append(y_axis.get_labels(text_styles, labeller)) |
paul@0 | 442 | |
paul@0 | 443 | label_styles = {} |
paul@0 | 444 | label_styles.update(text_styles) |
paul@0 | 445 | |
paul@0 | 446 | all_elements.append(x_axis.get_axis(styles)) |
paul@0 | 447 | all_elements.append(x_axis.get_labels(label_styles, labeller)) |
paul@0 | 448 | |
paul@0 | 449 | # Render the data. |
paul@0 | 450 | |
paul@0 | 451 | points = [] |
paul@0 | 452 | for t in data: |
paul@0 | 453 | x, y = t[:2] |
paul@0 | 454 | if len(t) > 2: |
paul@0 | 455 | label_text = t[2] |
paul@0 | 456 | else: |
paul@0 | 457 | label_text = "(%s, %s)" % (x, y) |
paul@0 | 458 | points.append((x, y, radius, label_text, y, x)) |
paul@0 | 459 | |
paul@0 | 460 | # Render the line and labelled points. |
paul@0 | 461 | |
paul@0 | 462 | styles = {} |
paul@0 | 463 | styles.update(text_styles) |
paul@0 | 464 | |
paul@0 | 465 | all_elements.append(plot.get_line(segments_from_points(points), styles)) |
paul@0 | 466 | |
paul@0 | 467 | all_elements.append(get_labelled_points(plot, x_axis, y_axis, points, labelx, labely, font_height, |
paul@0 | 468 | axis_label_x, axis_label_y, attributes=styles)) |
paul@0 | 469 | |
paul@0 | 470 | # Work out the extent of the chart. |
paul@0 | 471 | |
paul@0 | 472 | left = min(plot.get_xbase(), plot.scale_x(xorigin)) - plot.scaled_xpc(40) |
paul@0 | 473 | left_to_right = plot.scaled_xpc(180) |
paul@0 | 474 | bottom = min(plot.get_ybase(), plot.scale_y(yorigin)) - plot.scaled_ypc(10) |
paul@0 | 475 | bottom_to_top = plot.scaled_ypc(180) |
paul@0 | 476 | |
paul@0 | 477 | if chart_width is not None: |
paul@0 | 478 | if chart_height is None: |
paul@0 | 479 | chart_height = chart_width / left_to_right * bottom_to_top |
paul@0 | 480 | else: |
paul@0 | 481 | if chart_height is not None: |
paul@0 | 482 | chart_width = chart_height / bottom_to_top * left_to_right |
paul@0 | 483 | |
paul@0 | 484 | # Write the SVG file. |
paul@0 | 485 | |
paul@0 | 486 | return template % { |
paul@0 | 487 | "width" : int(chart_width), |
paul@0 | 488 | "height" : int(chart_height), |
paul@0 | 489 | "viewBox" : "%d %d %d %d" % ( |
paul@0 | 490 | left, bottom, left_to_right, bottom_to_top, |
paul@0 | 491 | ), |
paul@0 | 492 | "chart" : "".join(all_elements), |
paul@0 | 493 | "styles_url" : styles_url, |
paul@0 | 494 | "encoding" : encoding |
paul@0 | 495 | } |
paul@0 | 496 | |
paul@0 | 497 | # vim: tabstop=4 expandtab shiftwidth=4 |