Skip to content

Commit

Permalink
Update: Ordering of groupby chart colors.
Browse files Browse the repository at this point in the history
This is surprisingly weird. The official documentation actually
mentions a issue post that advises referencing an undocumented
internal variable from vega-lite.
Docs:
https://altair-viz.github.io/user_guide/encodings/channels.html#order
The issue referenced:
vega/altair#245 (comment)
I'm not quite sure how the alt.Order object works. But this
implementation works for now. I would like to carefully verify
or test this though.
  • Loading branch information
andrewKOwong committed May 4, 2023
1 parent 624a9e9 commit 10605f5
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def main(debug=False, log_file_path: str | None = None):
})

if not plot_weighted:
y = alt.Y(f"count({selected_var})", title='Count')
y = alt.Y(f"count({selected_var}):Q", title='Count')
else:
y = alt.Y(f'sum({WEIGHT_KEY})', title='Weighted Count')
y = alt.Y(f'sum({WEIGHT_KEY}):Q', title='Weighted Count')

x = alt.X(f"{selected_var}",
type='ordinal',
Expand All @@ -165,7 +165,8 @@ def main(debug=False, log_file_path: str | None = None):
color=alt.Color(
f"{groupby_var}:O",
title=GROUPBY_VARS[groupby_var],
sort=groupby_order)
sort=alt.Sort(groupby_order)),
order=alt.Order(f'color_{groupby_var}_sort_index:Q', sort='ascending')
)

st.altair_chart(chart, use_container_width=True)
Expand Down

0 comments on commit 10605f5

Please sign in to comment.