How to use the leather.svg 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 / grid.py View on Github external
# Charts
        grid_group = ET.Element('g')

        chart_count = len(self._charts)
        grid_width = math.ceil(math.sqrt(chart_count))
        grid_height = math.ceil(chart_count / grid_width)
        chart_width = width / grid_width
        chart_height = height / grid_height

        for i, chart in enumerate(self._charts):
            x = (i % grid_width) * chart_width
            y = math.floor(i / grid_width) * chart_height

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

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

            grid_group.append(group)

        root_group.append(grid_group)

        svg_text = svg.stringify(root)
        close = True

        if path:
            f = None

            try:
                if hasattr(path, 'write'):
github wireservice / leather / leather / legend.py View on Github external
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')
            item_group.set('transform', svg.translate(indent, y))

            fill_color = getattr(shape, '_fill_color', None)
            stroke_color = getattr(shape, '_stroke_color', None)

            if callable(fill_color):
                # TODO
                fill_color = 'black'

            # 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 / chart.py View on Github external
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

        if self._title:
            label = ET.Element('text',
                x=six.text_type(0),
                y=six.text_type(0),
                fill=theme.title_color
github wireservice / leather / leather / chart.py View on Github external
x_scale, x_axis = self._validate_dimension(X)
        y_scale, y_axis = self._validate_dimension(Y)

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

        canvas_width = body_width - left_margin
        canvas_height = body_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'))

        header_group.set('transform', svg.translate(left_margin, 0))

        body_group.append(axes_group)

        # Series
        series_group = ET.Element('g')

        palette = self._palette()

        for series, shape in self._layers:
            series_group.append(shape.to_svg(canvas_width, canvas_height, x_scale, y_scale, series, palette))

        axes_group.append(series_group)

        return root_group
github wireservice / leather / leather / grid.py View on Github external
for i, chart in enumerate(self._charts):
            x = (i % grid_width) * chart_width
            y = math.floor(i / grid_width) * chart_height

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

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

            grid_group.append(group)

        root_group.append(grid_group)

        svg_text = svg.stringify(root)
        close = True

        if path:
            f = None

            try:
                if hasattr(path, 'write'):
                    f = path
                    close = False
                else:
                    dirpath = os.path.dirname(path)

                    if dirpath and not os.path.exists(dirpath):
                        os.makedirs(dirpath)

                    f = open(path, 'w')
github wireservice / leather / leather / axis.py View on Github external
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