Skip to content

Commit

Permalink
Auto-sizing for grid and lattice.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 19, 2016
1 parent 9aec927 commit eb8cb78
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 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.
12 changes: 11 additions & 1 deletion leather/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import six

import leather.svg as svg
from leather import theme


class Grid(object):
Expand All @@ -22,10 +23,19 @@ def add_chart(self, chart):
"""
self._charts.append(chart)

def to_svg(self, path, width=1200, height=1200):
def to_svg(self, path, width=None, height=None):
"""
Render the grid to an SVG.
"""
if not width or not height:
count = len(self._charts)

columns = math.ceil(math.sqrt(count))
rows = math.ceil(count / columns)

width = columns * theme.default_width
height = rows * theme.default_height

root = ET.Element('svg',
width=six.text_type(width),
height=six.text_type(height),
Expand Down
2 changes: 1 addition & 1 deletion leather/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _validate_dimension(self, dimension, chart_series):

return Scale.infer(chart_series, dimension, data_type)

def to_svg(self, path, width=1200, height=1200):
def to_svg(self, path, width=None, height=None):
"""
Render the grid to an SVG.
"""
Expand Down
2 changes: 1 addition & 1 deletion leather/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Chart
default_width = 800
default_height = 400
default_height = 600
background_color = '#f9f9f9'
margin = 0.05

Expand Down
92 changes: 51 additions & 41 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,6 @@
# chart.add_dot(dot_data)
# chart.to_svg('test.svg')

# data = [[
# (0, 3),
# (4, 5),
# (7, 9),
# (8, 4)
# ], [
# (0, 4),
# (1, 3),
# (2, 5),
# (5, 6),
# (9, 10)
# ]]
#
# grid = leather.Grid()
#
# chart = leather.Chart('Chart A')
# chart.add_lines(data[0])
# grid.add_chart(chart)
#
# chart = leather.Chart('Chart B')
# chart.add_dots(data[1])
# grid.add_chart(chart)
#
# grid.to_svg('test.svg', 1200, 600)

data = [[
(0, 3),
(4, 5),
Expand All @@ -75,26 +50,61 @@
], [
(0, 4),
(1, 3),
(2, 3),
(10, 7),
(15, 5)
], [
(2, 5),
(5, 6),
(9, 10)
],[
(0, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8)
], [
(4, 4),
(6, 3),
(7, 5),
(8, 6),
(12, 10)
(1, 3),
(2, 5),
(5, 6),
(9, 10)
]]

lattice = leather.Lattice(data, leather.Lines('purple'), ['A', 'B', 'C', 'D'])
grid = leather.Grid()

chart = leather.Chart('Chart A')
chart.add_lines(data[0])
grid.add_chart(chart)

chart = leather.Chart('Chart B')
chart.add_dots(data[1])
grid.add_chart(chart)

chart = leather.Chart('Chart C')
chart.add_dots(data[2])
grid.add_chart(chart)

grid.to_svg('test.svg')

lattice.to_svg('test.svg', 1200, 600)
# data = [[
# (0, 3),
# (4, 5),
# (7, 9),
# (8, 4)
# ], [
# (0, 4),
# (1, 3),
# (2, 3),
# (10, 7),
# (15, 5)
# ], [
# (0, 4),
# (5, 5),
# (6, 6),
# (7, 7),
# (8, 8)
# ], [
# (4, 4),
# (6, 3),
# (7, 5),
# (8, 6),
# (12, 10)
# ]]
#
# lattice = leather.Lattice(data, leather.Lines('purple'), ['A', 'B', 'C', 'D'])
#
# lattice.to_svg('test.svg', 1200, 600)

# data = [
# (3, 1),
Expand Down

0 comments on commit eb8cb78

Please sign in to comment.