dropdown selection, filters the places

This commit is contained in:
2023-10-18 01:23:52 +03:00
parent c526e00039
commit 97f7c410d3

View File

@@ -1,7 +1,6 @@
import pandas as pd import pandas as pd
import dash import dash
import dash_core_components as dcc from dash import dcc, html
import dash_html_components as html
from dash.dependencies import Input, Output from dash.dependencies import Input, Output
import plotly.graph_objects as go import plotly.graph_objects as go
@@ -15,49 +14,38 @@ app = dash.Dash(__name__)
# Define the columns to summarize # Define the columns to summarize
columns_to_summarize = ["ancient", "old", "middle", "new", "late"] columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
# Create the figure # Create the figure for the initial graph (placeholder data)
fig = go.Figure() initial_data = df.groupby("place")[columns_to_summarize].sum().reset_index()
initial_fig = go.Figure(data=[
# Add an initial trace for demonstration go.Bar(name=col, x=initial_data["place"], y=initial_data[col]) for col in columns_to_summarize
fig.add_trace(go.Bar(x=["Place 1", "Place 2", "Place 3"], ])
y=[5, 7, 4], name="ancient"))
# Define the app layout # Define the app layout
app.layout = html.Div([ app.layout = html.Div([
html.H1("Interactive Bar Graph with Filtering"), html.H1("Dynamic Stacked Bar Graph"),
dcc.Checklist( dcc.Dropdown(
id="place-selector", id="place-selector",
options=[{"label": place, "value": place} for place in df["place"].unique()], options=[{"label": place, "value": place} for place in df["place"].unique()],
value=df["place"].unique().tolist(), multi=True,
labelStyle={'display': 'block'} placeholder="Select Places",
), ),
dcc.Checklist( dcc.Graph(id="bar-graph", figure=initial_fig),
id="column-selector",
options=[{"label": col, "value": col} for col in columns_to_summarize],
value=columns_to_summarize,
labelStyle={'display': 'block'}
),
dcc.Graph(id="bar-graph", figure=fig)
]) ])
# Define callback functions to update the graph # Define a callback to update the graph based on selected places
@app.callback( @app.callback(
Output("bar-graph", "figure"), Output("bar-graph", "figure"),
[Input("place-selector", "value"), Input("column-selector", "value")] [Input("place-selector", "value")]
) )
def update_graph(selected_places, selected_columns): def update_graph(selected_places):
filtered_df = df[(df["place"].isin(selected_places)) & (df[selected_columns].sum(axis=1) > 0)] filtered_df = df[df["place"].isin(selected_places)]
grouped = filtered_df.groupby("place")[selected_columns].sum().reset_index() grouped = filtered_df.groupby("place")[columns_to_summarize].sum().reset_index()
fig = go.Figure()
for col in selected_columns: fig = go.Figure(data=[
fig.add_trace(go.Bar(x=grouped["place"], y=grouped[col], name=col, text=grouped[col], textposition='auto')) go.Bar(name=col, x=grouped["place"], y=grouped[col]) for col in columns_to_summarize
fig.update_layout( ])
title="Summary of Categories by Place with Selection", fig.update_layout(barmode="stack") # Set the bars to be stacked
xaxis_title="Place",
yaxis_title="Total Count",
xaxis=dict(categoryorder='total descending'),
barmode='stack'
)
return fig return fig
if __name__ == "__main__": if __name__ == "__main__":