Skip to content

Commit

Permalink
Specify style_function. Closes #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 20, 2016
1 parent b844d95 commit 16c8b91
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 21 deletions.
2 changes: 2 additions & 0 deletions docs/api/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ Series
:no-members:

.. autoclass:: leather.Series

.. autofunction:: leather.key_function
2 changes: 2 additions & 0 deletions docs/api/shapes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Shapes
.. autoclass:: leather.Dots

.. autoclass:: leather.Lines

.. autofunction:: leather.style_function
13 changes: 11 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Examples
========

Data
====
Data series
===========

Simple pairs
------------
Expand Down Expand Up @@ -42,6 +42,13 @@ Custom data

TKTK

Multiple series
---------------

Multiple data series can be displayed on a single chart.

TKTK

Shapes
======

Expand Down Expand Up @@ -115,6 +122,8 @@ TKTK
Styling data based on value
---------------------------

Style attributes of individual data points can be set by value using a :func:`.style_function`.

.. literalinclude:: ../examples/colorized_dots.py
:language: python

Expand Down
2 changes: 1 addition & 1 deletion examples/colorized_dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

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

chart = leather.Chart('Colorized dots')
Expand Down
4 changes: 2 additions & 2 deletions leather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
from leather.lattice import Lattice
from leather.legend import Legend
from leather.scales import Scale, Linear, Ordinal, Temporal
from leather.series import Series
from leather.shapes import Shape, Bars, Columns, Dots, Lines
from leather.series import Series, key_function
from leather.shapes import Shape, Bars, Columns, Dots, Lines, style_function
from leather import theme
2 changes: 1 addition & 1 deletion leather/shapes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from leather.shapes.base import Shape
from leather.shapes.base import Shape, style_function
from leather.shapes.bars import Bars
from leather.shapes.columns import Columns
from leather.shapes.dots import Dots
Expand Down
7 changes: 3 additions & 4 deletions leather/shapes/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class Bars(Shape):
Render a series of data as bars.
:param color:
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.
The color to fill the bars. You may also specify a
:func:`.style_function`.
"""
def __init__(self, color):
self._color = color
Expand All @@ -34,7 +33,7 @@ def to_svg(self, width, height, x_scale, y_scale, series):
y1, y2 = y_scale.project_interval(y, height, 0)

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

Expand Down
20 changes: 20 additions & 0 deletions leather/shapes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,23 @@ class Shape(object):
Base class for shapes that can be used to render data :class:`.Series`.
"""
pass


def style_function(x, y, row, index):
"""
This example shows how to define a function to specify style values for
individual data points.
:param x:
The function will be called with the X and Y values from the data.
:param y:
See :code:`x`.
:param row:
The entire data row will also be passed in, allowing styling based on
non-coordinate attributes.
:param index:
The row index in the series data will also be provided.
:returns:
An appropriate value for the attribute being styled.
"""
pass
7 changes: 3 additions & 4 deletions leather/shapes/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class Columns(Shape):
Render a series of data as columns.
:param color:
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.
The color to fill the columns. You may also specify a
:func:`.style_function`.
"""
def __init__(self, color):
self._color = color
Expand All @@ -34,7 +33,7 @@ def to_svg(self, width, height, x_scale, y_scale, series):
proj_y = y_scale.project(y, height, 0)

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

Expand Down
12 changes: 5 additions & 7 deletions leather/shapes/dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ class Dots(Shape):
Render a series of data as 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.
The color to fill the dots. You may also specify a
:func:`.style_function`.
:param radius:
The radius of the rendered dots. Defaults to
:data:`.theme.default_dot_radius`. You may also specify a function,
which will be called with the arguments :code:`(x, y, index)` and
should return a radius.
:data:`.theme.default_dot_radius`. You may also specify a
:func:`.style_function`.
"""
def __init__(self, fill_color, radius=None):
self._fill_color = fill_color
Expand All @@ -41,7 +39,7 @@ def to_svg(self, width, height, x_scale, y_scale, series):
proj_y = y_scale.project(y, height, 0)

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

Expand Down

0 comments on commit 16c8b91

Please sign in to comment.