Example #1
0
File: svg.py Project: Kozea/pygal
    def gauge_background(self, serie_node, start_angle, center, radius, small_radius, end_angle, half_pie, max_value):

        if end_angle == 2 * pi:
            end_angle = nearly_2pi

        to_shade = [
            coord_abs_project(center, radius, start_angle),
            coord_abs_project(center, radius, end_angle),
            coord_abs_project(center, small_radius, end_angle),
            coord_abs_project(center, small_radius, start_angle),
        ]

        self.node(
            serie_node["plot"],
            "path",
            d="M%s A%s 0 1 1 %s L%s A%s 0 1 0 %s z"
            % (to_shade[0], coord_dual(radius), to_shade[1], to_shade[2], coord_dual(small_radius), to_shade[3]),
            class_="gauge-background reactive",
        )

        if half_pie:
            begin_end = [
                coord_diff(center, coord_project(radius - (radius - small_radius) / 2, start_angle)),
                coord_diff(center, coord_project(radius - (radius - small_radius) / 2, end_angle)),
            ]
            pos = 0
            for i in begin_end:
                self.node(
                    serie_node["plot"],
                    "text",
                    class_="y-{} bound reactive".format(pos),
                    x=i[0],
                    y=i[1] + 10,
                    attrib={"text-anchor": "middle"},
                ).text = "{}".format(0 if pos == 0 else max_value)
                pos += 1
        else:
            middle_radius = 0.5 * (radius + small_radius)
            # Correct text vertical alignment
            middle_radius -= 0.1 * (radius - small_radius)
            to_labels = [
                coord_abs_project(center, middle_radius, 0),
                coord_abs_project(center, middle_radius, nearly_2pi),
            ]
            self.node(
                self.defs,
                "path",
                id="valuePath-%s%s" % center,
                d="M%s A%s 0 1 1 %s" % (to_labels[0], coord_dual(middle_radius), to_labels[1]),
            )
            text_ = self.node(serie_node["text_overlay"], "text")
            self.node(
                text_,
                "textPath",
                class_="max-value reactive",
                attrib={"href": "#valuePath-%s%s" % center, "startOffset": "99%", "text-anchor": "end"},
            ).text = max_value
Example #2
0
    def solid_gauge(self, serie_node, node, radius, small_radius, angle,
                    start_angle, center, val, i, metadata, half_pie, end_angle,
                    max_value):
        """Draw a solid gauge slice and background slice"""
        if angle == 2 * pi:
            angle = nearly_2pi

        if angle > 0:
            to = [
                coord_abs_project(center, radius, start_angle),
                coord_abs_project(center, radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle)
            ]

            self.node(
                node,
                'path',
                d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' %
                (to[0], coord_dual(radius), int(angle > pi), to[1], to[2],
                 coord_dual(small_radius), int(angle > pi), to[3]),
                class_='slice reactive tooltip-trigger')
        else:
            return

        x, y = coord_diff(
            center,
            coord_project((radius + small_radius) / 2,
                          start_angle + angle / 2))
        self.graph._static_value(serie_node, val, x, y, metadata, 'middle')
        self.graph._tooltip_data(
            node, val, x, y, "centered", self.graph._x_labels
            and self.graph._x_labels[i][0])
Example #3
0
    def slice(self, serie_node, node, radius, small_radius, angle, start_angle,
              center, val, i, metadata):
        """Draw a pie slice"""
        if angle == 2 * pi:
            angle = nearly_2pi

        if angle > 0:
            to = [
                coord_abs_project(center, radius, start_angle),
                coord_abs_project(center, radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle)
            ]
            rv = self.node(
                node,
                'path',
                d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' %
                (to[0], coord_dual(radius), int(angle > pi), to[1], to[2],
                 coord_dual(small_radius), int(angle > pi), to[3]),
                class_='slice reactive tooltip-trigger')
        else:
            rv = None
        x, y = coord_diff(
            center,
            coord_project((radius + small_radius) / 2,
                          start_angle + angle / 2))

        self.graph._tooltip_data(
            node, val, x, y, "centered", self.graph._x_labels
            and self.graph._x_labels[i][0])
        if angle >= 0.3:  # 0.3 radians is about 17 degrees
            self.graph._static_value(serie_node, val, x, y, metadata)
        return rv
Example #4
0
    def solid_gauge(
            self, serie_node, node, radius, small_radius,
            angle, start_angle, center, val, i, metadata, half_pie, end_angle,
            max_value):
        """Draw a solid gauge slice and background slice"""
        if angle == 2 * pi:
            angle = nearly_2pi

        if angle > 0:
            to = [coord_abs_project(center, radius, start_angle),
                  coord_abs_project(center, radius, start_angle + angle),
                  coord_abs_project(center, small_radius, start_angle + angle),
                  coord_abs_project(center, small_radius, start_angle)]

            self.node(
                node, 'path',
                d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' % (
                    to[0],
                    coord_dual(radius),
                    int(angle > pi),
                    to[1],
                    to[2],
                    coord_dual(small_radius),
                    int(angle > pi),
                    to[3]),
                class_='slice reactive tooltip-trigger')
        else:
            return

        x, y = coord_diff(center, coord_project(
                (radius + small_radius) / 2, start_angle + angle / 2))
        self.graph._static_value(serie_node, val, x, y, metadata, 'middle')
        self.graph._tooltip_data(
            node, val, x, y, "centered",
            self.graph._x_labels and self.graph._x_labels[i][0])
Example #5
0
    def slice(
            self, serie_node, node, radius, small_radius,
            angle, start_angle, center, val, i, metadata):
        """Draw a pie slice"""
        if angle == 2 * pi:
            angle = nearly_2pi

        if angle > 0:
            to = [coord_abs_project(center, radius, start_angle),
                  coord_abs_project(center, radius, start_angle + angle),
                  coord_abs_project(center, small_radius, start_angle + angle),
                  coord_abs_project(center, small_radius, start_angle)]
            rv = self.node(
                node, 'path',
                d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' % (
                    to[0],
                    coord_dual(radius), int(angle > pi), to[1],
                    to[2],
                    coord_dual(small_radius), int(angle > pi), to[3]),
                class_='slice reactive tooltip-trigger')
        else:
            rv = None
        x, y = coord_diff(center, coord_project(
            (radius + small_radius) / 2, start_angle + angle / 2))

        self.graph._tooltip_data(
            node, val, x, y, "centered",
            self.graph._x_labels and self.graph._x_labels[i][0])
        if angle >= 0.3:  # 0.3 radians is about 17 degrees
            self.graph._static_value(serie_node, val, x, y, metadata)
        return rv
Example #6
0
    def slice(self, serie_node, node, radius, small_radius, angle, start_angle,
              center, val, i, metadata):
        """Draw a pie slice"""
        if angle == 2 * pi:
            angle = nearly_2pi

        if angle > 0:
            to = [
                coord_abs_project(center, radius, start_angle),
                coord_abs_project(center, radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle + angle),
                coord_abs_project(center, small_radius, start_angle)
            ]
            rv = self.node(
                node,
                'path',
                d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' %
                (to[0], coord_dual(radius), int(angle > pi), to[1], to[2],
                 coord_dual(small_radius), int(angle > pi), to[3]),
                class_='slice reactive tooltip-trigger')
        else:
            rv = None
        co = coord_project((radius + small_radius), start_angle + angle / 2)
        x, y = coord_diff(center, co)

        # Hack to properly place slice values
        if co[0] > 0 and co[1] > 0:
            x = x - 25 if co[0] > 25 else x - 10
            y = y - 25

        elif co[0] < 0 and co[1] > 0:
            x = x + 20
            y = y - 20

        elif co[0] < 0 and co[1] < 0:
            x = x + 20
            y = y + 20

        elif co[0] > 0 and co[1] < 0:
            x = x - 20
            y = y + 20

        self.graph._tooltip_data(
            node, val, x, y, "centered", self.graph._x_labels
            and self.graph._x_labels[i][0])
        self.graph._static_value(serie_node, val, x, y, metadata)
        return rv
Example #7
0
    def gauge_background(
            self, serie_node, start_angle, center, radius, small_radius,
            end_angle, half_pie, max_value
    ):

        if end_angle == 2 * pi:
            end_angle = nearly_2pi

        to_shade = [
            coord_abs_project(center, radius, start_angle),
            coord_abs_project(center, radius, end_angle),
            coord_abs_project(center, small_radius, end_angle),
            coord_abs_project(center, small_radius, start_angle)
        ]

        self.node(
            serie_node['plot'],
            'path',
            d='M%s A%s 0 1 1 %s L%s A%s 0 1 0 %s z' % (
                to_shade[0], coord_dual(radius), to_shade[1], to_shade[2],
                coord_dual(small_radius), to_shade[3]
            ),
            class_='gauge-background reactive'
        )

        if half_pie:
            begin_end = [
                coord_diff(
                    center,
                    coord_project(
                        radius - (radius - small_radius) / 2, start_angle
                    )
                ),
                coord_diff(
                    center,
                    coord_project(
                        radius - (radius - small_radius) / 2, end_angle
                    )
                )
            ]
            pos = 0
            for i in begin_end:
                self.node(
                    serie_node['plot'],
                    'text',
                    class_='y-{} bound reactive'.format(pos),
                    x=i[0],
                    y=i[1] + 10,
                    attrib={
                        'text-anchor': 'middle'
                    }
                ).text = '{}'.format(0 if pos == 0 else max_value)
                pos += 1
        else:
            middle_radius = .5 * (radius + small_radius)
            # Correct text vertical alignment
            middle_radius -= .1 * (radius - small_radius)
            to_labels = [
                coord_abs_project(center, middle_radius, 0),
                coord_abs_project(center, middle_radius, nearly_2pi)
            ]
            self.node(
                self.defs,
                'path',
                id='valuePath-%s%s' % center,
                d='M%s A%s 0 1 1 %s' %
                (to_labels[0], coord_dual(middle_radius), to_labels[1])
            )
            text_ = self.node(serie_node['text_overlay'], 'text')
            self.node(
                text_,
                'textPath',
                class_='max-value reactive',
                attrib={
                    'href': '#valuePath-%s%s' % center,
                    'startOffset': '99%',
                    'text-anchor': 'end'
                }
            ).text = max_value
Example #8
0
    def gauge_background(
            self, serie_node, start_angle, center, radius, small_radius,
            end_angle, half_pie, max_value):

        if end_angle == 2 * pi:
            end_angle = nearly_2pi

        to_shade = [
            coord_abs_project(center, radius, start_angle),
            coord_abs_project(center, radius, end_angle),
            coord_abs_project(center, small_radius, end_angle),
            coord_abs_project(center, small_radius, start_angle)]

        self.node(
            serie_node['plot'], 'path',
            d='M%s A%s 0 1 1 %s L%s A%s 0 1 0 %s z' % (
                to_shade[0],
                coord_dual(radius),
                to_shade[1],
                to_shade[2],
                coord_dual(small_radius),
                to_shade[3]),
            class_='gauge-background reactive')

        if half_pie:
            begin_end = [
                coord_diff(
                    center,
                    coord_project(
                        radius-(radius-small_radius)/2, start_angle)),
                coord_diff(
                    center,
                    coord_project(
                        radius-(radius-small_radius)/2, end_angle))]
            pos = 0
            for i in begin_end:
                self.node(
                    serie_node['plot'], 'text',
                    class_='y-{} bound reactive'.format(pos),
                    x=i[0],
                    y=i[1]+10,
                    attrib={'text-anchor': 'middle'}
                ).text = '{}'.format(0 if pos == 0 else max_value)
                pos += 1
        else:
            middle_radius = .5 * (radius + small_radius)
            # Correct text vertical alignment
            middle_radius -= .1 * (radius - small_radius)
            to_labels = [
                coord_abs_project(
                    center, middle_radius, 0),
                coord_abs_project(
                    center, middle_radius, nearly_2pi)
            ]
            self.node(
                self.defs, 'path', id='valuePath-%s%s' % center,
                d='M%s A%s 0 1 1 %s' % (
                    to_labels[0],
                    coord_dual(middle_radius),
                    to_labels[1]
                ))
            text_ = self.node(
                serie_node['text_overlay'], 'text')
            self.node(
                text_, 'textPath', class_='max-value reactive',
                attrib={
                    'href': '#valuePath-%s%s' % center,
                    'startOffset': '99%',
                    'text-anchor': 'end'
                }
            ).text = max_value