From 8d6a183fbda1dd95d23ae9699ff872986881049a Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Thu, 2 Feb 2012 18:07:08 +0100 Subject: [PATCH] And the title --- pygal/css/graph.css | 6 ++++++ pygal/line.py | 9 ++++++--- pygal/svg.py | 5 +++++ pygal/test/test_line.py | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pygal/css/graph.css b/pygal/css/graph.css index 74d6c09..bfb1cef 100644 --- a/pygal/css/graph.css +++ b/pygal/css/graph.css @@ -10,6 +10,12 @@ fill: #777; } +.title { + fill: #eee; + text-anchor: middle; + alignment-baseline: baseline; +} + .legend text { font-size: 12px; font-family: sans; diff --git a/pygal/line.py b/pygal/line.py index 6c8de18..03deeae 100644 --- a/pygal/line.py +++ b/pygal/line.py @@ -12,7 +12,7 @@ class Line(BaseGraph): self.svg = Svg(width, height) self.label_font_size = 12 self.series = [] - self.x_labels = None + self.x_labels = self.title = None def add(self, title, values): self.series.append( @@ -43,7 +43,7 @@ class Line(BaseGraph): self.validate() vals = [val for serie in self.series for val in serie.values] - margin = Margin(*(4 * [20])) + margin = Margin(*(4 * [10])) ymin, ymax = min(vals), max(vals) x_labels = self.x_labels y_labels = self.y_labels(ymin, ymax) @@ -51,7 +51,9 @@ class Line(BaseGraph): margin.left += 10 + max( map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size margin.bottom += 10 + self.label_font_size - margin.right += 40 + max(map(len, series_labels)) + margin.right += 20 + max( + map(len, series_labels)) * 0.6 * self.label_font_size + margin.top += 10 + self.label_font_size # Actual drawing self.svg.set_view(margin, ymin, ymax) @@ -59,6 +61,7 @@ class Line(BaseGraph): self.svg.x_axis(x_labels) self.svg.y_axis(y_labels) self.svg.legend(margin, series_labels) + self.svg.title(margin, self.title) for serie_index, serie in enumerate(self.series): serie_node = self.svg.serie(serie_index) self.svg.line(serie_node, [ diff --git a/pygal/svg.py b/pygal/svg.py index e49ace4..639ec90 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -112,6 +112,11 @@ class Svg(object): ).text = title self.node(legend, 'text', x=15, y=i * 15).text = title + def title(self, margin, title): + self.node(self.graph, 'text', class_='title', + x=margin.left + self.view.width / 2, + y=10).text = title + def serie(self, serie): return self.node( self.plot, class_='series serie-%d color-%d' % (serie, serie)) diff --git a/pygal/test/test_line.py b/pygal/test/test_line.py index e9bd624..9fe49fe 100644 --- a/pygal/test/test_line.py +++ b/pygal/test/test_line.py @@ -8,4 +8,5 @@ def test_simple_line(): line.add('test2', [sin(x / 10.) for x in range(-30, 30, 5)]) line.add('test3', [cos(x / 10.) - sin(x / 10.) for x in range(-30, 30, 5)]) line.set_labels(map(str, range(-30, 30, 5))) + line.title = "cos sin and cos - sin" line._in_browser()