带有累计求和的折线图#
此图表通过对字段进行累计求和来创建简单的折线图。
import altair as alt
from vega_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'
)
import altair as alt
from vega_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)