forked from wireservice/leather
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding GroupedColumns. wireservice#26
- Loading branch information
Showing
9 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,16 @@ | ||
import leather | ||
from datetime import date | ||
|
||
data = [ | ||
('Hello', 1, 'first'), | ||
('World', 5, 'first'), | ||
('Hello', 7, 'second'), | ||
('Goodbye', 4, 'second'), | ||
('Hello', 7, 'third'), | ||
('Goodbye', 3, 'third'), | ||
('Yellow', 4, 'third') | ||
] | ||
|
||
chart = leather.Chart('Columns') | ||
chart.add_grouped_columns(data) | ||
chart.to_svg('examples/charts/grouped_columns.svg') |
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
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,87 @@ | ||
#!/usr/bin/env python | ||
|
||
import xml.etree.ElementTree as ET | ||
|
||
import six | ||
|
||
from leather.series import CategorySeries | ||
from leather.shapes.category import CategoryShape | ||
from leather.utils import X, Z | ||
from leather import theme | ||
|
||
|
||
class GroupedColumns(CategoryShape): | ||
""" | ||
Render a categorized series of data as grouped columns. | ||
:param fill_color: | ||
A sequence of colors to fill the columns. The sequence must have length | ||
greater than or equal to the number of unique values in all categories. | ||
You may also specify a :func:`.style_function`. | ||
""" | ||
def __init__(self, fill_color=None): | ||
self._fill_color = fill_color | ||
self._legend_dimension = X | ||
|
||
def validate_series(self, series): | ||
""" | ||
Verify this shape can be used to render a given series. | ||
""" | ||
if not isinstance(series, CategorySeries): | ||
raise ValueError('GroupedColumns can only be used to render CategorySeries.') | ||
|
||
def to_svg(self, width, height, x_scale, y_scale, series, palette): | ||
""" | ||
Render columns to SVG elements. | ||
""" | ||
group = ET.Element('g') | ||
group.set('class', 'series grouped-columns') | ||
|
||
zero_y = y_scale.project(0, height, 0) | ||
|
||
if self._fill_color: | ||
fill_color = self._fill_color | ||
else: | ||
fill_color = list(palette) | ||
|
||
label_colors = self.legend_labels(series, fill_color) | ||
|
||
categories = series.categories() | ||
category_counts = {c: series.values(Z).count(c) for c in categories} | ||
seen_counts = {c: 0 for c in categories} | ||
|
||
for d in series.data(): | ||
if d.x is None or d.y is None: | ||
continue | ||
|
||
x1, x2 = x_scale.project_interval(d.z, 0, width) | ||
|
||
group_width = (x1 - x2) / category_counts[d.z] | ||
x1 = x2 + (group_width * (seen_counts[d.z] + 1)) + 1 | ||
x2 = x2 + (group_width * seen_counts[d.z]) | ||
|
||
proj_y = y_scale.project(d.y, height, 0) | ||
|
||
if d.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(fill_color): | ||
color = fill_color(d) | ||
else: | ||
color = dict(label_colors)[d.x] | ||
|
||
seen_counts[d.z] += 1 | ||
|
||
group.append(ET.Element('rect', | ||
x=six.text_type(x1), | ||
y=six.text_type(column_y), | ||
width=six.text_type(x2 - x1), | ||
height=six.text_type(column_height), | ||
fill=color | ||
)) | ||
|
||
return group |
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