Skip to content

Commit

Permalink
Support for negative bars and columns. Closes #52.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 20, 2016
1 parent a8f027f commit f6bf87f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
15 changes: 12 additions & 3 deletions leather/shapes/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ def to_svg(self, width, height, x_scale, y_scale, series):
group = ET.Element('g')
group.set('class', 'series bars')

zero_x = x_scale.project(0, 0, width)

for i, (x, y, row) in enumerate(series):
if x is None or y is None:
continue

proj_x = x_scale.project(x, 0, width)
y1, y2 = y_scale.project_interval(y, height, 0)
proj_x = x_scale.project(x, 0, width)

if x < 0:
bar_x = proj_x
bar_width = zero_x - proj_x
else:
bar_x = zero_x
bar_width = proj_x - zero_x

if callable(self._fill_color):
color = self._fill_color(x, y, row, i)
else:
color = self._fill_color

group.append(ET.Element('rect',
x=six.text_type(0),
x=six.text_type(bar_x),
y=six.text_type(y2),
width=six.text_type(proj_x),
width=six.text_type(bar_width),
height=six.text_type(y1 - y2),
fill=color
))
Expand Down
13 changes: 11 additions & 2 deletions leather/shapes/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,32 @@ def to_svg(self, width, height, x_scale, y_scale, series):
group = ET.Element('g')
group.set('class', 'series columns')

zero_y = y_scale.project(0, height, 0)

for i, (x, y, row) in enumerate(series):
if x is None or y is None:
continue

x1, x2 = x_scale.project_interval(x, 0, width)
proj_y = y_scale.project(y, height, 0)

if y < 0:
column_y = zero_y
column_height = proj_y - zero_y
else:
column_y = proj_y
column_height = zero_y - proj_y

if callable(self._fill_color):
color = self._fill_color(x, y, row, i)
else:
color = self._fill_color

group.append(ET.Element('rect',
x=six.text_type(x1),
y=six.text_type(proj_y),
y=six.text_type(column_y),
width=six.text_type(x2 - x1),
height=six.text_type(height - proj_y),
height=six.text_type(column_height),
fill=color
))

Expand Down
27 changes: 6 additions & 21 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import leather

data1 = [
(0, 3),
(4, 5),
(7, 9),
(8, 4)
data = [
(-3, 'foo', -3),
(5, 'bar', 5),
(-9, 'baz', -9)
]

data2 = [
(2, 4),
(7, 3),
(6, 2),
(5, 9)
]

data3 = [
(2, 7),
(7, 2)
]

chart = leather.Chart('Multiple series')
chart.add_dots(data1, name="This is a really insanely long series name......")
chart.add_dots(data2, name="And this one is almost as long!")
chart.add_dots(data3, name="But this one's short.")
chart = leather.Chart('Negative columns')
chart.add_bars(data)
chart.to_svg('test.svg')

0 comments on commit f6bf87f

Please sign in to comment.