1- from django .db .models import Sum
1+ from django .db .models import Sum , ExpressionWrapper , FloatField
22from django .utils .translation import gettext as _
33
44import pandas as pd
55import plotly .express as px
6- from core .constants import DAY_MILLISECONDS
6+ from core .constants import DAY_MILLISECONDS , HOUR_MINUTES
77from homes .models import Home
88
99from homes .queries import (
@@ -76,7 +76,12 @@ def prepare_work_by_type_chart(home: Home) -> str:
7676 work_by_type = list (
7777 home .work_performed .values ("type__name" )
7878 .order_by ("type__name" )
79- .annotate (total_hours = Sum ("duration_hours" )),
79+ .annotate (
80+ total_hours = ExpressionWrapper (
81+ Sum ("duration_minutes" ) / HOUR_MINUTES ,
82+ output_field = FloatField (),
83+ ),
84+ ),
8085 )
8186
8287 work_by_type_chart = px .bar (
@@ -88,16 +93,30 @@ def prepare_work_by_type_chart(home: Home) -> str:
8893 "type__name" : _ ("Type of work" ),
8994 "total_hours" : _ ("Total hours" ),
9095 },
91- ).to_html ()
92- return work_by_type_chart
96+ template = "plotly_dark" ,
97+ )
98+
99+ # Set plot background/paper color to transparent
100+ work_by_type_chart .update_layout (
101+ plot_bgcolor = "rgba(0, 0, 0, 0)" ,
102+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
103+ font_color = "#FFFFFF" ,
104+ )
105+
106+ return work_by_type_chart .to_html ()
93107
94108
95109def prepare_work_by_caregiver_role_chart (home : Home ) -> str :
96110 """Prepare the work hours by caregiver role chart."""
97111 work_by_caregiver_role = list (
98112 home .work_performed .values ("caregiver_role__name" )
99113 .order_by ("caregiver_role__name" )
100- .annotate (total_hours = Sum ("duration_hours" )),
114+ .annotate (
115+ total_hours = ExpressionWrapper (
116+ Sum ("duration_minutes" ) / HOUR_MINUTES ,
117+ output_field = FloatField (),
118+ ),
119+ ),
101120 )
102121
103122 work_by_caregiver_role_chart = px .bar (
@@ -109,9 +128,17 @@ def prepare_work_by_caregiver_role_chart(home: Home) -> str:
109128 "caregiver_role__name" : _ ("Caregiver role" ),
110129 "total_hours" : _ ("Total hours" ),
111130 },
112- ).to_html ()
131+ template = "plotly_dark" ,
132+ )
113133
114- return work_by_caregiver_role_chart
134+ # Set plot background/paper color to transparent
135+ work_by_caregiver_role_chart .update_layout (
136+ plot_bgcolor = "rgba(0, 0, 0, 0)" ,
137+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
138+ font_color = "#FFFFFF" ,
139+ )
140+
141+ return work_by_caregiver_role_chart .to_html ()
115142
116143
117144def prepare_daily_work_percent_by_caregiver_role_and_type_chart (home : Home ) -> str :
@@ -134,6 +161,7 @@ def prepare_daily_work_percent_by_caregiver_role_and_type_chart(home: Home) -> s
134161 },
135162 # Add numeric text on bars
136163 text_auto = True ,
164+ template = "plotly_dark" ,
137165 )
138166
139167 # Format y-axis as percentages
@@ -149,6 +177,19 @@ def prepare_daily_work_percent_by_caregiver_role_and_type_chart(home: Home) -> s
149177 width = DAY_MILLISECONDS ,
150178 )
151179
180+ # Set plot background/paper color to transparent
181+ daily_work_percent_by_caregiver_role_and_type_chart .update_layout (
182+ plot_bgcolor = "rgba(0, 0, 0, 0)" ,
183+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
184+ font_color = "#FFFFFF" ,
185+ )
186+
187+ # Remove individual y-axis labels and add a single global one
188+ daily_work_percent_by_caregiver_role_and_type_chart .update_yaxes (title_text = "" )
189+ daily_work_percent_by_caregiver_role_and_type_chart .update_layout (
190+ yaxis_title = _ ("Work percent" ),
191+ )
192+
152193 return daily_work_percent_by_caregiver_role_and_type_chart .to_html ()
153194
154195
@@ -169,6 +210,7 @@ def prepare_home_work_percent_by_caregiver_role_chart(home: Home) -> str:
169210 "home_name" : "" ,
170211 },
171212 text_auto = True ,
213+ template = "plotly_dark" ,
172214 )
173215
174216 home_work_percent_by_caregiver_role_chart .update_layout (
@@ -181,7 +223,9 @@ def prepare_home_work_percent_by_caregiver_role_chart(home: Home) -> str:
181223 "pad" : 0 ,
182224 },
183225 plot_bgcolor = "rgba(0, 0, 0, 0)" ,
226+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
184227 showlegend = False ,
228+ font_color = "#FFFFFF" ,
185229 xaxis = {
186230 "tickformat" : ",.0%" ,
187231 },
@@ -213,9 +257,17 @@ def prepare_work_percent_by_caregiver_role_and_type_chart(
213257 "work_type" : _ ("Type of work" ),
214258 },
215259 text_auto = True ,
260+ template = "plotly_dark" ,
216261 )
217262 work_percent_by_caregiver_role_and_type_chart .layout .yaxis .tickformat = ",.0%"
218263
264+ # Set plot background/paper color to transparent
265+ work_percent_by_caregiver_role_and_type_chart .update_layout (
266+ plot_bgcolor = "rgba(0, 0, 0, 0)" ,
267+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
268+ font_color = "#FFFFFF" ,
269+ )
270+
219271 return work_percent_by_caregiver_role_and_type_chart .to_html ()
220272
221273
@@ -234,6 +286,14 @@ def prepare_work_by_caregiver_role_and_type_chart(
234286 "total_hours" : _ ("Total hours" ),
235287 "work_type" : _ ("Type of work" ),
236288 },
289+ template = "plotly_dark" ,
290+ )
291+
292+ # Set plot background/paper color to transparent
293+ work_by_caregiver_role_and_type_chart .update_layout (
294+ plot_bgcolor = "rgba(0, 0, 0, 0)" ,
295+ paper_bgcolor = "rgba(0, 0, 0, 0)" ,
296+ font_color = "#FFFFFF" ,
237297 )
238298
239299 return work_by_caregiver_role_and_type_chart .to_html ()
0 commit comments