/usr/lib/python3/dist-packages/leather/axis.py is in python3-leather 0.3.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #!/usr/bin/env python
import xml.etree.ElementTree as ET
import six
from leather import svg
from leather import theme
class Axis(object):
"""
A horizontal or vertical chart axis.
:param ticks:
Instead of inferring tick values from the data, use exactly this
sequence of ticks values. These will still be passed to the
:code:`tick_formatter`.
:param tick_formatter:
An optional :func:`.tick_format_function`.
"""
def __init__(self, ticks=None, tick_formatter=None, name=None):
self._ticks = ticks
self._tick_formatter = tick_formatter
self._name = six.text_type(name) if name is not None else None
def _estimate_left_tick_width(self, scale):
"""
Estimate the y axis space used by tick labels.
"""
tick_values = self._ticks or scale.ticks()
tick_count = len(tick_values)
tick_formatter = self._tick_formatter or scale.format_tick
max_len = 0
for i, value in enumerate(tick_values):
max_len = max(max_len, len(tick_formatter(value, i, tick_count)))
return max_len * theme.tick_font_char_width
def estimate_label_margin(self, scale, orient):
"""
Estimate the space needed for the tick labels.
"""
margin = 0
if orient == 'left':
margin += self._estimate_left_tick_width(scale) + (theme.tick_size * 2)
elif orient == 'bottom':
margin += theme.tick_font_char_height + (theme.tick_size * 2)
if self._name:
margin += theme.axis_title_font_char_height + theme.axis_title_gap
return margin
def to_svg(self, width, height, scale, orient):
"""
Render this axis to SVG elements.
"""
group = ET.Element('g')
group.set('class', 'axis ' + orient)
# Axis title
if self._name is not None:
if orient == 'left':
title_x = -(self._estimate_left_tick_width(scale) + theme.axis_title_gap)
title_y = height / 2
dy=''
transform = svg.rotate(270, title_x, title_y)
elif orient == 'bottom':
title_x = width / 2
title_y = height + theme.tick_font_char_height + (theme.tick_size * 2) + theme.axis_title_gap
dy='1em'
transform = ''
title = ET.Element('text',
x=six.text_type(title_x),
y=six.text_type(title_y),
dy=dy,
fill=theme.axis_title_color,
transform=transform
)
title.set('text-anchor', 'middle')
title.set('font-family', theme.axis_title_font_family)
title.text = self._name
group.append(title)
# Ticks
if orient == 'left':
label_x = -(theme.tick_size * 2)
x1 = -theme.tick_size
x2 = width
range_min = height
range_max = 0
elif orient == 'bottom':
label_y = height + (theme.tick_size * 2)
y1 = 0
y2 = height + theme.tick_size
range_min = 0
range_max = width
tick_values = self._ticks or scale.ticks()
tick_count = len(tick_values)
tick_formatter = self._tick_formatter or scale.format_tick
zero_tick_group = None
for i, value in enumerate(tick_values):
# Tick group
tick_group = ET.Element('g')
tick_group.set('class', 'tick')
if value == 0:
zero_tick_group = tick_group
else:
group.append(tick_group)
# Tick line
projected_value = scale.project(value, range_min, range_max)
if value == 0:
tick_color = theme.zero_color
else:
tick_color = theme.tick_color
if orient == 'left':
y1 = projected_value
y2 = projected_value
elif orient == 'bottom':
x1 = projected_value
x2 = projected_value
tick = ET.Element('line',
x1=six.text_type(x1),
y1=six.text_type(y1),
x2=six.text_type(x2),
y2=six.text_type(y2),
stroke=tick_color
)
tick.set('stroke-width', six.text_type(theme.tick_width))
tick_group.append(tick)
# Tick label
if orient == 'left':
x = label_x
y = projected_value
dy = '0.32em'
text_anchor = 'end'
elif orient == 'bottom':
x = projected_value
y = label_y
dy = '1em'
text_anchor = 'middle'
label = ET.Element('text',
x=six.text_type(x),
y=six.text_type(y),
dy=dy,
fill=theme.label_color
)
label.set('text-anchor', text_anchor)
label.set('font-family', theme.tick_font_family)
value = tick_formatter(value, i, tick_count)
label.text = six.text_type(value)
tick_group.append(label)
if zero_tick_group is not None:
group.append(zero_tick_group)
return group
def tick_format_function(value, index, tick_count):
"""
This example shows how to define a function to format tick values for
display.
:param x:
The value to be formatted.
:param index:
The index of the tick.
:param tick_count:
The total number of ticks being displayed.
:returns:
A stringified tick value for display.
"""
return six.text_type(value)
|