def _x_axis(self, draw_axes=True):
"""Override x axis to make it polar"""
if not self._x_labels or not self.show_x_labels:
return
axis = self.svg.node(self.nodes["plot"], class_="axis x web%s" % (" always_show" if self.show_x_guides else ""))
format_ = lambda x: "%f %f" % x
center = self.view((0, 0))
r = self._rmax
# Can't simply determine truncation
truncation = self.truncate_label or 25
for label, theta in self._x_labels:
major = label in self._x_labels_major
if not (self.show_minor_x_labels or major):
continue
guides = self.svg.node(axis, class_="guides")
end = self.view((r, theta))
self.svg.node(
guides,
"path",
d="M%s L%s" % (format_(center), format_(end)),
class_="%s%sline" % ("axis " if label == "0" else "", "major " if major else ""),
)
r_txt = (1 - self._box.__class__.margin) * self._box.ymax
pos_text = self.view((r_txt, theta))
text = self.svg.node(guides, "text", x=pos_text[0], y=pos_text[1], class_="major" if major else "")
text.text = truncate(label, truncation)
if text.text != label:
self.svg.node(guides, "title").text = label
else:
self.svg.node(guides, "title").text = self._x_format(theta)
angle = -theta + pi / 2
if cos(angle) < 0:
angle -= pi
text.attrib["transform"] = "rotate(%f %s)" % (self.x_label_rotation or deg(angle), format_(pos_text))
开发者ID:madaiz11,项目名称:pygal,代码行数:40,代码来源:radar.py
示例12: _x_axis
def _x_axis(self):
"""Make the x axis: labels and guides"""
if not self._x_labels:
return
axis = self.svg.node(self.nodes['plot'], class_="axis x%s" % (
' always_show' if self.show_x_guides else ''
))
truncation = self.truncate_label
if not truncation:
if self.x_label_rotation or len(self._x_labels) <= 1:
truncation = 25
else:
first_label_position = self.view.x(self._x_labels[0][1]) or 0
last_label_position = self.view.x(self._x_labels[-1][1]) or 0
available_space = (
last_label_position - first_label_position) / (
len(self._x_labels) - 1)
truncation = reverse_text_len(
available_space, self.label_font_size)
truncation = max(truncation, 1)
if 0 not in [label[1] for label in self._x_labels]:
self.svg.node(axis, 'path',
d='M%f %f v%f' % (0, 0, self.view.height),
class_='line')
lastlabel = self._x_labels[-1][0]
for label, position in self._x_labels:
major = label in self._x_major_labels
if not (self.show_minor_x_labels or major):
continue
guides = self.svg.node(axis, class_='guides')
x = self.view.x(position)
y = self.view.height + 5
last_guide = (self._y_2nd_labels and label == lastlabel)
self.svg.node(
guides, 'path',
d='M%f %f v%f' % (x or 0, 0, self.view.height),
class_='%s%sline' % (
'major ' if major else '',
'guide ' if position != 0 and not last_guide else ''))
y += .5 * self.label_font_size + 5
text = self.svg.node(
guides, 'text',
x=x,
y=y,
class_='major' if major else ''
)
if isinstance(label, dict):
label = label['title']
text.text = truncate(label, truncation)
if text.text != label:
self.svg.node(guides, 'title').text = label
if self.x_label_rotation:
text.attrib['transform'] = "rotate(%d %f %f)" % (
self.x_label_rotation, x, y)
if self._x_2nd_labels:
secondary_ax = self.svg.node(
self.nodes['plot'], class_="axis x x2%s" % (
' always_show' if self.show_x_guides else ''
))
for label, position in self._x_2nd_labels:
major = label in self._x_major_labels
if not (self.show_minor_x_labels or major):
continue
# it is needed, to have the same structure as primary axis
guides = self.svg.node(secondary_ax, class_='guides')
x = self.view.x(position)
y = -5
text = self.svg.node(
guides, 'text',
x=x,
y=y,
class_='major' if major else ''
)
text.text = label
if self.x_label_rotation:
text.attrib['transform'] = "rotate(%d %f %f)" % (
-self.x_label_rotation, x, y)
def _x_axis(self):
"""Make the x axis: labels and guides"""
if not self._x_labels:
return
axis = self.svg.node(self.nodes["plot"], class_="axis x%s" % (" always_show" if self.show_x_guides else ""))
truncation = self.truncate_label
if not truncation:
if self.x_label_rotation or len(self._x_labels) <= 1:
truncation = 25
else:
first_label_position = self.view.x(self._x_labels[0][1])
last_label_position = self.view.x(self._x_labels[-1][1])
available_space = (last_label_position - first_label_position) / (len(self._x_labels) - 1)
truncation = reverse_text_len(available_space, self.label_font_size)
if 0 not in [label[1] for label in self._x_labels]:
self.svg.node(axis, "path", d="M%f %f v%f" % (0, 0, self.view.height), class_="line")
lastlabel = self._x_labels[-1][0]
if self.x_labels_major:
x_labels_major = self.x_labels_major
elif self.x_labels_major_every:
x_labels_major = [self._x_labels[i][0] for i in range(0, len(self._x_labels), self.x_labels_major_every)]
elif self.x_labels_major_count:
label_count = len(self._x_labels)
major_count = self.x_labels_major_count
if major_count >= label_count:
x_labels_major = [label[0] for label in self._x_labels]
else:
x_labels_major = [
self._x_labels[int(i * (label_count - 1) / (major_count - 1))][0] for i in range(major_count)
]
else:
x_labels_major = []
for label, position in self._x_labels:
major = label in x_labels_major
if not (self.show_minor_x_labels or major):
continue
guides = self.svg.node(axis, class_="guides")
x = self.view.x(position)
y = self.view.height + 5
last_guide = self._y_2nd_labels and label == lastlabel
self.svg.node(
guides,
"path",
d="M%f %f v%f" % (x, 0, self.view.height),
class_="%s%sline" % ("major " if major else "", "guide " if position != 0 and not last_guide else ""),
)
y += 0.5 * self.label_font_size + 5
text = self.svg.node(guides, "text", x=x, y=y, class_="major" if major else "")
text.text = truncate(label, truncation)
if text.text != label:
self.svg.node(guides, "title").text = label
if self.x_label_rotation:
text.attrib["transform"] = "rotate(%d %f %f)" % (self.x_label_rotation, x, y)
if self._x_2nd_labels:
secondary_ax = self.svg.node(
self.nodes["plot"], class_="axis x x2%s" % (" always_show" if self.show_x_guides else "")
)
for label, position in self._x_2nd_labels:
major = label in x_labels_major
if not (self.show_minor_x_labels or major):
continue
# it is needed, to have the same structure as primary axis
guides = self.svg.node(secondary_ax, class_="guides")
x = self.view.x(position)
y = -5
text = self.svg.node(guides, "text", x=x, y=y, class_="major" if major else "")
text.text = label
if self.x_label_rotation:
text.attrib["transform"] = "rotate(%d %f %f)" % (-self.x_label_rotation, x, y)
开发者ID:Qalthos,项目名称:pygal,代码行数:71,代码来源:graph.py
示例14: _x_axis
def _x_axis(self):
"""Make the x axis: labels and guides"""
if not self._x_labels or not self.show_x_labels:
return
axis = self.svg.node(self.nodes['plot'], class_="axis x%s" % (
' always_show' if self.show_x_guides else ''
))
truncation = self.truncate_label
if not truncation:
if self.x_label_rotation or len(self._x_labels) <= 1:
truncation = 25
else:
first_label_position = self.view.x(self._x_labels[0][1]) or 0
last_label_position = self.view.x(self._x_labels[-1][1]) or 0
available_space = (
last_label_position - first_label_position) / (
len(self._x_labels) - 1)
truncation = reverse_text_len(
available_space, self.style.label_font_size)
truncation = max(truncation, 1)
lastlabel = self._x_labels[-1][0]
if 0 not in [label[1] for label in self._x_labels]:
self.svg.node(axis, 'path',
d='M%f %f v%f' % (0, 0, self.view.height),
class_='line')
lastlabel = None
for label, position in self._x_labels:
if self.horizontal:
major = position in self._x_labels_major
else:
major = label in self._x_labels_major
if not (self.show_minor_x_labels or major):
continue
guides = self.svg.node(axis, class_='guides')
x = self.view.x(position)
if x is None:
continue
y = self.view.height + 5
last_guide = (self._y_2nd_labels and label == lastlabel)
self.svg.node(
guides, 'path',
d='M%f %f v%f' % (x or 0, 0, self.view.height),
class_='%s%s%sline' % (
'axis ' if label == "0" else '',
'major ' if major else '',
'guide ' if position != 0 and not last_guide else ''))
y += .5 * self.style.label_font_size + 5
text = self.svg.node(
guides, 'text',
x=x,
y=y,
class_='major' if major else ''
)
text.text = truncate(label, truncation)
if text.text != label:
self.svg.node(guides, 'title').text = label
elif self._dual:
self.svg.node(
guides, 'title',
).text = self._x_format(position)
if self.x_label_rotation:
text.attrib['transform'] = "rotate(%d %f %f)" % (
self.x_label_rotation, x, y)
if self.x_label_rotation >= 180:
text.attrib['class'] = ' '.join(
(text.attrib['class'] and text.attrib['class'].split(
' ') or []) + ['backwards'])
if self._y_2nd_labels and 0 not in [
label[1] for label in self._x_labels]:
self.svg.node(axis, 'path',
d='M%f %f v%f' % (
self.view.width, 0, self.view.height),
class_='line')
if self._x_2nd_labels:
secondary_ax = self.svg.node(
self.nodes['plot'], class_="axis x x2%s" % (
' always_show' if self.show_x_guides else ''
))
for label, position in self._x_2nd_labels:
major = label in self._x_labels_major
if not (self.show_minor_x_labels or major):
continue
# it is needed, to have the same structure as primary axis
guides = self.svg.node(secondary_ax, class_='guides')
x = self.view.x(position)
y = -5
text = self.svg.node(
guides, 'text',
x=x,
y=y,
class_='major' if major else ''
)
text.text = label
if self.x_label_rotation:
#.........这里部分代码省略.........
请发表评论