Skip to content

Commit

Permalink
Support colorized shapes.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 20, 2016
1 parent f0f699a commit 16fc333
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 50 deletions.
4 changes: 4 additions & 0 deletions docs/example2.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ Show me code
.. figure:: example.svg

.. code-block:: python
dot_data = [(random.randint(0, 250), random.randint(0, 250)) for i in range(100)]
def colorizer(x, y, i):
return 'rgb(%i, %i, %i)' % (x, y, 150)
chart = leather.Chart('Something a little fancier')
chart.add_dots(dot_data, color=colorizer)
chart.to_svg('docs/example2.svg')
.. figure:: example2.svg

Join us
=======

Expand Down
14 changes: 14 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin env python

import random

import leather

# Example 1

data = [
(0, 3),
Expand All @@ -14,3 +17,14 @@
chart.add_lines(data)
chart.add_dots(data)
chart.to_svg('docs/example.svg')

# Example 2

dot_data = [(random.randint(0, 250), random.randint(0, 250)) for i in range(100)]

def colorizer(x, y, i):
return 'rgb(%i, %i, %i)' % (x, y, 150)

chart = leather.Chart('Well that was easy')
chart.add_dots(dot_data, color=colorizer)
chart.to_svg('docs/example2.svg')
15 changes: 11 additions & 4 deletions leather/shapes/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class Bars(Shape):
Render a series of data as bars.
:param color:
The color to fill the bars.
The color to fill the bars. You may also specify a function, which will
be called with the arguments :code:`(x, y, index)` and should return a
color.
"""
def __init__(self, color):
self.color = color
self._color = color

def to_svg(self, width, height, x_scale, y_scale, series):
"""
Expand All @@ -24,19 +26,24 @@ def to_svg(self, width, height, x_scale, y_scale, series):
group = ET.Element('g')
group.set('class', 'series bars')

for x, y in series.data:
for i, (x, y) in enumerate(series.data):
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)

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

group.append(ET.Element('rect',
x=six.text_type(0),
y=six.text_type(y2),
width=six.text_type(proj_x),
height=six.text_type(y1 - y2),
fill=self.color
fill=color
))

return group
15 changes: 11 additions & 4 deletions leather/shapes/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class Columns(Shape):
Render a series of data as columns.
:param color:
The color to fill the columns.
The color to fill the columns. You may also specify a function, which
will be called with the arguments :code:`(x, y, index)` and should
return a color.
"""
def __init__(self, color):
self.color = color
self._color = color

def to_svg(self, width, height, x_scale, y_scale, series):
"""
Expand All @@ -24,19 +26,24 @@ def to_svg(self, width, height, x_scale, y_scale, series):
group = ET.Element('g')
group.set('class', 'series columns')

for x, y in series.data:
for i, (x, y) in enumerate(series.data):
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 callable(self._color):
color = self._color(x, y, i)
else:
color = self._color

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

return group
23 changes: 15 additions & 8 deletions leather/shapes/dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ class Dots(Shape):
"""
Render a series of data as dots.
:param color:
The color to fill the dots.
:param fill_color:
The color to fill the dots. You may also specify a function, which will
be called with the arguments :code:`(x, y, index)` and should return a
color.
:param radius:
The radius of the rendered dots. Defaults to
:data:`.theme.default_dot_radius`.
"""
def __init__(self, color, radius=None):
self.color = color
self.radius = radius or theme.default_dot_radius
def __init__(self, fill_color, radius=None):
self._fill_color = fill_color
self._radius = radius or theme.default_dot_radius

def to_svg(self, width, height, x_scale, y_scale, series):
"""
Expand All @@ -29,18 +31,23 @@ def to_svg(self, width, height, x_scale, y_scale, series):
group = ET.Element('g')
group.set('class', 'series dots')

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

proj_x = x_scale.project(x, 0, width)
proj_y = y_scale.project(y, height, 0)

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

group.append(ET.Element('circle',
cx=six.text_type(proj_x),
cy=six.text_type(proj_y),
r=six.text_type(self.radius),
fill=self.color
r=six.text_type(self._radius),
fill=fill_color
))

return group
2 changes: 1 addition & 1 deletion leather/shapes/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Lines(Shape):
Render a series of data as a lines.
:param color:
The color to use for the line stroke.
The color to stroke the lines.
:param width:
The width of the lines. Defaults to :data:`.theme.default_line_width`.
"""
Expand Down
69 changes: 36 additions & 33 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
#!/usr/bin/env python

import leather
import random

dot_data = [
(0, 3),
(4, 5),
(7, 9),
(8, 4)
]
import leather

line_data = [
(0, 4),
(1, 3),
(2, 5),
(5, 6),
(9, 10)
]
dot_data = [(random.randint(0, 250), random.randint(0, 250)) for i in range(100)]

leather.theme.default_chart_width = 100
def colorizer(x, y, i):
return 'rgb(%i, %i, %i)' % (x, y, 150)

chart = leather.Chart()
# chart.set_x_scale(leather.Linear(0, 20))
chart.add_dots(dot_data)
chart.add_lines(line_data)
chart = leather.Chart('Well that was easy')
chart.add_dots(dot_data, color=colorizer)
chart.to_svg('test.svg')

leather.theme.default_chart_width = 1000

chart.to_svg('test2.svg')

# dot_data = [
# ('foo', 3),
# ('bing', 5),
# ('baz', 9),
# ('blurg', 4)
# (0, 3),
# (4, 5),
# (7, 9),
# (8, 4)
# ]
#
# line_data = [
# ('foo', 7),
# ('bing', 2),
# ('baz', 3),
# ('blurg', 4)
# (0, 4),
# (1, 3),
# (2, 5),
# (5, 6),
# (9, 10)
# ]
#
# chart = leather.Chart()
# chart.add_column(line_data)
# chart.add_dot(dot_data)
# # chart.set_x_scale(leather.Linear(0, 20))
# chart.add_dots(dot_data)
# chart.add_lines(line_data)
# chart.to_svg('test.svg')

# bar_data = [
# (3, 'foo'),
# (5, 'bing blaarg murg'),
# (9, 'baz'),
# (4, 'blurg')
# ]
#
# def colorizer(x, y, i):
# if y == 'baz':
# return 'yellow'
# else:
# return 'blue'
#
# chart = leather.Chart('Bar charts are fun')
# chart.add_dots(bar_data, color=colorizer)
# chart.to_svg('test.svg')
#
# data = [[
Expand Down

0 comments on commit 16fc333

Please sign in to comment.