Browse Source

Fix empty bar graphs

pull/8/head
Florian Mounier 13 years ago
parent
commit
1aa5726e46
  1. 13
      pygal/bar.py
  2. 6
      pygal/css/graph.css
  3. 15
      pygal/graph.py
  4. 5
      pygal/util/boundary.py

13
pygal/bar.py

@ -58,15 +58,16 @@ class Bar(Graph):
return map(str, self.get_data_values())
def data_max(self):
return max(chain(*map(lambda set: set['data'], self.data)))
return max(
list(chain(*map(lambda set: set['data'], self.data))) + [0])
# above is same as
# return max(map(lambda set: max(set['data']), self.data))
def data_min(self):
if not getattr(self, 'min_scale_value') is None:
return self.min_scale_value
min_value = min(chain(*map(lambda set: set['data'], self.data)))
min_value = min(min_value, 0)
min_value = min(list(
chain(*map(lambda set: set['data'], self.data))) + [0])
return min_value
def get_bar_gap(self, field_size):
@ -103,8 +104,12 @@ class VerticalBar(Bar):
min_value = self.data_min()
unit_size = (
float(self.graph_height) - self.font_size * 2 * self.top_font)
unit_size /= (
divisor = (
max(self.get_data_values()) - min(self.get_data_values()))
if divisor == 0:
unit_size = 0
else:
unit_size /= divisor
bar_gap = self.get_bar_gap(self.get_field_width())

6
pygal/css/graph.css

@ -4,7 +4,7 @@
font-family: helvetica;
}
.svgBackground{
fill: transparent;
fill: #fff;
}
.graphBackground{
fill: #e8eef6;
@ -153,11 +153,11 @@
}
.light2 {
stop-color: #38588e;
stop-color: #476fb2;
}
.light3 {
stop-color: #476fb2;
stop-color: #38588e;
}
.light4 {

15
pygal/graph.py

@ -292,14 +292,19 @@ class Graph(object):
return 0
def get_field_width(self):
return (float(
self.graph_width - self.font_size * 2 * self.right_font) /
(len(self.get_x_labels()) - self.right_align))
divisor = (len(self.get_x_labels()) - self.right_align)
if divisor == 0:
return 0
return float(
self.graph_width - self.font_size * 2 * self.right_font) / divisor
field_width = get_field_width
def get_field_height(self):
return (float(self.graph_height - self.font_size * 2 * self.top_font) /
(len(self.get_y_labels()) - self.top_align))
divisor = (len(self.get_y_labels()) - self.top_align)
if divisor == 0:
return 0
return float(
self.graph_height - self.font_size * 2 * self.top_font) / divisor
field_height = get_field_height
def draw_y_labels(self):

5
pygal/util/boundary.py

@ -52,7 +52,7 @@ def calculate_bottom_margin(graph):
max_x_label_height_px = graph.x_label_font_size
if graph.x_label_rotation:
label_lengths = map(len, graph.get_x_labels())
max_x_label_len = reduce(max, label_lengths)
max_x_label_len = reduce(max, label_lengths, 0)
max_x_label_height_px *= max_x_label_len * 0.6
max_x_label_height_px *= sin(graph.x_label_rotation)
bb += max_x_label_height_px + graph.y_label_font_size
@ -85,7 +85,8 @@ def calculate_left_margin(graph):
bl += graph.y_title_font_size + 5
if graph.x_label_rotation:
first_x_label_width = (
graph.x_label_font_size * len(graph.get_x_labels()[0]) * 0.6)
graph.x_label_font_size * len(
(graph.get_x_labels() or [[0]])[0]) * 0.6)
bl = max(bl, first_x_label_width * cos(graph.x_label_rotation))
return bl

Loading…
Cancel
Save