Skip to content

Commit

Permalink
enh: add overview plot to depots
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Oct 7, 2024
1 parent 1ba8673 commit 9bad49d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
13 changes: 8 additions & 5 deletions rssched/app/pages/04_depots.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from typing import List

import pandas as pd
import streamlit as st

from rssched.app.utils.io import get_uploaded_data
from rssched.app.utils.plot import plot_depots_bar_chart
from rssched.app.utils.transform import flatten_depots
from rssched.visualization.depot_loads import plot_depot_vehicle_loads

Expand All @@ -17,11 +15,16 @@


with tabs[0]:
st.plotly_chart(plot_depots_bar_chart(df_depots))

show_aggregated = st.checkbox("Aggregate", value=True)

if show_aggregated:
df_grouped = df_depots.groupby("depot_id", as_index=False).agg(
{"vehicles": "sum", "capacity": "mean"}
df_grouped = (
df_depots.groupby("depot_id", as_index=False)
.agg({"vehicles": "sum", "capacity": "mean"})
.sort_values(by="vehicles")
.reset_index(drop=True)
)
st.dataframe(df_grouped, hide_index=True)
else:
Expand Down
48 changes: 48 additions & 0 deletions rssched/app/utils/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pandas as pd
import plotly.graph_objects as go
from plotly.graph_objs import Figure


def plot_depots_bar_chart(df_depots: pd.DataFrame) -> Figure:
# Group by depot_id to calculate the total capacity and sort by the number of vehicles
df_depots_grouped = (
df_depots[df_depots["vehicles"] > 0]
.groupby("depot_id", as_index=False)
.agg({"vehicles": "sum", "capacity": "max"})
.sort_values(by="vehicles", ascending=False)
)

# Create a bar chart
fig = go.Figure()

# Add bar for the number of vehicles
fig.add_trace(
go.Bar(
x=df_depots_grouped["depot_id"],
y=df_depots_grouped["vehicles"],
name="Vehicles",
marker_color="blue", # Color for vehicles bar
)
)

# Add bar for the depot capacity
fig.add_trace(
go.Bar(
x=df_depots_grouped["depot_id"],
y=df_depots_grouped["capacity"],
name="Capacity",
marker_color="gray", # Color for capacity bar
)
)

# Update layout of the bar chart
fig.update_layout(
barmode="group", # Bars grouped side by side
title="Depot Vehicles and Capacity",
xaxis_title="Depot ID",
# yaxis_title="Count",
legend_title="Legend",
)

# Return the figure object
return fig
4 changes: 3 additions & 1 deletion rssched/app/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def flatten_depots(request: Request, response: Response) -> pd.DataFrame:
df_request, df_response, on=["depot_id", "vehicle_type"], how="outer"
)
df_combined["vehicles"] = df_combined["vehicles"].fillna(0).astype(int)
return df_combined
return df_combined.sort_values(by="vehicles", ascending=False).reset_index(
drop=True
)


def _flatten_response_depot_loads(
Expand Down

0 comments on commit 9bad49d

Please sign in to comment.