Skip to content

Commit

Permalink
add more aggregators on timeseries chart
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv-chauhan committed Aug 2, 2022
1 parent 9c95409 commit 7d124b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
18 changes: 11 additions & 7 deletions network-layer-one/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ def build_grid_options(show_quantitative_columns, show_stats):
['Bar chart', 'Line chart', 'Area chart']
)

if 'with_mean' not in st.session_state:
st.session_state.with_mean = True
aggregate = st.radio(
"Aggregate on",
('none', 'count', 'sum', 'mean', 'median', 'min', 'max'),
index=3,
horizontal=True
)
if aggregate == 'none':
aggregate = None

with_mean = st.checkbox('With mean', value=True)
st.session_state.with_mean = with_mean
with col2:
metric_on_y = st.selectbox(
'Metric on y-axis',
Expand All @@ -216,12 +220,12 @@ def build_grid_options(show_quantitative_columns, show_stats):

if chart_type == "Line chart":
charts.plot_line(quantitative_df[['network', metric_on_y,
metric_on_x]], metric_on_y, metric_on_x, with_mean)
metric_on_x]], metric_on_y, metric_on_x, aggregate)
elif chart_type == "Bar chart":
charts.plot_bar(quantitative_df[['network', metric_on_y,
metric_on_x]], metric_on_y, metric_on_x, with_mean)
metric_on_x]], metric_on_y, metric_on_x, aggregate)
elif chart_type == "Area chart":
charts.plot_area(quantitative_df[['network', metric_on_y,
metric_on_x]], metric_on_y, metric_on_x, with_mean)
metric_on_x]], metric_on_y, metric_on_x, aggregate)

st.markdown('---')
32 changes: 16 additions & 16 deletions network-layer-one/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import altair as alt


def plot_mean(data, on_y):
mean = alt.Chart(data).mark_rule().encode(
y=alt.Y(on_y, aggregate='mean'),
def plot_aggregate(aggregate, data, on_y):
rule = alt.Chart(data).mark_rule().encode(
y=alt.Y(on_y, aggregate=aggregate),
color='network',
size=alt.value(2)
)
return mean
return rule


def plot_line(data, on_y, on_x, with_mean):
def plot_line(data, on_y, on_x, aggregate):
line = alt.Chart(data, title=f"{on_y} vs. {on_x}").mark_line().encode(
x=alt.X(on_x, title=on_x),
y=alt.Y(on_y, title=on_y),
color='network',
tooltip=[on_y, on_x]).interactive()

if with_mean:
mean = plot_mean(data, on_y)
st.altair_chart((line + mean), use_container_width=True)
if aggregate:
rule = plot_aggregate(aggregate, data, on_y)
st.altair_chart((line + rule), use_container_width=True)
return

st.altair_chart(line, use_container_width=True)


def plot_bar(data, on_y, on_x, with_mean):
def plot_bar(data, on_y, on_x, aggregate):
bar = alt.Chart(data, title=f"{on_y} vs. {on_x}").mark_bar(
cornerRadiusTopLeft=3,
cornerRadiusTopRight=3,
Expand All @@ -36,25 +36,25 @@ def plot_bar(data, on_y, on_x, with_mean):
color='network',
tooltip=[on_y, on_x])

if with_mean:
mean = plot_mean(data, on_y)
st.altair_chart((bar + mean), use_container_width=True)
if aggregate:
rule = plot_aggregate(aggregate, data, on_y)
st.altair_chart((bar + rule), use_container_width=True)
return

st.altair_chart(bar, use_container_width=True)


def plot_area(data, on_y, on_x, with_mean):
def plot_area(data, on_y, on_x, aggregate):
area = alt.Chart(data, title=f"{on_y} vs. {on_x}").mark_area(
opacity=0.5).encode(
x=alt.X(on_x, title=on_x),
y=alt.Y(on_y, title=on_y, stack=None),
color='network',
tooltip=[on_y, on_x])

if with_mean:
mean = plot_mean(data, on_y)
st.altair_chart((area + mean), use_container_width=True)
if aggregate:
rule = plot_aggregate(aggregate, data, on_y)
st.altair_chart((area + rule), use_container_width=True)
return

st.altair_chart(area, use_container_width=True)

0 comments on commit 7d124b4

Please sign in to comment.