Permalink
Browse files

Feature: "`linear_x`"

---------------------

Since the X axis is often a bunch of date strings (or maybe date objects), we may want labels that are some human-readable text representation of a date (instead of a huge number, like unix epoch).

We take advantage of the fact that in these cases, the X values are evenly spaced along the axis. We just calculate the distance between them (which is constant) and pick labels out of the X values supplied "every N" values.
  • Loading branch information...
stnbu committed Sep 3, 2018
1 parent 02e31fe commit dab44717655fe7b1bcefcb85b9956decf997e9a5
Showing with 13 additions and 6 deletions.
  1. +13 −6 svg_graph/base.py
@@ -5,11 +5,11 @@
class LineGraph(object):
def __init__(self, title, points, height=400, width=600, labels=None, normalize=True):
def __init__(self, title, points, height=400, width=600, labels=None, normalize=True, linear_x=False):
self.title = title
self.height = height
self.width = width
self.linear_x = linear_x
if normalize:
self._raw_points = points
@@ -39,14 +39,21 @@ def make_labels(self):
return GraphLabel('X', x_labels, 100), GraphLabel('Y', y_labels, 100)
def map_to_scale(self, points):
x_min = min([x for x, _ in points])
x_max = max([x for x, _ in points])
if self.linear_x:
x_unit = 1 / len(points)
else:
x_min = min([x for x, _ in points])
x_max = max([x for x, _ in points])
y_min = min([y for _, y in points])
y_max = max([y for _, y in points])
_points = []
for x, y in points:
x_mul = 1 - (x_max - x) / (x_max - x_min)
for i, point in enumerate(points):
x, y = point
if self.linear_x:
x_mul = i * x_unit
else:
x_mul = 1 - (x_max - x) / (x_max - x_min)
y_mul = 1 - (y_max - y) / (y_max - y_min)
_points.append((x_mul * self.width, y_mul * self.height))
return _points

0 comments on commit dab4471

Please sign in to comment.