Browse Source

Negative in stacked bars

pull/8/head
Florian Mounier 13 years ago
parent
commit
6d09cdc346
  1. 4
      out.py
  2. 9
      pygal/stackedbar.py
  3. 4
      pygal/svg.py

4
out.py

@ -12,9 +12,9 @@ with open('out-bar.svg', 'w') as f:
f.write(bar.render()) f.write(bar.render())
stackedbar = StackedBar() stackedbar = StackedBar()
rng = [3, 32, 39, 12] rng = [3, -32, 39, 12]
stackedbar.add('test1', rng) stackedbar.add('test1', rng)
rng2 = [24, 8, 18, 12] rng2 = [24, -8, 18, 12]
stackedbar.add('test2', rng2) stackedbar.add('test2', rng2)
rng3 = [6, 1, -10, 0] rng3 = [6, 1, -10, 0]
stackedbar.add('test3', rng3) stackedbar.add('test3', rng3)

9
pygal/stackedbar.py

@ -6,8 +6,11 @@ class StackedBar(BaseGraph):
def _draw(self): def _draw(self):
transposed = zip(*[serie.values for serie in self.series]) transposed = zip(*[serie.values for serie in self.series])
vals = [sum(val) for val in transposed] positive_vals = [sum([val if val > 0 else 0 for val in vals])
ymin, ymax = min(min(vals), 0), max(max(vals), 0) 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) length = len(self.series[0].values)
x_pos = [x / float(length) for x in range(length + 1) x_pos = [x / float(length) for x in range(length + 1)
] if length > 1 else [0, 1] # Center if only one value ] 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.legend([serie.title for serie in self.series])
self.svg.title() self.svg.title()
stack_vals = [0] * length stack_vals = [[0, 0] for i in range(length)]
for serie in self.series: for serie in self.series:
serie_node = self.svg.serie(serie.index) serie_node = self.svg.serie(serie.index)
stack_vals = self.svg.stackbar( stack_vals = self.svg.stackbar(

4
pygal/svg.py

@ -205,8 +205,8 @@ class Svg(object):
height = self.view.y(0) - y height = self.view.y(0) - y
x = x + padding x = x + padding
y_txt = y + height / 2 y_txt = y + height / 2
shift = stack_vals[i] shift = stack_vals[i][int(height < 0)]
stack_vals[i] += height stack_vals[i][int(height < 0)] += height
if height < 0: if height < 0:
y = y + height y = y + height
height = -height height = -height

Loading…
Cancel
Save