Skip to content

Commit

Permalink
Dashboard with summary data
Browse files Browse the repository at this point in the history
  • Loading branch information
enjoki committed Jan 21, 2025
1 parent d221710 commit 655795c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
88 changes: 53 additions & 35 deletions apps/viz/dash/inventory_dashboard/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
from dash import dash_table, dcc, html
from dash.dependencies import Input, Output
Expand All @@ -12,8 +11,6 @@
unique_countries = sorted(clean_livelihood_data["country_code"].dropna().unique())
unique_zones = sorted(clean_livelihood_data["livelihood_zone_baseline_label"].dropna().unique())

logger = logging.getLogger(__name__)

# Dash app
app = DjangoDash("Inventory_dashboard", suppress_callback_exceptions=True, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = "HEA Dashboard"
Expand All @@ -29,18 +26,18 @@
dcc.Dropdown(
id="country-dropdown",
options=[{"label": country, "value": country} for country in unique_countries],
placeholder="Select Country(s)",
placeholder=f"Select Country(s) (e.g., {unique_countries[0]})",
multi=True,
),
],
style={"flex": "1", "marginRight": "10px"}, # Set flex and spacing
style={"flex": "1", "marginRight": "10px"},
),
html.Div(
[
dcc.Dropdown(
id="livelihood-zone-dropdown",
options=[{"label": zone, "value": zone} for zone in unique_zones],
placeholder="Select Livelihood Zone(s)",
placeholder=f"Select Livelihood Zone(s) (e.g., {unique_zones[0]})",
multi=True,
),
],
Expand Down Expand Up @@ -77,12 +74,22 @@
id="data-table",
columns=[
{"name": "Livelihood Zone", "id": "livelihood_zone_baseline_label"},
{"name": "Strategy Type Count", "id": "Count"},
{"name": "Wealth Characteristics Count", "id": "Wealth Characteristics Count"},
{"name": "Total Strategy Type Count", "id": "Strategy Type Count"},
{"name": "Summary Wealth Characteristics", "id": "Summary Data"},
{"name": "Community Wealth Characteristics", "id": "Community Data"},
],
style_table={"overflowX": "auto"},
style_cell={"textAlign": "left"},
page_size=12,
style_cell={
"textAlign": "left",
"fontSize": "15px", # Increase font size
"padding": "10px", # Add padding
},
style_header={
"fontSize": "18px", # Increase font size for header
"fontWeight": "bold", # Make header bold
"textAlign": "center",
},
page_size=10,
),
],
className="inventory-filter inventory-filter-last",
Expand Down Expand Up @@ -124,63 +131,74 @@ def update_charts(selected_countries, selected_zones):
]
filtered_wealth = filtered_wealth[filtered_wealth["livelihood_zone_baseline_label"].isin(selected_zones)]

# Group data for charts
# Group and aggregate Strategy Type Count
livelihood_grouped = (
filtered_livelihood.groupby(["livelihood_zone_baseline_label", "strategy_type_label"])
.size()
.reset_index(name="Count")
.reset_index(name="Strategy Type Count")
)
wealth_grouped = (
filtered_wealth.groupby("livelihood_zone_baseline_label")
.size()
.reset_index(name="Wealth Characteristics Count")
livelihood_summary = (
livelihood_grouped.groupby("livelihood_zone_baseline_label")["Strategy Type Count"].sum().reset_index()
)
wealth_monthly_grouped = (
filtered_wealth.groupby(["created_month", "livelihood_zone_baseline_label"])

# Group and pivot Wealth Characteristics Count
wealth_grouped = (
filtered_wealth.groupby(["livelihood_zone_baseline_label", "Record Type"])
.size()
.reset_index(name="Wealth Characteristics Count")
)

wealth_grouped_pivot = wealth_grouped.pivot_table(
index="livelihood_zone_baseline_label",
columns="Record Type",
values="Wealth Characteristics Count",
aggfunc="sum",
fill_value=0,
).reset_index()

# Merge livelihood and wealth data
merged_data = pd.merge(livelihood_summary, wealth_grouped_pivot, on="livelihood_zone_baseline_label", how="left")

# Create charts
wealth_fig = px.bar(
wealth_grouped,
x="livelihood_zone_baseline_label",
y="Wealth Characteristics Count",
title="Wealth Characteristics per Baseline",
color="Record Type",
barmode="stack",
title="Wealth Characteristics by Type",
labels={
"livelihood_zone_baseline_label": "Baseline",
"Wealth Characteristics Count": "No. of Wealth Characteristics",
"livelihood_zone_baseline_label": "Livelihood Zone",
},
)

livelihood_fig = px.bar(
livelihood_grouped,
x="strategy_type_label",
y="Count",
y="Strategy Type Count",
color="livelihood_zone_baseline_label",
title="Livelihood Strategies per Baseline",
barmode="stack",
title="Strategy Types by Baseline",
labels={
"strategy_type_label": "Strategy Type",
"Count": "No. of Livelihood Strategies",
"livelihood_zone_baseline_label": "Baseline",
"livelihood_zone_baseline_label": "Baseline Zone",
},
)

# Stacked/multiple column chart for wealth characteristics by month
wealth_monthly_fig = px.bar(
wealth_monthly_grouped,
filtered_wealth.groupby(["created_month", "Record Type"])
.size()
.reset_index(name="Wealth Characteristics Count"),
x="created_month",
y="Wealth Characteristics Count",
color="livelihood_zone_baseline_label",
barmode="stack", # Use 'group' for multiple column chart
title="Wealth Characteristics by Month and Baseline",
color="Record Type",
barmode="stack",
title="Wealth Characteristics by Month",
labels={
"created_month": "Month",
"Wealth Characteristics Count": "No. of Wealth Characteristics",
"livelihood_zone_baseline_label": "Baseline",
},
)

return zone_options, wealth_fig, livelihood_fig, wealth_monthly_fig, livelihood_grouped.to_dict("records")
return zone_options, wealth_fig, livelihood_fig, wealth_monthly_fig, merged_data.to_dict("records")


# Run the app
Expand Down
30 changes: 8 additions & 22 deletions apps/viz/dash/inventory_dashboard/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,27 @@ def prepare_wealth_group_data(df):
"""
Prepare wealth group data for visualization.
"""
# Rename columns for consistency
df.rename(columns={"livelihood_zone_country_code": "country_code"}, inplace=True)

# Extract baseline date from 'livelihood_zone_baseline_label'
if "livelihood_zone_baseline_label" in df.columns:
df["ls_baseline_date"] = df["livelihood_zone_baseline_label"].str.split(": ").str[1]
else:
df["ls_baseline_date"] = None # Assign None if the column is missing
df["ls_baseline_date"] = None

# Convert baseline date to datetime and extract the month
df["ls_baseline_month"] = pd.to_datetime(df["ls_baseline_date"], errors="coerce").dt.month

# Define month mapping dictionary
month_mapping = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December",
}

# Extract 'created_month' from 'created_date' or fallback to 'ls_baseline_date'
month_mapping = {month: pd.Timestamp(f"2023-{month:02}-01").strftime("%B") for month in range(1, 13)}

if "created_date" in df.columns:
df["created_month"] = pd.to_datetime(df["created_date"], errors="coerce").dt.month.map(month_mapping)
else:
print("Warning: 'created_date' column is missing. Using 'ls_baseline_date' instead.")
df["created_month"] = pd.to_datetime(df["ls_baseline_date"], errors="coerce").dt.month.map(month_mapping)

if "community_name" in df.columns:
df["Record Type"] = df["community_name"].apply(lambda x: "Summary Data" if pd.isna(x) else "Community Data")
else:
df["Record Type"] = "Community Data"

return df


Expand Down

0 comments on commit 655795c

Please sign in to comment.