|
1 | | -import logging |
2 | | - |
3 | 1 | import dash_bootstrap_components as dbc |
| 2 | +import pandas as pd |
4 | 3 | import plotly.express as px |
5 | 4 | from dash import dash_table, dcc, html |
6 | 5 | from dash.dependencies import Input, Output |
|
12 | 11 | unique_countries = sorted(clean_livelihood_data["country_code"].dropna().unique()) |
13 | 12 | unique_zones = sorted(clean_livelihood_data["livelihood_zone_baseline_label"].dropna().unique()) |
14 | 13 |
|
15 | | -logger = logging.getLogger(__name__) |
16 | | - |
17 | 14 | # Dash app |
18 | 15 | app = DjangoDash("Inventory_dashboard", suppress_callback_exceptions=True, external_stylesheets=[dbc.themes.BOOTSTRAP]) |
19 | 16 | app.title = "HEA Dashboard" |
|
29 | 26 | dcc.Dropdown( |
30 | 27 | id="country-dropdown", |
31 | 28 | options=[{"label": country, "value": country} for country in unique_countries], |
32 | | - placeholder="Select Country(s)", |
| 29 | + placeholder=f"Select Country(s) (e.g., {unique_countries[0]})", |
33 | 30 | multi=True, |
34 | 31 | ), |
35 | 32 | ], |
36 | | - style={"flex": "1", "marginRight": "10px"}, # Set flex and spacing |
| 33 | + style={"flex": "1", "marginRight": "10px"}, |
37 | 34 | ), |
38 | 35 | html.Div( |
39 | 36 | [ |
40 | 37 | dcc.Dropdown( |
41 | 38 | id="livelihood-zone-dropdown", |
42 | 39 | options=[{"label": zone, "value": zone} for zone in unique_zones], |
43 | | - placeholder="Select Livelihood Zone(s)", |
| 40 | + placeholder=f"Select Livelihood Zone(s) (e.g., {unique_zones[0]})", |
44 | 41 | multi=True, |
45 | 42 | ), |
46 | 43 | ], |
|
77 | 74 | id="data-table", |
78 | 75 | columns=[ |
79 | 76 | {"name": "Livelihood Zone", "id": "livelihood_zone_baseline_label"}, |
80 | | - {"name": "Strategy Type Count", "id": "Count"}, |
81 | | - {"name": "Wealth Characteristics Count", "id": "Wealth Characteristics Count"}, |
| 77 | + {"name": "Total Strategy Type Count", "id": "Strategy Type Count"}, |
| 78 | + {"name": "Summary Wealth Characteristics", "id": "Summary Data"}, |
| 79 | + {"name": "Community Wealth Characteristics", "id": "Community Data"}, |
82 | 80 | ], |
83 | 81 | style_table={"overflowX": "auto"}, |
84 | | - style_cell={"textAlign": "left"}, |
85 | | - page_size=12, |
| 82 | + style_cell={ |
| 83 | + "textAlign": "left", |
| 84 | + "fontSize": "15px", # Increase font size |
| 85 | + "padding": "10px", # Add padding |
| 86 | + }, |
| 87 | + style_header={ |
| 88 | + "fontSize": "18px", # Increase font size for header |
| 89 | + "fontWeight": "bold", # Make header bold |
| 90 | + "textAlign": "center", |
| 91 | + }, |
| 92 | + page_size=10, |
86 | 93 | ), |
87 | 94 | ], |
88 | 95 | className="inventory-filter inventory-filter-last", |
@@ -124,63 +131,74 @@ def update_charts(selected_countries, selected_zones): |
124 | 131 | ] |
125 | 132 | filtered_wealth = filtered_wealth[filtered_wealth["livelihood_zone_baseline_label"].isin(selected_zones)] |
126 | 133 |
|
127 | | - # Group data for charts |
| 134 | + # Group and aggregate Strategy Type Count |
128 | 135 | livelihood_grouped = ( |
129 | 136 | filtered_livelihood.groupby(["livelihood_zone_baseline_label", "strategy_type_label"]) |
130 | 137 | .size() |
131 | | - .reset_index(name="Count") |
| 138 | + .reset_index(name="Strategy Type Count") |
132 | 139 | ) |
133 | | - wealth_grouped = ( |
134 | | - filtered_wealth.groupby("livelihood_zone_baseline_label") |
135 | | - .size() |
136 | | - .reset_index(name="Wealth Characteristics Count") |
| 140 | + livelihood_summary = ( |
| 141 | + livelihood_grouped.groupby("livelihood_zone_baseline_label")["Strategy Type Count"].sum().reset_index() |
137 | 142 | ) |
138 | | - wealth_monthly_grouped = ( |
139 | | - filtered_wealth.groupby(["created_month", "livelihood_zone_baseline_label"]) |
| 143 | + |
| 144 | + # Group and pivot Wealth Characteristics Count |
| 145 | + wealth_grouped = ( |
| 146 | + filtered_wealth.groupby(["livelihood_zone_baseline_label", "Record Type"]) |
140 | 147 | .size() |
141 | 148 | .reset_index(name="Wealth Characteristics Count") |
142 | 149 | ) |
143 | | - |
| 150 | + wealth_grouped_pivot = wealth_grouped.pivot_table( |
| 151 | + index="livelihood_zone_baseline_label", |
| 152 | + columns="Record Type", |
| 153 | + values="Wealth Characteristics Count", |
| 154 | + aggfunc="sum", |
| 155 | + fill_value=0, |
| 156 | + ).reset_index() |
| 157 | + |
| 158 | + # Merge livelihood and wealth data |
| 159 | + merged_data = pd.merge(livelihood_summary, wealth_grouped_pivot, on="livelihood_zone_baseline_label", how="left") |
| 160 | + |
| 161 | + # Create charts |
144 | 162 | wealth_fig = px.bar( |
145 | 163 | wealth_grouped, |
146 | 164 | x="livelihood_zone_baseline_label", |
147 | 165 | y="Wealth Characteristics Count", |
148 | | - title="Wealth Characteristics per Baseline", |
| 166 | + color="Record Type", |
| 167 | + barmode="stack", |
| 168 | + title="Wealth Characteristics by Type", |
149 | 169 | labels={ |
150 | | - "livelihood_zone_baseline_label": "Baseline", |
151 | | - "Wealth Characteristics Count": "No. of Wealth Characteristics", |
| 170 | + "livelihood_zone_baseline_label": "Livelihood Zone", |
152 | 171 | }, |
153 | 172 | ) |
154 | 173 |
|
155 | 174 | livelihood_fig = px.bar( |
156 | 175 | livelihood_grouped, |
157 | 176 | x="strategy_type_label", |
158 | | - y="Count", |
| 177 | + y="Strategy Type Count", |
159 | 178 | color="livelihood_zone_baseline_label", |
160 | | - title="Livelihood Strategies per Baseline", |
| 179 | + barmode="stack", |
| 180 | + title="Strategy Types by Baseline", |
161 | 181 | labels={ |
162 | 182 | "strategy_type_label": "Strategy Type", |
163 | | - "Count": "No. of Livelihood Strategies", |
164 | | - "livelihood_zone_baseline_label": "Baseline", |
| 183 | + "livelihood_zone_baseline_label": "Baseline Zone", |
165 | 184 | }, |
166 | 185 | ) |
167 | 186 |
|
168 | | - # Stacked/multiple column chart for wealth characteristics by month |
169 | 187 | wealth_monthly_fig = px.bar( |
170 | | - wealth_monthly_grouped, |
| 188 | + filtered_wealth.groupby(["created_month", "Record Type"]) |
| 189 | + .size() |
| 190 | + .reset_index(name="Wealth Characteristics Count"), |
171 | 191 | x="created_month", |
172 | 192 | y="Wealth Characteristics Count", |
173 | | - color="livelihood_zone_baseline_label", |
174 | | - barmode="stack", # Use 'group' for multiple column chart |
175 | | - title="Wealth Characteristics by Month and Baseline", |
| 193 | + color="Record Type", |
| 194 | + barmode="stack", |
| 195 | + title="Wealth Characteristics by Month", |
176 | 196 | labels={ |
177 | 197 | "created_month": "Month", |
178 | | - "Wealth Characteristics Count": "No. of Wealth Characteristics", |
179 | | - "livelihood_zone_baseline_label": "Baseline", |
180 | 198 | }, |
181 | 199 | ) |
182 | 200 |
|
183 | | - return zone_options, wealth_fig, livelihood_fig, wealth_monthly_fig, livelihood_grouped.to_dict("records") |
| 201 | + return zone_options, wealth_fig, livelihood_fig, wealth_monthly_fig, merged_data.to_dict("records") |
184 | 202 |
|
185 | 203 |
|
186 | 204 | # Run the app |
|
0 commit comments