forked from saret/DHGeography
add quantety
This commit is contained in:
21
ratios.py
21
ratios.py
@@ -16,13 +16,14 @@ columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
|
||||
|
||||
# Create the figure for the initial graph (placeholder data)
|
||||
initial_data = df.groupby("place")[columns_to_summarize].sum().reset_index()
|
||||
initial_fig = go.Figure(data=[
|
||||
go.Bar(name=col, x=initial_data["place"], y=initial_data[col]) for col in columns_to_summarize
|
||||
])
|
||||
initial_fig = go.Figure()
|
||||
|
||||
for col in columns_to_summarize:
|
||||
initial_fig.add_trace(go.Bar(name=col, x=initial_data["place"], y=initial_data[col], text=initial_data[col], textposition="auto"))
|
||||
|
||||
# Define the app layout
|
||||
app.layout = html.Div([
|
||||
html.H1("Dynamic Stacked Bar Graph"),
|
||||
html.H1("Dynamic Stacked Bar Graph with Labels"),
|
||||
dcc.Dropdown(
|
||||
id="place-selector",
|
||||
options=[{"label": place, "value": place} for place in df["place"].unique()],
|
||||
@@ -41,11 +42,17 @@ def update_graph(selected_places):
|
||||
filtered_df = df[df["place"].isin(selected_places)]
|
||||
grouped = filtered_df.groupby("place")[columns_to_summarize].sum().reset_index()
|
||||
|
||||
fig = go.Figure(data=[
|
||||
go.Bar(name=col, x=grouped["place"], y=grouped[col]) for col in columns_to_summarize
|
||||
])
|
||||
fig = go.Figure()
|
||||
|
||||
for col in columns_to_summarize:
|
||||
fig.add_trace(go.Bar(name=col, x=grouped["place"], y=grouped[col], text=grouped[col], textposition="auto"))
|
||||
|
||||
fig.update_layout(barmode="stack") # Set the bars to be stacked
|
||||
|
||||
for index, place in enumerate(grouped["place"]):
|
||||
total = grouped[columns_to_summarize].iloc[index].sum()
|
||||
fig.add_annotation(text=f"Total: {total}", x=place, y=total + 5)
|
||||
|
||||
return fig
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user