-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement experimental Annual scale. #35.
- Loading branch information
Showing
10 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ Scales | |
.. autoclass:: leather.Ordinal | ||
|
||
.. autoclass:: leather.Temporal | ||
|
||
.. autoclass:: leather.Annual |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
from datetime import datetime, date | ||
|
||
from leather.scales.temporal import Temporal | ||
|
||
|
||
class Annual(Temporal): | ||
""" | ||
A scale that maps years to a pixel range. | ||
This scale takes linear values (dates, datetimes, or numbers), but treats | ||
them as ordinal values for purposes of projection. Thus you can use this | ||
scale to render :class:`.Bars` or :class:`.Columns` for yearly data. | ||
:param domain_min: | ||
The minimum value of the domain. | ||
:param domain_max: | ||
The maximum value of the domain. | ||
""" | ||
def __init__(self, domain_min, domain_max): | ||
self._min = self._value_as_date(domain_min) | ||
self._max = self._value_as_date(domain_max) | ||
|
||
def _value_as_date(self, value): | ||
""" | ||
Convert a date or number to a date for consistent logic. | ||
""" | ||
if isinstance(value, (datetime, date)): | ||
return value | ||
elif isinstance(value, (int, float)): | ||
return date(value, 1, 1) | ||
|
||
raise ValueError('Unsupported domain value for Annual scale.') | ||
|
||
def project(self, value, range_min, range_max): | ||
""" | ||
Project a value in this scale's domain to a target range. | ||
""" | ||
d = self._value_as_date(value) | ||
|
||
segments = self._max.year - self._min.year + 1 | ||
segment_size = (range_max - range_min) / segments | ||
|
||
pos = d.year - self._min.year | ||
|
||
return range_min + ((pos + 0.5) * segment_size) | ||
|
||
def project_interval(self, value, range_min, range_max): | ||
""" | ||
Project a value in this scale's domain to an interval in the target | ||
range. This is used for places :class:`.Bars` and :class:`.Columns`. | ||
""" | ||
d = self._value_as_date(value) | ||
|
||
segments = self._max.year - self._min.year + 1 | ||
segment_size = (range_max - range_min) / segments | ||
gap = segment_size * 0.05 | ||
|
||
pos = d.year - self._min.year | ||
|
||
a = range_min + ((pos) * segment_size) + gap | ||
b = range_min + ((pos + 1) * segment_size) - gap | ||
|
||
return (a, b) | ||
|
||
def ticks(self, count): | ||
""" | ||
Generate a series of ticks for this scale. | ||
""" | ||
return [date(year, 1, 1) for year in range(self._min.year, self._max.year + 1)] | ||
|
||
def format_tick(self, value, i, count): | ||
""" | ||
Display only year component. | ||
""" | ||
return value.year |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
#!/usr/bin/env python | ||
|
||
from datetime import date, datetime, time | ||
|
||
from leather.scales.base import Scale | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import leather | ||
|
||
import datetime | ||
|
||
data = [ | ||
(-1, -1), | ||
(0, 0), | ||
(1, 1) | ||
(datetime.date(2010, 1, 1), -1), | ||
(datetime.date(2011, 1, 1), -1), | ||
(datetime.date(2012, 1, 1), 0), | ||
(datetime.date(2013, 1, 1), 1) | ||
] | ||
|
||
chart = leather.Chart() | ||
chart.add_columns(data) | ||
chart.add_lines(data) | ||
chart.add_dots(data) | ||
chart.set_x_scale(leather.Annual(datetime.date(2010, 1, 1), datetime.date(2014, 1, 1))) | ||
chart.to_svg('test.svg') |