-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_figures.py
348 lines (334 loc) · 10.4 KB
/
make_figures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import numpy as np
import pandas as pd
import dash_core_components as dcc
import dash_table
from dash_table.Format import Format
import dash_table.FormatTemplate as FormatTemplate
import plotly.graph_objects as go
# attendance summary
def make_attendance_table(df):
# check if not enough info
if df.loc[0, 'attendance_category'] == 'Not enough info':
sure_bet_count, at_risk_count, not_met_count, on_track_count = [None] * 4
sure_bet_pct, at_risk_pct, not_met_pct, on_track_pct = [None] * 4
else:
# count children in attendance category
# using shape[0] to count rows and return an int
sure_bet_count = df[df['attendance_category'] == 'Sure bet'].shape[0]
at_risk_count = df[df['attendance_category'] == 'At risk'].shape[0]
not_met_count = df[df['attendance_category'] == 'Not met'].shape[0]
on_track_count = df[df['attendance_category'] == 'On track'].shape[0]
total_count = df.loc[df['attendance_category'] != 'Case expired'].shape[0]
# calculate percentage of children in each category
sure_bet_pct = sure_bet_count / total_count
at_risk_pct = at_risk_count / total_count
not_met_pct = not_met_count / total_count
on_track_pct = on_track_count / total_count
# make table
label_col = [
'Sure bet',
'On track',
'At risk',
'Not met'
]
pct_col = [
sure_bet_pct,
on_track_pct,
at_risk_pct,
not_met_pct
]
count_col = [
sure_bet_count,
on_track_count,
at_risk_count,
not_met_count
]
df_table = pd.DataFrame(
{
'attendance_category': label_col,
'percentage': pct_col,
'count': count_col
}
)
table = dash_table.DataTable(
id='summary',
columns=[
{
'id': 'attendance_category',
'name': 'Attendance risk',
'type': 'text'
}, {
'id': 'percentage',
'name': 'Percentage',
'type': 'numeric',
'format': {'nully':'-%',
'prefix': None,
'specifier': '.0%'}
}, {
'id': 'count',
'name': 'Count',
'type': 'numeric',
'format': Format(
precision=0,
nully='-'
)
}
],
data=df_table.to_dict('records'),
style_as_list_view=True,
style_table={
'overflowX': 'auto',
'padding':'20px'
},
style_data_conditional=[
{
'if': {
'filter_query': '{attendance_category} = "Sure bet"',
'column_id': 'attendance_category'
},
'color': '#0f8c48'
},
{
'if': {
'filter_query': '{attendance_category} = "Not met"',
'column_id': 'attendance_category'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "At risk"',
'column_id': 'attendance_category'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "On track"',
'column_id': 'attendance_category'
},
'color': '#0f8c48'
}
]
)
return table
# revenue barchart
def make_revenue_chart(df):
# sum up revenues
min_revenue_sum = df['min_revenue'].sum()
potential_revenue_sum = df['potential_revenue'].sum()
max_approved_revenue_sum = df['max_revenue'].sum()
potential_e_learning_revenue_sum = df['e_learning_revenue_potential'].sum()
min_potential_delta = potential_revenue_sum - min_revenue_sum
potential_max_delta = max_approved_revenue_sum - potential_revenue_sum
trace_min = go.Bar(
name=(
'Guaranteed revenue <br>' + '$' + str(round(min_revenue_sum))
),
y=['revenue'],
x=[min_revenue_sum],
width=[0.3],
orientation='h',
marker_color='rgb(0,110,160)'
)
trace_potential = go.Bar(
name=(
'Potential revenue <br>' + '$' + str(round(potential_revenue_sum))
),
y=['revenue'],
x=[min_potential_delta],
width=[0.3],
orientation='h',
marker_color='rgb(56,178,234)'
)
trace_max = go.Bar(
name=(
'Max. approved revenue <br>' + '$' + str(round(max_approved_revenue_sum))
),
y=['revenue'],
x=[potential_max_delta],
width=[0.3],
orientation='h',
marker_color='rgb(204,239,255)'
)
trace_elearning = go.Bar(
name=(
'Potential e-learning revenue <br>'
+ '$' + str(round(potential_e_learning_revenue_sum))
),
y=['revenue'],
x=[potential_e_learning_revenue_sum],
width=[0.3],
orientation='h',
marker_color='rgb(230,230,230)',
)
data = [trace_min, trace_potential, trace_max, trace_elearning]
layout = go.Layout(
barmode='stack',
yaxis={
'visible':False,
'showticklabels':False,
'fixedrange':True
},
xaxis={
'showgrid':False,
'visible':False,
'showticklabels':False,
'fixedrange':True,
},
plot_bgcolor='rgb(255,255,255)',
margin={
'l':0,
'r':0,
't':0,
'b':0,
},
legend={
'orientation':'h',
'traceorder':'normal'
},
hovermode=False,
font={'size':16},
height=200,
)
fig = go.Figure(data=data, layout=layout)
return dcc.Graph(
figure=fig,
config={'displayModeBar':False},
)
# child level table
def make_table(df):
table = dash_table.DataTable(
id='child_level',
data=df.to_dict('records'),
columns=[
{
'id': 'name',
'name': 'Child name',
'type': 'text'
}, {
'id': 'case_number',
'name': 'Case number',
'type': 'text'
}, {
'id': 'biz_name',
'name': 'Business',
'type': 'text'
}, {
'id': 'attendance_category',
'name': 'Attendance risk',
'type': 'text'
}, {
'id': 'attendance_rate',
'name': 'Attendance rate',
'type': 'numeric',
'format': {'nully':'-%',
'prefix': None,
'specifier': '.0%'}
}, {
'id': 'min_revenue',
'name': 'Guaranteed revenue',
'type': 'numeric',
'format': FormatTemplate.money(2)
}, {
'id': 'potential_revenue',
'name': 'Potential revenue',
'type': 'numeric',
'format': FormatTemplate.money(2)
}, {
'id': 'max_revenue',
'name': 'Max. approved revenue',
'type': 'numeric',
'format': FormatTemplate.money(2)
}, {
'id': 'e_learning_revenue_potential',
'name': 'Potential e-learning revenue',
'type': 'numeric',
'format': FormatTemplate.money(2)
}
],
style_data_conditional=[
{
'if': {
'filter_query': '{attendance_category} = "Not enough info"',
'column_id': 'attendance_category'
},
'backgroundColor': 'rgb(248, 248, 248)',
'color': 'rgb(128,128,128)'
},
{
'if': {
'filter_query': '{attendance_category} = "Sure bet"',
'column_id': 'attendance_category'
},
'color': '#0f8c48'
},
{
'if': {
'filter_query': '{attendance_category} = "Sure bet"',
'column_id': 'attendance_rate'
},
'color': '#0f8c48'
},
{
'if': {
'filter_query': '{attendance_category} = "Not met"',
'column_id': 'attendance_category'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "Not met"',
'column_id': 'attendance_rate'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "At risk"',
'column_id': 'attendance_category'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "At risk"',
'column_id': 'attendance_rate'
},
'color': '#da5d4f'
},
{
'if': {
'filter_query': '{attendance_category} = "On track"',
'column_id': 'attendance_category'
},
'color': '#0f8c48'
},
{
'if': {
'filter_query': '{attendance_category} = "On track"',
'column_id': 'attendance_rate'
},
'color': '#0f8c48'
},
],
style_header={
'whiteSpace': 'normal',
'height': 'auto',
'maxWidth': '100px',
},
style_table={
'padding': '20px',
'overflowX': 'auto',
},
sort_action='native',
sort_mode='single',
)
return table
if __name__ == '__main__':
from data_input import get_dashboard_data
df_dashboard, latest_date, is_data_insufficient, days_req_for_warnings = get_dashboard_data()
# fig = make_revenue_chart(df_dashboard)
# fig.show()
make_attendance_table(df_dashboard)