diff --git a/out.py b/out.py index c71bc75..d756112 100755 --- a/out.py +++ b/out.py @@ -12,9 +12,9 @@ with open('out-bar.svg', 'w') as f: f.write(bar.render()) stackedbar = StackedBar() -rng = [3, 32, 39, 12] +rng = [3, -32, 39, 12] stackedbar.add('test1', rng) -rng2 = [24, 8, 18, 12] +rng2 = [24, -8, 18, 12] stackedbar.add('test2', rng2) rng3 = [6, 1, -10, 0] stackedbar.add('test3', rng3) diff --git a/pygal/stackedbar.py b/pygal/stackedbar.py index bcfdba8..48452ae 100644 --- a/pygal/stackedbar.py +++ b/pygal/stackedbar.py @@ -6,8 +6,11 @@ class StackedBar(BaseGraph): def _draw(self): transposed = zip(*[serie.values for serie in self.series]) - vals = [sum(val) for val in transposed] - ymin, ymax = min(min(vals), 0), max(max(vals), 0) + positive_vals = [sum([val if val > 0 else 0 for val in vals]) + for vals in transposed] + negative_vals = [sum([val if val < 0 else 0 for val in vals]) + for vals in transposed] + ymin, ymax = min(min(negative_vals), 0), max(max(positive_vals), 0) length = len(self.series[0].values) x_pos = [x / float(length) for x in range(length + 1) ] if length > 1 else [0, 1] # Center if only one value @@ -28,7 +31,7 @@ class StackedBar(BaseGraph): self.svg.legend([serie.title for serie in self.series]) self.svg.title() - stack_vals = [0] * length + stack_vals = [[0, 0] for i in range(length)] for serie in self.series: serie_node = self.svg.serie(serie.index) stack_vals = self.svg.stackbar( diff --git a/pygal/svg.py b/pygal/svg.py index d765f59..bc6034a 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -205,8 +205,8 @@ class Svg(object): height = self.view.y(0) - y x = x + padding y_txt = y + height / 2 - shift = stack_vals[i] - stack_vals[i] += height + shift = stack_vals[i][int(height < 0)] + stack_vals[i][int(height < 0)] += height if height < 0: y = y + height height = -height