How to use the leather.theme function in leather

To help you get started, we’ve selected a few leather examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wireservice / leather / leather / chart.py View on Github external
left=default_margin
            )
        elif isinstance(margin, int):
            margin = Box(margin, margin, margin, margin)
        elif not isinstance(margin, Box):
            margin = Box(*margin)

        # Root / background
        root_group = ET.Element('g')

        root_group.append(ET.Element('rect',
            x=six.text_type(0),
            y=six.text_type(0),
            width=six.text_type(width),
            height=six.text_type(height),
            fill=theme.background_color
        ))

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

        margin_width = width - (margin.left + margin.right)
        margin_height = height - (margin.top + margin.bottom)

        root_group.append(margin_group)

        # Header
        header_group = ET.Element('g')

        header_margin = 0
github wireservice / leather / examples / theme.py View on Github external
(2, 5),
    (4, 4),
    (8, 3)
]

dot_data = [
    (1, 3),
    (2, 5),
    (6, 9),
    (10, 4)
]

leather.theme.title_font_family = 'Comic Sans MS'
leather.theme.legend_font_family = 'Comic Sans MS'
leather.theme.tick_font_family = 'Comic Sans MS'
leather.theme.default_series_colors = ['#ff0000', '#00ff00']

chart = leather.Chart('Theme')
chart.add_line(line_data)
chart.add_dots(dot_data)
chart.to_svg('examples/charts/theme.svg')
github wireservice / leather / leather / chart.py View on Github external
margin_height = height - (margin.top + margin.bottom)

        root_group.append(margin_group)

        # Header
        header_group = ET.Element('g')

        header_margin = 0

        if self._title:
            label = ET.Element('text',
                x=six.text_type(0),
                y=six.text_type(0),
                fill=theme.title_color
            )
            label.set('font-family', theme.title_font_family)
            label.set('font-size', six.text_type(theme.title_font_size))
            label.text = six.text_type(self._title)

            header_group.append(label)
            header_margin += theme.title_font_char_height + theme.title_gap

        # Legend
        if len(self._layers) > 1 or isinstance(self._layers[0][0], CategorySeries):
            legend_group = ET.Element('g')
            legend_group.set('transform', svg.translate(0, header_margin))

            indent = 0
            rows = 1
            palette = self._palette()

            for series, shape in self._layers:
github wireservice / leather / leather / axis.py View on Github external
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
github wireservice / leather / leather / shapes / base.py View on Github external
else:
            fill_color = None

        if hasattr(self, '_stroke_color'):
            if self._stroke_color:
                if callable(self._stroke_color):
                    # TODO
                    stroke_color = 'black'
                else:
                    stroke_color = self._stroke_color
            else:
                stroke_color = next(palette)
        else:
            stroke_color = None

        bubble_width = theme.legend_bubble_size + theme.legend_bubble_offset

        text = six.text_type(series.name) if series.name is not None else 'Unnamed series'
        text_width = (len(text) + 4) * theme.legend_font_char_width

        item_width = text_width + bubble_width

        # Group
        item_group = ET.Element('g')

        # Bubble
        bubble = ET.Element('rect',
            x=six.text_type(0),
            y=six.text_type(-theme.legend_font_char_height + theme.legend_bubble_offset),
            width=six.text_type(theme.legend_bubble_size),
            height=six.text_type(theme.legend_bubble_size)
        )
github wireservice / leather / leather / axis.py View on Github external
"""
        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)
github wireservice / leather / leather / shapes / dots.py View on Github external
def __init__(self, fill_color=None, radius=None):
        self._fill_color = fill_color
        self._radius = radius or theme.default_dot_radius
github wireservice / leather / leather / legend.py View on Github external
def to_svg(self, width, layers):
        """
        Render a legend series list and return it for embedding in an SVG.
        """
        legend_group = ET.Element('g')

        bubble_width = theme.legend_bubble_size + theme.legend_bubble_offset

        rows = 1
        indent = 0

        for i, (series, shape) in enumerate(layers):
            text = six.text_type(series._name or 'Series %i' % i)
            text_width = (len(text) + 4) * theme.legend_font_char_width

            if indent + text_width + bubble_width > width:
                indent = 0
                rows += 1

            y = (rows - 1) * (theme.legend_font_char_height + theme.legend_gap)

            # Group
            item_group = ET.Element('g')
github wireservice / leather / leather / chart.py View on Github external
def to_svg_group(self, width=None, height=None):
        """
        Render this chart to an SVG group element.

        This can then be placed inside an :code:`<svg>` tag to make a complete
        SVG graphic.

        See :meth:`.Chart.to_svg` for arguments.
        """
        width = width or theme.default_width
        height = height or theme.default_height

        if not self._layers:
            raise ValueError('You must add at least one series to the chart before rendering.')

        if isinstance(theme.margin, float):
            default_margin = width * theme.margin

            margin = Box(
                top=default_margin,
                right=default_margin,
                bottom=default_margin,
                left=default_margin
            )
        elif isinstance(margin, int):
            margin = Box(margin, margin, margin, margin)</svg>
github wireservice / leather / leather / chart.py View on Github external
header_group = ET.Element('g')

        header_margin = 0

        if self._title:
            label = ET.Element('text',
                x=six.text_type(0),
                y=six.text_type(0),
                fill=theme.title_color
            )
            label.set('font-family', theme.title_font_family)
            label.set('font-size', six.text_type(theme.title_font_size))
            label.text = six.text_type(self._title)

            header_group.append(label)
            header_margin += theme.title_font_char_height + theme.title_gap

        # Legend
        if len(self._layers) > 1 or isinstance(self._layers[0][0], CategorySeries):
            legend_group = ET.Element('g')
            legend_group.set('transform', svg.translate(0, header_margin))

            indent = 0
            rows = 1
            palette = self._palette()

            for series, shape in self._layers:
                for item_group, item_width in shape.legend_to_svg(series, palette):
                    if indent + item_width > width:
                        indent = 0
                        rows += 1