Skip to content

Commit

Permalink
Approximate tick fitting. #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 18, 2016
1 parent 73d78d4 commit 4b545f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/example.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion leather/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def __init__(self, ticks=5, tick_width='1px', tick_size=4, tick_color='#eee', la
self.label_color = label_color
self.zero_color = zero_color

self.label_font_family = 'Monaco'
self.label_font_size = '14px'
self.label_font_char_height = 14
self.label_font_char_width = 8

def compute_text_margin(self, scale, orient):
if orient == 'left':
max_len = max(len(six.text_type(t)) for t in scale.ticks(self.ticks))
return max_len * self.label_font_char_width
elif orient == 'bottom':
return self.label_font_char_height

def to_svg(self, width, height, scale, orient):
"""
Render this axis to SVG elements.
Expand Down Expand Up @@ -84,8 +96,8 @@ def to_svg(self, width, height, scale, orient):
dy=dy,
fill=self.label_color
)

label.set('text-anchor', text_anchor)
label.set('font-family', 'Monaco')
label.text = six.text_type(value)

tick_group.append(tick)
Expand Down
17 changes: 12 additions & 5 deletions leather/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,25 @@ def to_svg_group(self, width, height, margin=None):
elif not isinstance(margin, Box):
margin = Box(*margin)

canvas_width = width - (margin.left + margin.right)
canvas_height = height - (margin.top + margin.bottom)
root_width = width - (margin.left + margin.right)
root_height = height - (margin.top + margin.bottom)

root_group = ET.Element('g')
root_group.set('transform', svg.translate(margin.left, margin.top))

# Axes
axes_group = ET.Element('g')

x_scale, x_axis = self._validate_dimension(X)
y_scale, y_axis = self._validate_dimension(Y)

bottom_margin = x_axis.compute_text_margin(x_scale, 'bottom')
left_margin = y_axis.compute_text_margin(y_scale, 'left')

canvas_width = root_width - left_margin
canvas_height = root_height - bottom_margin

axes_group = ET.Element('g')
axes_group.set('transform', svg.translate(left_margin, 0))

axes_group.append(x_axis.to_svg(canvas_width, canvas_height, x_scale, 'bottom'))
axes_group.append(y_axis.to_svg(canvas_width, canvas_height, y_scale, 'left'))

Expand All @@ -166,8 +173,8 @@ def to_svg_group(self, width, height, margin=None):
for series in self._layers:
series_group.append(series.to_svg(canvas_width, canvas_height, x_scale, y_scale))

axes_group.append(series_group)
root_group.append(axes_group)
root_group.append(series_group)

return root_group

Expand Down

0 comments on commit 4b545f4

Please sign in to comment.