Pie Chart

A pie chart is a circular statistical graphic which is divided into slices to illustrate numerical proportion.

For a pie chart we must define an rx.recharts.pie() component for each set of values we wish to plot. Each rx.recharts.pie() component has a data, a data_key and a name_key which clearly states which data and which variables in our data we are tracking. In this simple example we plot value column as our data_key against the name column which we set as our name_key. We also use the fill prop to set the color of the pie slices.

def pie_simple():
    return rx.recharts.pie_chart(
        rx.recharts.pie(
            data=data01,
            data_key="value",
            name_key="name",
            fill="#8884d8",
            label=True,
        ),
        width="100%",
        height=300,
    )

We can also add two pies on one chart by using two rx.recharts.pie components.

In this example inner_radius and outer_radius props are used. They define the doughnut shape of a pie chart: inner_radius creates the hollow center (use "0%" for a full pie), while outer_radius sets the overall size. The padding_angle prop, used on the green pie below, adds space between pie slices, enhancing visibility of individual segments.

def pie_double():
    return rx.recharts.pie_chart(
        rx.recharts.pie(
            data=data01,
            data_key="value",
            name_key="name",
            fill="#82ca9d",
            inner_radius="60%",
            padding_angle=5,
        ),
        rx.recharts.pie(
            data=data02,
            data_key="value",
            name_key="name",
            fill="#8884d8",
            outer_radius="50%",
        ),
        rx.recharts.graphing_tooltip(),
        width="100%",
        height=300,
    )

Dynamic Data

Chart data tied to a State var causes the chart to automatically update when the state changes, providing a nice way to visualize data in response to user interface elements. View the "Data" tab to see the substate driving this half-pie chart.

🏆1

🪵1

🥑1

🧱1

from typing import Any


class PieChartState(rx.State):
    resources: list[dict[str, Any]] = [
        dict(type_="🏆", count=1),
        dict(type_="🪵", count=1),
        dict(type_="🥑", count=1),
        dict(type_="🧱", count=1),
    ]

    @rx.var(cache=True)
    def resource_types(self) -> list[str]:
        return [r["type_"] for r in self.resources]

    def increment(self, type_: str):
        for resource in self.resources:
            if resource["type_"] == type_:
                resource["count"] += 1
                break

    def decrement(self, type_: str):
        for resource in self.resources:
            if (
                resource["type_"] == type_
                and resource["count"] > 0
            ):
                resource["count"] -= 1
                break


def dynamic_pie_example():
    return rx.hstack(
        rx.recharts.pie_chart(
            rx.recharts.pie(
                data=PieChartState.resources,
                data_key="count",
                name_key="type_",
                cx="50%",
                cy="50%",
                start_angle=180,
                end_angle=0,
                fill="#8884d8",
                label=True,
            ),
            rx.recharts.graphing_tooltip(),
        ),
        rx.vstack(
            rx.foreach(
                PieChartState.resource_types,
                lambda type_, i: rx.hstack(
                    rx.button(
                        "-",
                        on_click=PieChartState.decrement(
                            type_
                        ),
                    ),
                    rx.text(
                        type_,
                        PieChartState.resources[i]["count"],
                    ),
                    rx.button(
                        "+",
                        on_click=PieChartState.increment(
                            type_
                        ),
                    ),
                ),
            ),
        ),
        width="100%",
        height="15em",
    )

API Reference

rx.recharts.PieChart

A Pie chart component in Recharts.

PropType | ValuesDefault
margin
Dict[str, Any]
width
Union[str, int]
"100%"
height
Union[str, int]
"100%"

Valid Children

PolarAngleAxisPolarRadiusAxisPolarGridLegendGraphingTooltipPie

rx.recharts.Pie

A Pie chart component in Recharts.

PropType | ValuesDefault
data
List
data_key
Union[str, int]
cx
Union[str, int]
"50%"
cy
Union[str, int]
"50%"
inner_radius
Union[str, int]
0
outer_radius
Union[str, int]
"80%"
start_angle
int
0
end_angle
int
360
min_angle
int
0
padding_angle
int
0
name_key
str
"name"
legend_type
"line" | "plainline" | ...
"rect"
label
bool
False
label_line
bool
False
data
List
stroke
Union[str, Color]
rx.color("accent", 9)
fill
Union[str, Color]
rx.color("accent", 3)
is_animation_active
bool
true in CSR, and false in SSR
animation_begin
int
400
animation_duration
int
1500
animation_easing
"ease" | "ease-in" | ...
"ease"
root_tab_index
int
0

Valid Children

CellLabelList

Event Triggers

See the full list of default event triggers
TriggerDescription
on_animation_startThe on_animation_start event handler is called when the animation starts. It receives the animation name as an argument.
on_animation_endThe on_animation_end event handler is called when the animation ends. It receives the animation name as an argument.