Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -2
src/streamlit_app.py
CHANGED
|
@@ -70,8 +70,6 @@ st.dataframe(top_crimes) # should show “Crime Type” and “Count” with co
|
|
| 70 |
total = top_crimes["Count"].sum()
|
| 71 |
top_crimes["Percentage"] = top_crimes["Count"] / total
|
| 72 |
top_crimes["Count"] = pd.to_numeric(top_crimes["Count"], errors="coerce")
|
| 73 |
-
st.write(total)
|
| 74 |
-
st.write(top_crimes)
|
| 75 |
|
| 76 |
# And finally your pie‐chart…
|
| 77 |
chart1 = (
|
|
@@ -89,4 +87,36 @@ chart1 = (
|
|
| 89 |
.properties(width=400, height=400, title="Top 10 Crime Types")
|
| 90 |
)
|
| 91 |
st.altair_chart(chart1, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
total = top_crimes["Count"].sum()
|
| 71 |
top_crimes["Percentage"] = top_crimes["Count"] / total
|
| 72 |
top_crimes["Count"] = pd.to_numeric(top_crimes["Count"], errors="coerce")
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# And finally your pie‐chart…
|
| 75 |
chart1 = (
|
|
|
|
| 87 |
.properties(width=400, height=400, title="Top 10 Crime Types")
|
| 88 |
)
|
| 89 |
st.altair_chart(chart1, use_container_width=True)
|
| 90 |
+
###########################################################################
|
| 91 |
+
# 1. Base pie chart
|
| 92 |
+
base = (
|
| 93 |
+
alt.Chart(top_crimes)
|
| 94 |
+
.mark_arc(innerRadius=50)
|
| 95 |
+
.encode(
|
| 96 |
+
theta=alt.Theta("Count:Q"),
|
| 97 |
+
color=alt.Color("Crime Type:N", legend=alt.Legend(title="Crime Type")),
|
| 98 |
+
tooltip=[
|
| 99 |
+
alt.Tooltip("Crime Type:N", title="Crime Type"),
|
| 100 |
+
alt.Tooltip("Count:Q", title="Count"),
|
| 101 |
+
alt.Tooltip("Percentage:Q", format=".1%")
|
| 102 |
+
]
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# 2. Add labels at mid-radius
|
| 107 |
+
labels = (
|
| 108 |
+
alt.Chart(top_crimes)
|
| 109 |
+
.mark_text(radius=90, size=12, color="white")
|
| 110 |
+
.encode(
|
| 111 |
+
theta=alt.Theta("Count:Q"),
|
| 112 |
+
text=alt.Text("Percentage:Q", format=".1%")
|
| 113 |
+
)
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# 3. Combine and set a fixed, small size
|
| 117 |
+
pie = (base + labels).properties(width=300, height=300, title="Top 10 Crime Types")
|
| 118 |
|
| 119 |
+
# 4. Center in the page using columns
|
| 120 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 121 |
+
with col2:
|
| 122 |
+
st.altair_chart(pie, use_container_width=False)
|