Skip to content

Commit

Permalink
Add SVG helper file.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 18, 2016
1 parent b709ba4 commit cb01c52
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
15 changes: 7 additions & 8 deletions leather/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from leather.shapes.column import Column
from leather.shapes.dot import Dot
from leather.shapes.line import Line
from leather.utils import X, Y, DIMENSIONS, Box, svg_translate
import leather.svg as svg
from leather.utils import X, Y, DIMENSIONS, Box

DEFAULT_DOT = Dot()
DEFAULT_LINE = Line()
Expand Down Expand Up @@ -166,7 +167,7 @@ def to_svg_group(self, width, height, margin=None):
canvas_height = height - (margin.top + margin.bottom)

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

# Axes
axes_group = ET.Element('g')
Expand Down Expand Up @@ -195,15 +196,15 @@ def to_svg(self, path, width=600, height=600, margin=None):
:param path:
Filepath or file-like object to write to.
"""
svg = ET.Element('svg',
root = ET.Element('svg',
width=six.text_type(width),
height=six.text_type(height),
version='1.1',
xmlns='http://www.w3.org/2000/svg'
)

group = self.to_svg_group(width, height, margin)
svg.append(group)
root.append(group)

close = True

Expand All @@ -219,10 +220,8 @@ def to_svg(self, path, width=600, height=600, margin=None):

f = open(path, 'w')

f.write('<?xml version="1.0" standalone="no"?>\n')
f.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n')
f.write('"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
f.write(ET.tostring(svg, encoding='unicode'))
f.write(svg.HEADER)
f.write(ET.tostring(root, encoding='unicode'))
f.close()
finally:
if close and f is not None:
Expand Down
16 changes: 7 additions & 9 deletions leather/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import six

from leather.utils import svg_translate
import leather.svg as svg


class Grid(object):
Expand All @@ -26,7 +26,7 @@ def to_svg(self, path, width=1200, height=1200):
"""
Render the grid to an SVG.
"""
svg = ET.Element('svg',
root = ET.Element('svg',
width=six.text_type(width),
height=six.text_type(height),
version='1.1',
Expand All @@ -44,12 +44,12 @@ def to_svg(self, path, width=1200, height=1200):
y = math.floor(i / grid_width) * chart_height

group = ET.Element('g')
group.set('transform', svg_translate(x, y))
group.set('transform', svg.translate(x, y))

chart = chart.to_svg_group(chart_width, chart_height)
group.append(chart)
svg.append(group)

root.append(group)

close = True

Expand All @@ -65,10 +65,8 @@ def to_svg(self, path, width=1200, height=1200):

f = open(path, 'w')

f.write('<?xml version="1.0" standalone="no"?>\n')
f.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n')
f.write('"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
f.write(ET.tostring(svg, encoding='unicode'))
f.write(svg.HEADER)
f.write(ET.tostring(root, encoding='unicode'))
f.close()
finally:
if close and f is not None:
Expand Down
12 changes: 12 additions & 0 deletions leather/svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

"""
Helpers for working with SVG.
"""

HEADER = '<?xml version="1.0" standalone="no"?>\n' + \
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n' + \
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'

def translate(x, y):
return 'translate(%i %i)' % (x, y)
3 changes: 0 additions & 3 deletions leather/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@

#: Data structure for representing margins or other CSS-edge like properties
Box = namedtuple('Box', ['top', 'right', 'bottom', 'left'])

def svg_translate(x, y):
return 'translate(%i %i)' % (x, y)

0 comments on commit cb01c52

Please sign in to comment.