Skip to content

Commit

Permalink
Feature: "linear_x"
Browse files Browse the repository at this point in the history
---------------------

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 dab4471
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions svg_graph/base.py
Expand Up @@ -5,11 +5,11 @@


class LineGraph(object): 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.title = title
self.height = height self.height = height
self.width = width self.width = width
self.linear_x = linear_x


if normalize: if normalize:
self._raw_points = points self._raw_points = points
Expand Down Expand Up @@ -39,14 +39,21 @@ def make_labels(self):
return GraphLabel('X', x_labels, 100), GraphLabel('Y', y_labels, 100) return GraphLabel('X', x_labels, 100), GraphLabel('Y', y_labels, 100)


def map_to_scale(self, points): def map_to_scale(self, points):
x_min = min([x for x, _ in points]) if self.linear_x:
x_max = max([x for x, _ in points]) 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_min = min([y for _, y in points])
y_max = max([y for _, y in points]) y_max = max([y for _, y in points])


_points = [] _points = []
for x, y in points: for i, point in enumerate(points):
x_mul = 1 - (x_max - x) / (x_max - x_min) 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) y_mul = 1 - (y_max - y) / (y_max - y_min)
_points.append((x_mul * self.width, y_mul * self.height)) _points.append((x_mul * self.width, y_mul * self.height))
return _points return _points
Expand Down

0 comments on commit dab4471

Please sign in to comment.