Browse Source

And the title

pull/8/head
Florian Mounier 13 years ago
parent
commit
8d6a183fbd
  1. 6
      pygal/css/graph.css
  2. 9
      pygal/line.py
  3. 5
      pygal/svg.py
  4. 1
      pygal/test/test_line.py

6
pygal/css/graph.css

@ -10,6 +10,12 @@
fill: #777; fill: #777;
} }
.title {
fill: #eee;
text-anchor: middle;
alignment-baseline: baseline;
}
.legend text { .legend text {
font-size: 12px; font-size: 12px;
font-family: sans; font-family: sans;

9
pygal/line.py

@ -12,7 +12,7 @@ class Line(BaseGraph):
self.svg = Svg(width, height) self.svg = Svg(width, height)
self.label_font_size = 12 self.label_font_size = 12
self.series = [] self.series = []
self.x_labels = None self.x_labels = self.title = None
def add(self, title, values): def add(self, title, values):
self.series.append( self.series.append(
@ -43,7 +43,7 @@ class Line(BaseGraph):
self.validate() self.validate()
vals = [val for serie in self.series for val in serie.values] 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) ymin, ymax = min(vals), max(vals)
x_labels = self.x_labels x_labels = self.x_labels
y_labels = self.y_labels(ymin, ymax) y_labels = self.y_labels(ymin, ymax)
@ -51,7 +51,9 @@ class Line(BaseGraph):
margin.left += 10 + max( margin.left += 10 + max(
map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size
margin.bottom += 10 + 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 # Actual drawing
self.svg.set_view(margin, ymin, ymax) self.svg.set_view(margin, ymin, ymax)
@ -59,6 +61,7 @@ class Line(BaseGraph):
self.svg.x_axis(x_labels) self.svg.x_axis(x_labels)
self.svg.y_axis(y_labels) self.svg.y_axis(y_labels)
self.svg.legend(margin, series_labels) self.svg.legend(margin, series_labels)
self.svg.title(margin, self.title)
for serie_index, serie in enumerate(self.series): for serie_index, serie in enumerate(self.series):
serie_node = self.svg.serie(serie_index) serie_node = self.svg.serie(serie_index)
self.svg.line(serie_node, [ self.svg.line(serie_node, [

5
pygal/svg.py

@ -112,6 +112,11 @@ class Svg(object):
).text = title ).text = title
self.node(legend, 'text', x=15, y=i * 15).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): def serie(self, serie):
return self.node( return self.node(
self.plot, class_='series serie-%d color-%d' % (serie, serie)) self.plot, class_='series serie-%d color-%d' % (serie, serie))

1
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('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.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.set_labels(map(str, range(-30, 30, 5)))
line.title = "cos sin and cos - sin"
line._in_browser() line._in_browser()

Loading…
Cancel
Save