Browse Source

Label rotation

pull/8/head
Florian Mounier 14 years ago
parent
commit
65c45f7670
  1. 1
      pygal/bar.css
  2. 1
      pygal/graph.css
  3. 31
      pygal/graph.py

1
pygal/bar.css

@ -2,6 +2,7 @@
fill-opacity: 0; fill-opacity: 0;
-webkit-transition: 250ms; -webkit-transition: 250ms;
} }
.bar:hover .dataPointLabel { .bar:hover .dataPointLabel {
fill-opacity: 0.9; fill-opacity: 0.9;
fill: #000000; fill: #000000;

1
pygal/graph.css

@ -44,7 +44,6 @@ Base styles for pygal.Graph
font-size: %(x_label_font_size)dpx; font-size: %(x_label_font_size)dpx;
font-family: "Arial", sans-serif; font-family: "Arial", sans-serif;
font-weight: normal; font-weight: normal;
transform: rotate(45deg);
} }
.yAxisLabels{ .yAxisLabels{

31
pygal/graph.py

@ -11,7 +11,7 @@ from operator import itemgetter
from itertools import islice from itertools import islice
import pkg_resources import pkg_resources
import functools import functools
import math
import cssutils import cssutils
from lxml import etree from lxml import etree
@ -24,6 +24,14 @@ except ImportError:
zlib = None zlib = None
def cos(angle):
return math.cos(angle * math.pi / 180)
def sin(angle):
return math.sin(angle * math.pi / 180)
def sort_multiple(arrays): def sort_multiple(arrays):
"sort multiple lists (of equal size) " "sort multiple lists (of equal size) "
"using the first list for the sort keys" "using the first list for the sort keys"
@ -59,7 +67,7 @@ class Graph(object):
min_scale_value = None min_scale_value = None
show_x_labels = True show_x_labels = True
stagger_x_labels = False stagger_x_labels = False
rotate_x_labels = False x_label_rotation = 0
step_x_labels = 1 step_x_labels = 1
step_include_first_x_label = True step_include_first_x_label = True
show_y_labels = True show_y_labels = True
@ -287,7 +295,7 @@ class Graph(object):
bb += 10 bb += 10
if self.show_x_labels: if self.show_x_labels:
max_x_label_height_px = self.x_label_font_size max_x_label_height_px = self.x_label_font_size
if self.rotate_x_labels: if self.x_label_rotation:
label_lengths = map(len, self.get_x_labels()) label_lengths = map(len, self.get_x_labels())
max_x_label_len = reduce(max, label_lengths) max_x_label_len = reduce(max, label_lengths)
max_x_label_height_px *= 0.6 * max_x_label_len max_x_label_height_px *= 0.6 * max_x_label_len
@ -386,11 +394,12 @@ class Graph(object):
text.set('x', str(x)) text.set('x', str(x))
text.set('y', str(y)) text.set('y', str(y))
if self.rotate_x_labels: if self.x_label_rotation:
transform = 'rotate(90 %d %d) translate(0 -%d)' % \ transform = 'rotate(%d %d %d) translate(0 -%d)' % \
(x, y - self.x_label_font_size, self.x_label_font_size / 4) (-self.x_label_rotation, x, y - self.x_label_font_size,
self.x_label_font_size / 4)
text.set('transform', transform) text.set('transform', transform)
text.set('style', 'text-anchor: start') text.set('style', 'text-anchor: end')
else: else:
text.set('style', 'text-anchor: middle') text.set('style', 'text-anchor: middle')
@ -585,10 +594,10 @@ class Graph(object):
y_offset = self.border_top + self.graph_height + 5 y_offset = self.border_top + self.graph_height + 5
if self.show_x_labels: if self.show_x_labels:
max_x_label_height_px = self.x_label_font_size max_x_label_height_px = self.x_label_font_size
if self.rotate_x_labels: if self.x_label_rotation:
longest_label_length = max(map(len, self.get_x_labels())) longest_label_length = max(map(len, self.get_x_labels()))
# I think 0.6 is the ratio of width to height of characters max_x_label_height_px *= longest_label_length
max_x_label_height_px *= longest_label_length * 0.6 max_x_label_height_px *= sin(self.x_label_rotation)
y_offset += max_x_label_height_px y_offset += max_x_label_height_px
if self.stagger_x_labels: if self.stagger_x_labels:
y_offset += max_x_label_height_px + 5 y_offset += max_x_label_height_px + 5
@ -651,7 +660,7 @@ class Graph(object):
self.root.addprevious(pi) self.root.addprevious(pi)
comment_strings = ( comment_strings = (
u'Generatel with pygal ©Kozea 2011', u'Generated with pygal ©Kozea 2011',
'Based upon SVG.Graph by Jason R. Coombs', 'Based upon SVG.Graph by Jason R. Coombs',
) )
map(self.root.append, map(etree.Comment, comment_strings)) map(self.root.append, map(etree.Comment, comment_strings))

Loading…
Cancel
Save