✨ Reflex is in Hosting Alpha! Learn more here. ✨
DocsBlogGallery

Search documentation...

/

Star

13k+

[ Learn ]

[ Concepts ]

[ Reference ]

LineChart


A line chart is a type of chart used to show information that changes over time. Line charts are created by plotting a series of several points and connecting them with a straight line.

For a line chart we must define an rx.recharts.line() component for each set of values we wish to plot. Each rx.recharts.line() component has a data_key which clearly states which variable in our data we are tracking. In this simple example we plot pv and uv as separate lines against the name column which we set as the data_key in rx.recharts.x_axis.

rx.recharts.line_chart(
    rx.recharts.line(
        data_key="pv",
        stroke="#8884d8",
    ),
    rx.recharts.line(
        data_key="uv",
        stroke="#82ca9d",
    ),
    rx.recharts.x_axis(data_key="name"),
    rx.recharts.y_axis(),
    data=data,
)

Chart Features


Our second example uses exactly the same data as our first example, except now we add some extra features to our line graphs. We add a type_ prop to rx.recharts.line to style the lines differently. In addition, we add an rx.recharts.cartesian_grid to get a grid in the background, an rx.recharts.legend to give us a legend for our graphs and an rx.recharts.graphing_tooltip to add a box with text that appears when you pause the mouse pointer on an element in the graph.

rx.recharts.line_chart(
    rx.recharts.line(
        data_key="pv",
        type_="monotone",
        stroke="#8884d8",
    ),
    rx.recharts.line(
        data_key="uv",
        type_="monotone",
        stroke="#82ca9d",
    ),
    rx.recharts.x_axis(data_key="name"),
    rx.recharts.y_axis(),
    rx.recharts.cartesian_grid(stroke_dasharray="3 3"),
    rx.recharts.graphing_tooltip(),
    rx.recharts.legend(),
    data=data,
)

Dynamic Data and Style


Chart data can be modified by tying the data prop to a State var. Most other props, such as type_, can be controlled dynamically as well. In the following example the "Munge Data" button can be used to randomly modify the data, and the two select elements change the line type_. Since the data and style is saved in the per-browser-tab State, the changes will not be visible to other visitors.

rx.vstack(
    rx.recharts.line_chart(
        rx.recharts.line(
            data_key="pv",
            type_=LineChartState.pv_type,
            stroke="#8884d8",
        ),
        rx.recharts.line(
            data_key="uv",
            type_=LineChartState.uv_type,
            stroke="#82ca9d",
        ),
        rx.recharts.x_axis(data_key="name"),
        rx.recharts.y_axis(),
        data=LineChartState.data,
    ),
    rx.button(
        "Munge Data", on_click=LineChartState.munge_data
    ),
    rx.select(
        [
            "monotone",
            "linear",
            "step",
            "stepBefore",
            "stepAfter",
        ],
        value=LineChartState.pv_type,
        on_change=LineChartState.set_pv_type,
    ),
    rx.select(
        [
            "monotone",
            "linear",
            "step",
            "stepBefore",
            "stepAfter",
        ],
        value=LineChartState.uv_type,
        on_change=LineChartState.set_uv_type,
    ),
    height="15em",
    width="100%",
)

LineChart


A Line chart component in Recharts.


  • Base Event Triggers

Line


A Line component in Recharts.


  • Base Event Triggers

← FunnelPie →

Copyright © 2023 Pynecone, Inc.