forked from banjtheman/defundthepolice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
54 lines (46 loc) · 1.8 KB
/
viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import altair as alt
import streamlit as st
from plotly import graph_objects
def bar_graph(df):
st.info(
"Select which columns to display on the bar chart below which displays percent of budget"
)
selectd_cols = st.multiselect("Select columns", list(df["item"]), list(df["item"]))
df = df.loc[df["item"].isin(selectd_cols)]
# x_col = st.selectbox("Select x axis for bar chart", df.columns)
# xcol_string=x_col+":O"
# if st.checkbox("Show as continuous?",key="bar_chart_x_is_cont"):
x_col = "percent"
xcol_string = x_col + ":Q"
y_col = "item"
z_col = "percent"
# y_col = st.selectbox("Select y axis for bar chart", df.columns)
# z_col = st.selectbox("Select z axis for bar chart", df.columns)
chart = (
alt.Chart(df)
.mark_bar()
.encode(x=xcol_string, y=y_col, color=z_col, tooltip=list(df.columns))
# .interactive()
# .properties(title="Defund The Police")
.configure_title(
fontSize=20,
)
.configure_axis(labelFontSize=10, titleFontSize=10)
.configure_legend(labelFontSize=10, titleFontSize=10)
)
st.altair_chart(chart, use_container_width=True)
# TODO figure out saving images
# chart.save('chart.png')
return chart
def pie_chart(data):
# chart = altair.Chart(data, height=500).transform_calculate(
# percent_adjusted="datum.percent / 100"
# ).mark_bar().encode(
# altair.X("item:O"),
# altair.Y("percent_adjusted:Q", axis=altair.Axis(format="%")),
# )
# st.altair_chart(chart, use_container_width=True)
labels = list(data.get("item", []))
values = list(data.get("percent", []))
fig = graph_objects.Figure(data=[graph_objects.Pie(labels=labels, values=values)])
st.plotly_chart(fig, use_container_width=True)