def _get_pie_svg_with_legend(self, pie_svg_text):
        """Return SVG text changed so it includes a legend."""
        ### create root element
        pie_svg_tag = XML(pie_svg_text)

        ### remove first rect, since it is used as background, and the html
        ### presention doesn't need one
        rect_to_remove = pie_svg_tag.find('rect')
        pie_svg_tag.remove(rect_to_remove)

        ### create a rect representing the pie

        pie_rect = (

            Rect(
                0,
                0,
                int(pie_svg_tag.get('width')),
                int(pie_svg_tag.get('height')),
            )

        )

        ### position legend relatively and unite it with te pie rect instance

        LEGEND_RECTSMAN.midleft = pie_rect.move(10, 0).midright
        pie_rect.union_ip(LEGEND_RECTSMAN)

        ### update dimensions of pie svg tag with pie rect

        for dimension in ('width', 'height'):
            pie_svg_tag.set(dimension, str(getattr(pie_rect, dimension)))

        pie_svg_tag.set('viewBox', f'0 0 {pie_rect.width} {pie_rect.height}')

        ### create and add legend shapes/text

        for index, key in enumerate(LEGEND_ORDER):

            color_rect, text_rect = LEGEND_RECT_PAIRS[index]

            ## get rect tag

            color = RESULT_COLOR_MAP[key]

            ###
            rect_tag = XML(
                get_rect_svg_text(
                    0, 0, 16, 16,
                    fill_color=color,
                    outline_color='black',
                    outline_width=2,
                )
            ).find('rect')

            ## set x and y

            for char in 'xy':
                rect_tag.set(char, str(getattr(color_rect, char)))

            ## append it to pie svg
            pie_svg_tag.append(rect_tag)

            ## create text tag

            title = TITLE_MAP[key]

            x, y = text_rect.bottomleft

            pie_svg_tag.append(
                XML(f'<text x="{x}" y="{y}">{title}</text>')
            )

        ### return pie svg from updated tag
        return (
            element_to_string(
                pie_svg_tag,
                encoding='unicode',
            )
        ).replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"')
Return SVG text changed so it includes a legend.
nodezator/systemtesting/reportviewer/__init__.py