
:orphan:
:html_theme.sidebar_secondary.remove:

.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_line_chart_with_cumsum:


Line Chart with Cumulative Sum
------------------------------
This chart creates a simple line chart from the cumulative sum of a fields.


.. altair-plot::
    :remove-code:
    


    import altair as alt
    from altair.datasets import data

    source = data.wheat()

    alt.Chart(source).mark_line().transform_window(
        # Sort the data chronologically
        sort=[{'field': 'year'}],
        # Include all previous records before the current record and none after
        # (This is the default value so you could skip it and it would still work.)
        frame=[None, 0],
        # What to add up as you go
        cumulative_wheat='sum(wheat)'
    ).encode(
        x='year:O',
        # Plot the calculated field created by the transformation
        y='cumulative_wheat:Q'
    ).properties(width=600)

.. tab-set::

    .. tab-item:: Method syntax
        :sync: method

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.wheat()

            alt.Chart(source, width=600).mark_line().transform_window(
                # Sort the data chronologically
                sort=[{'field': 'year'}],
                # Include all previous records before the current record and none after
                # (This is the default value so you could skip it and it would still work.)
                frame=[None, 0],
                # What to add up as you go
                cumulative_wheat='sum(wheat)'
            ).encode(
                x='year:O',
                # Plot the calculated field created by the transformation
                y='cumulative_wheat:Q'
            )


    .. tab-item:: Attribute syntax
        :sync: attribute

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.wheat()

            alt.Chart(source).mark_line().transform_window(
                # Sort the data chronologically
                sort=[{'field': 'year'}],
                # Include all previous records before the current record and none after
                # (This is the default value so you could skip it and it would still work.)
                frame=[None, 0],
                # What to add up as you go
                cumulative_wheat='sum(wheat)'
            ).encode(
                x='year:O',
                # Plot the calculated field created by the transformation
                y='cumulative_wheat:Q'
            ).properties(width=600)