From 13b83e4efdd989693a1dfc99c1eab4f62fbb345a Mon Sep 17 00:00:00 2001 From: 1kamma Date: Wed, 18 Oct 2023 01:27:16 +0300 Subject: [PATCH] add quantety --- ratios.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ratios.py b/ratios.py index bd4033a..dea67c3 100644 --- a/ratios.py +++ b/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__":