diff --git a/leather/shapes/grouped_bars.py b/leather/shapes/grouped_bars.py index 0a32716..40ea6b8 100644 --- a/leather/shapes/grouped_bars.py +++ b/leather/shapes/grouped_bars.py @@ -15,8 +15,8 @@ class GroupedBars(CategoryShape): Render a categorized series of data as grouped bars. :param fill_color: - A sequence of colors to fill the bars. If the sequence is shorter than - the number of values in any category, the colors will be repeated. + A sequence of colors to fill the bars. The sequence must have length + greater than or equal to the number of values in any category. """ def __init__(self, fill_color=None): self._fill_color = fill_color @@ -26,8 +26,11 @@ def validate_series(self, series): """ Verify this shape can be used to render a given series. """ - if issequence(self._fill_color) and len(series.categories()) > len(self._fill_color.keys()): - raise ValueError('fill_color must have an element for every category in the series.') + if len(series.categories()) > len(self._fill_color): + raise ValueError('Fill color must have an element for every category in the series.') + + if isinstance(series, CategorySeries): + raise ValueError('GroupedBars can only be used to render CategorySeries.') def to_svg(self, width, height, x_scale, y_scale, series, palette): """ @@ -43,6 +46,9 @@ def to_svg(self, width, height, x_scale, y_scale, series, palette): else: fill_color = list(palette) + if len(series.categories()) > len(fill_color): + raise ValueError('Fill color must have an element for every category in the series.') + label_colors = self.legend_labels(series, fill_color) categories = series.categories() diff --git a/tests/test_shapes.py b/tests/test_shapes.py index 2c83937..e1d25eb 100644 --- a/tests/test_shapes.py +++ b/tests/test_shapes.py @@ -195,6 +195,12 @@ def test_to_svg(self): self.assertEqual(float(rects[1].get('width')), 100) self.assertEqual(rects[1].get('fill'), 'white') + def test_invalid_fill_color(self): + series = leather.CategorySeries(self.rows) + + with self.assertRaises(ValueError): + group = self.shape.to_svg(200, 100, self.linear, self.ordinal, series, ['one', 'two']) + def test_nulls(self): series = leather.CategorySeries([ (0, 'foo', 'first'),