-
Notifications
You must be signed in to change notification settings - Fork 21
/
callbacks.py
984 lines (902 loc) · 37.1 KB
/
callbacks.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import dash
import dash_table
from dash_table.Format import Format, Group, Scheme
import dash_table.FormatTemplate as FormatTemplate
from datetime import datetime as dt
from app import app
####################################################################################################
# 000 - FORMATTING INFO
####################################################################################################
####################### Corporate css formatting
corporate_colors = {
'dark-blue-grey' : 'rgb(62, 64, 76)',
'medium-blue-grey' : 'rgb(77, 79, 91)',
'superdark-green' : 'rgb(41, 56, 55)',
'dark-green' : 'rgb(57, 81, 85)',
'medium-green' : 'rgb(93, 113, 120)',
'light-green' : 'rgb(186, 218, 212)',
'pink-red' : 'rgb(255, 101, 131)',
'dark-pink-red' : 'rgb(247, 80, 99)',
'white' : 'rgb(251, 251, 252)',
'light-grey' : 'rgb(208, 206, 206)'
}
externalgraph_rowstyling = {
'margin-left' : '15px',
'margin-right' : '15px'
}
externalgraph_colstyling = {
'border-radius' : '10px',
'border-style' : 'solid',
'border-width' : '1px',
'border-color' : corporate_colors['superdark-green'],
'background-color' : corporate_colors['superdark-green'],
'box-shadow' : '0px 0px 17px 0px rgba(186, 218, 212, .5)',
'padding-top' : '10px'
}
filterdiv_borderstyling = {
'border-radius' : '0px 0px 10px 10px',
'border-style' : 'solid',
'border-width' : '1px',
'border-color' : corporate_colors['light-green'],
'background-color' : corporate_colors['light-green'],
'box-shadow' : '2px 5px 5px 1px rgba(255, 101, 131, .5)'
}
navbarcurrentpage = {
'text-decoration' : 'underline',
'text-decoration-color' : corporate_colors['pink-red'],
'text-shadow': '0px 0px 1px rgb(251, 251, 252)'
}
recapdiv = {
'border-radius' : '10px',
'border-style' : 'solid',
'border-width' : '1px',
'border-color' : 'rgb(251, 251, 252, 0.1)',
'margin-left' : '15px',
'margin-right' : '15px',
'margin-top' : '15px',
'margin-bottom' : '15px',
'padding-top' : '5px',
'padding-bottom' : '5px',
'background-color' : 'rgb(251, 251, 252, 0.1)'
}
recapdiv_text = {
'text-align' : 'left',
'font-weight' : '350',
'color' : corporate_colors['white'],
'font-size' : '1.5rem',
'letter-spacing' : '0.04em'
}
####################### Corporate chart formatting
corporate_title = {
'font' : {
'size' : 16,
'color' : corporate_colors['white']}
}
corporate_xaxis = {
'showgrid' : False,
'linecolor' : corporate_colors['light-grey'],
'color' : corporate_colors['light-grey'],
'tickangle' : 315,
'titlefont' : {
'size' : 12,
'color' : corporate_colors['light-grey']},
'tickfont' : {
'size' : 11,
'color' : corporate_colors['light-grey']},
'zeroline': False
}
corporate_yaxis = {
'showgrid' : True,
'color' : corporate_colors['light-grey'],
'gridwidth' : 0.5,
'gridcolor' : corporate_colors['dark-green'],
'linecolor' : corporate_colors['light-grey'],
'titlefont' : {
'size' : 12,
'color' : corporate_colors['light-grey']},
'tickfont' : {
'size' : 11,
'color' : corporate_colors['light-grey']},
'zeroline': False
}
corporate_font_family = 'Dosis'
corporate_legend = {
'orientation' : 'h',
'yanchor' : 'bottom',
'y' : 1.01,
'xanchor' : 'right',
'x' : 1.05,
'font' : {'size' : 9, 'color' : corporate_colors['light-grey']}
} # Legend will be on the top right, above the graph, horizontally
corporate_margins = {'l' : 5, 'r' : 5, 't' : 45, 'b' : 15} # Set top margin to in case there is a legend
corporate_layout = go.Layout(
font = {'family' : corporate_font_family},
title = corporate_title,
title_x = 0.5, # Align chart title to center
paper_bgcolor = 'rgba(0,0,0,0)',
plot_bgcolor = 'rgba(0,0,0,0)',
xaxis = corporate_xaxis,
yaxis = corporate_yaxis,
height = 270,
legend = corporate_legend,
margin = corporate_margins
)
####################################################################################################
# 000 - DATA MAPPING
####################################################################################################
#Sales mapping
sales_filepath = 'data/datasource.xlsx'
sales_fields = {
'date' : 'Date',
'reporting_group_l1' : 'Country',
'reporting_group_l2' : 'City',
'sales' : 'Sales Units',
'revenues' : 'Revenues',
'sales target' : 'Sales Targets',
'rev target' : 'Rev Targets',
'num clients' : 'nClients'
}
sales_formats = {
sales_fields['date'] : '%d/%m/%Y'
}
####################################################################################################
# 000 - IMPORT DATA
####################################################################################################
###########################
#Import sales data
xls = pd.ExcelFile(sales_filepath)
sales_import=xls.parse('Static')
#Format date field
sales_import[sales_fields['date']] = pd.to_datetime(sales_import[sales_fields['date']], format=sales_formats[sales_fields['date']])
sales_import['date_2'] = sales_import[sales_fields['date']].dt.date
min_dt = sales_import['date_2'].min()
min_dt_str = str(min_dt)
max_dt = sales_import['date_2'].max()
max_dt_str = str(max_dt)
#Create L1 dropdown options
repo_groups_l1 = sales_import[sales_fields['reporting_group_l1']].unique()
repo_groups_l1_all_2 = [
{'label' : k, 'value' : k} for k in sorted(repo_groups_l1)
]
repo_groups_l1_all_1 = [{'label' : '(Select All)', 'value' : 'All'}]
repo_groups_l1_all = repo_groups_l1_all_1 + repo_groups_l1_all_2
#Initialise L2 dropdown options
repo_groups_l2 = sales_import[sales_fields['reporting_group_l2']].unique()
repo_groups_l2_all_2 = [
{'label' : k, 'value' : k} for k in sorted(repo_groups_l2)
]
repo_groups_l2_all_1 = [{'label' : '(Select All)', 'value' : 'All'}]
repo_groups_l2_all = repo_groups_l2_all_1 + repo_groups_l2_all_2
repo_groups_l1_l2 = {}
for l1 in repo_groups_l1:
l2 = sales_import[sales_import[sales_fields['reporting_group_l1']] == l1][sales_fields['reporting_group_l2']].unique()
repo_groups_l1_l2[l1] = l2
################################################################################################################################################## SET UP END
####################################################################################################
# 000 - DEFINE ADDITIONAL FUNCTIONS
####################################################################################################
def group_wavg(df, gr_by_cols, weight, value):
"""This function returns a df grouped by the gr_by_cols and calculate the weighted avg based
on the entries in the weight and value lists"""
# Calculate weight * value columns
wcols = []
cols = []
for i in range(0,len(value),1):
wcol = "w"+value[i]
wcols.append(wcol)
df[wcol] = df[weight[i]] * df[value[i]]
# Group by summing the wcols and weight columns
cols = weight
for i in wcols:
cols.append(i)
df1 = df.groupby(gr_by_cols)[cols].agg('sum')
df1.reset_index(inplace=True)
# Divide wcols by weight and remove columns
for i in range(0,len(value),1):
df1[value[i]] = df1[wcols[i]] / df1[weight[i]]
df1.drop(wcols[i], axis='columns', inplace=True)
return df1
def colorscale_generator(n, starting_col = {'r' : 186, 'g' : 218, 'b' : 212}, finish_col = {'r' : 57, 'g' : 81, 'b' : 85}):
"""This function generate a colorscale between two given rgb extremes, for an amount of data points
The rgb should be specified as dictionaries"""
r = starting_col['r']
g = starting_col['g']
b = starting_col['b']
rf = finish_col['r']
gf = finish_col['g']
bf = finish_col['b']
ri = (rf - r) / n
gi = (gf - g) / n
bi = (bf - b) / n
color_i = 'rgb(' + str(r) +','+ str(g) +',' + str(b) + ')'
my_colorscale = []
my_colorscale.append(color_i)
for i in range(n):
r = r + ri
g = g + gi
b = b + bi
color = 'rgb(' + str(round(r)) +','+ str(round(g)) +',' + str(round(b)) + ')'
my_colorscale.append(color)
return my_colorscale
# Create a corporate colorcale
colors = colorscale_generator(n=11)
corporate_colorscale = [
[0.0, colors[0]],
[0.1, colors[1]],
[0.2, colors[2]],
[0.3, colors[3]],
[0.4, colors[4]],
[0.5, colors[5]],
[0.6, colors[6]],
[0.7, colors[7]],
[0.8, colors[8]],
[0.9, colors[9]],
[1.0, colors[10]]]
####################################################################################################
####################################################################################################
####################################################################################################
# SALES PAGE
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
# 001 - L2 DYNAMIC DROPDOWN OPTIONS
####################################################################################################
@app.callback(
dash.dependencies.Output('reporting-groups-l2dropdown-sales', 'options'),
[dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value')])
def l2dropdown_options(l1_dropdown_value):
isselect_all = 'Start' #Initialize isselect_all
#Rembember that the dropdown value is a list !
for i in l1_dropdown_value:
if i == 'All':
isselect_all = 'Y'
break
elif i != '':
isselect_all = 'N'
else:
pass
#Create options for individual selections
if isselect_all == 'N':
options_0 = []
for i in l1_dropdown_value:
options_0.append(repo_groups_l1_l2[i])
options_1 = [] # Extract string of string
for i1 in options_0:
for i2 in i1:
options_1.append(i2)
options_list = [] # Get unique values from the string
for i in options_1:
if i not in options_list:
options_list.append(i)
else:
pass
options_final_1 = [
{'label' : k, 'value' : k} for k in sorted(options_list)]
options_final_0 = [{'label' : '(Select All)', 'value' : 'All'}]
options_final = options_final_0 + options_final_1
#Create options for select all or none
else:
options_final_1 = [
{'label' : k, 'value' : k} for k in sorted(repo_groups_l2)]
options_final_0 = [{'label' : '(Select All)', 'value' : 'All'}]
options_final = options_final_0 + options_final_1
return options_final
####################################################################################################
# 002 - RECAP TABLE
####################################################################################################
@app.callback(
[dash.dependencies.Output('recap-table', 'data'), dash.dependencies.Output('recap-table', 'columns'), dash.dependencies.Output('recap-table', 'style_data_conditional')],
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df_1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
del sales_df
# Aggregate df
metrics = ['Sales (M u)','Revenues (M €)','Customers (M)']
result = [df_1[sales_fields['sales']].sum()/1000000, df_1[sales_fields['revenues']].sum()/1000000, df_1[sales_fields['num clients']].sum()/1000000]
target = [df_1[sales_fields['sales target']].sum()/1000000, df_1[sales_fields['rev target']].sum()/1000000, '']
performance = [df_1[sales_fields['sales']].sum()/df_1[sales_fields['sales target']].sum(), df_1[sales_fields['revenues']].sum()/df_1[sales_fields['rev target']].sum(), '']
df = pd.DataFrame({'KPI' : metrics, 'Result' : result, 'Target': target, 'Target_Percent' : performance})
# Configure table data
data = df.to_dict('records')
columns = [
{'id' : 'KPI', 'name' : 'KPI'},
{'id' : 'Result', 'name' : 'Result', 'type' : 'numeric', 'format' : Format(scheme=Scheme.fixed, precision=2, group=Group.yes, group_delimiter=',', decimal_delimiter='.')},
{'id' : 'Target', 'name' : 'Target', 'type' : 'numeric', 'format' : Format(scheme=Scheme.fixed, precision=2, group=Group.yes, group_delimiter=',', decimal_delimiter='.')},
{'id' : 'Target_Percent', 'name' : '% Target', 'type': 'numeric', 'format' : FormatTemplate.percentage(2)}
]
# Configure conditional formatting
conditional_style=[
{'if' : {
'filter_query' : '{Result} >= {Target} && {Target} > 0',
'column_id' : 'Target_Percent'},
'backgroundColor' : corporate_colors['light-green'],
'color' : corporate_colors['dark-green'],
'fontWeight' : 'bold'
},
{'if' : {
'filter_query' : '{Result} < {Target} && {Target} > 0',
'column_id' : 'Target_Percent'},
'backgroundColor' : corporate_colors['pink-red'],
'color' : corporate_colors['dark-green'],
'fontWeight' : 'bold'
},
]
return data, columns, conditional_style
####################################################################################################
# 003 - SALES COUNT DAY
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-count-day', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
#Aggregate df
val_cols = [sales_fields['sales'],sales_fields['sales target']]
sales_df = sales_df.groupby(sales_fields['date'])[val_cols].agg('sum')
sales_df.reset_index(inplace=True)
# Filter based on the date filters
df = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
del sales_df
# Build graph
hovertemplate_xy = (
"<i>Day</i>: %{x|%a, %d-%b-%Y}<br>"+
"<i>Sales</i>: %{y:,d}"+
"<extra></extra>") # Remove trace info
data = go.Scatter(
x = df[sales_fields['date']],
y = df[sales_fields['sales']],
line = {'color' : corporate_colors['light-green'], 'width' : 0.5},
hovertemplate = hovertemplate_xy)
fig = go.Figure(data=data, layout=corporate_layout)
fig.update_layout(
title={'text' : "Sales per Day"},
xaxis = {
'title' : "Day",
'tickformat' : "%d-%m-%y"},
yaxis = {
'title' : "Sales (units)",
'range' : [0, 100000]},
showlegend = False)
return fig
####################################################################################################
# 004 - SALES COUNT MONTH
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-count-month', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
df1['month'] = df1[sales_fields['date']].dt.month
del sales_df
#Aggregate df
val_cols = [sales_fields['sales'], sales_fields['sales target']]
df = df1.groupby('month')[val_cols].agg('sum')
df.reset_index(inplace=True)
del df1
# Build graph
hovertemplate_xy = (
"<i>Month</i>: %{x}<br>"+
"<i>Sales</i>: %{y:,d}"+
"<extra></extra>") # Remove trace info
data = go.Bar(
x = df['month'],
y = df[sales_fields['sales']],
marker = {'color' : corporate_colors['light-green'], 'opacity' : 0.75},
hovertemplate = hovertemplate_xy)
fig = go.Figure(data=data, layout=corporate_layout)
# Add target% as line on secondary axis
hovertemplate_xy2 = (
"<i>Month</i>: %{x}<br>"+
"<i>Target percentage</i>: %{y:%}"+
"<extra></extra>") # Remove trace info
fig.add_trace(
go.Scatter(
x = df['month'],
y = df[sales_fields['sales']]/df[sales_fields['sales target']],
line = {'color': corporate_colors['pink-red'], 'width' : 2},
yaxis = "y2",
opacity = 0.75,
hovertemplate = hovertemplate_xy2)
)
fig.update_layout(
title={'text' : "Sales per Month vs Target"},
xaxis = {
'title' : "Month",
'tickvals' : [1,2,3,4,5,6,7,8,9,10,11,12], #Display x values with different labels
'ticktext' : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']},
yaxis = {'title' : "Sales (units)"},
showlegend = False)
fig.update_layout(yaxis2 = corporate_yaxis)
fig.update_layout(
yaxis2 = {
'title' : "% over Sales target",
'side' : "right",
'showgrid' : False,
'tickformat' : ".0%",
'range' : [0, 1.15],
'overlaying' : "y",
'linewidth' : 1},
hovermode = 'x')
return fig
####################################################################################################
# 005 - WEEKLY-WEEKDAY SALES HEATMAP
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-weekly-heatmap', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
df1['week'] = df1[sales_fields['date']].dt.strftime("%V")
df1['weekday'] = df1[sales_fields['date']].dt.weekday
del sales_df
#Aggregate df
val_cols = [sales_fields['sales']]
df = df1.groupby(['week','weekday'])[val_cols].agg('sum')
df.reset_index(inplace=True)
del df1
# Build graph
hovertemplate_here = (
"<i>Week</i>: %{x}<br>"+
"<i>Weekday</i>: %{y}<br>"+
"<i>Sales</i>: %{z}"+
"<extra></extra>") # Remove trace info
data = go.Heatmap(
x = df['weekday'],
y = df['week'],
z = df[sales_fields['sales']],
hovertemplate = hovertemplate_here,
hoverongaps = False,
colorscale = corporate_colorscale,
showscale = False,
xgap = 1,
ygap = 1)
fig = go.Figure(data=data, layout=corporate_layout)
fig.update_layout(
title={'text' : "Heatmap: Sales by week and weekeday"},
xaxis = {
'title' : "Weekday",
'tickvals' : [0,1,2,3,4,5,6], #Display x values with different labels
'ticktext' : ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']},
yaxis = {
'title' : "Calendar Week",
'showgrid' : False})
return fig
####################################################################################################
# 006 - SALES BY COUNTRY
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-count-country', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
del sales_df
#Aggregate df
val_cols = [sales_fields['sales']]
df = df1.groupby(sales_fields['reporting_group_l1'])[val_cols].agg('sum')
df.reset_index(inplace=True)
df.sort_values(sales_fields['reporting_group_l1'], axis=0, ascending=True, inplace=True, na_position='last')
del df1
#Prepare incr % data
hover_text=[]
sale_perc=[]
sale_base=[0]
sale_b=0
sales_tot = df[sales_fields['sales']].sum()
for index, row in df.iterrows():
sale_p = row[sales_fields['sales']]/sales_tot
hover_text.append(("<i>Country</i>: {}<br>"+
"<i>Sales</i>: {:.2%}"+
"<extra></extra>").format(row[sales_fields['reporting_group_l1']],
sale_p))
sale_b = sale_b + sale_p
sale_perc.append(sale_p)
sale_base.append(sale_b)
sale_base = sale_base[:-1]
df['sale_p'] = sale_perc
df['hovertext'] = hover_text
# Build graph
data = go.Bar(
x = df[sales_fields['reporting_group_l1']],
y = df['sale_p'],
base = sale_base,
marker = {'color': corporate_colors['light-green'],
'opacity' : 0.75},
hovertemplate = df['hovertext'])
fig = go.Figure(data=data, layout=corporate_layout)
fig.update_layout(
title={'text' : "Sales Percentage by Country"},
xaxis = {'title' : "Country", 'tickangle' : 0},
yaxis = {
'title' : "Sales Percentage",
'tickformat' : ".0%",
'range' : [0, 1]},
barmode = 'group',
showlegend = False)
return fig
####################################################################################################
# 007 - SALES BUBBLE CHART
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-bubble-county', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
del sales_df
#Aggregate df
val_cols = [sales_fields['sales'], sales_fields['num clients'], sales_fields['revenues']]
df = df1.groupby(sales_fields['reporting_group_l1'])[val_cols].agg('sum')
df.reset_index(inplace=True)
df['rev_per_cl'] = df[sales_fields['revenues']]/df[sales_fields['num clients']]
del df1
# Build graph
#Add hover text info on the df
hover_text = []
for index, row in df.iterrows():
hover_text.append(('<i>Country</i>: {}<br>'+
'<i>Sales</i>: {:,d}<br>'+
'<i>Clients</i>: {:,d}<br>'+
'<i>Revenues</i>: {:,d}'+
'<extra></extra>').format(row[sales_fields['reporting_group_l1']],
row[sales_fields['sales']],
row[sales_fields['num clients']],
row[sales_fields['revenues']]))
df['hovertext'] = hover_text
sizeref = 2.*max(df[sales_fields['sales']])/(100**2)
#Create bubbles (1 color per country, one trace per city)
country_names = sorted(df[sales_fields['reporting_group_l1']].unique())
countries = len(country_names)
colorscale = colorscale_generator(n=countries, starting_col = {'r' : 57, 'g' : 81, 'b' : 85}, finish_col = {'r' : 251, 'g' : 251, 'b' : 252})
fig = go.Figure(layout=corporate_layout)
i = 0
for co in country_names:
color = colorscale[i]
i = i+1
df_i = df.loc[df[sales_fields['reporting_group_l1']]==co, :].copy()
fig.add_trace(
go.Scatter(
x=df_i['rev_per_cl'],
y=df_i[sales_fields['num clients']],
name=co,
hovertemplate=df_i['hovertext'],
marker_size=df_i[sales_fields['sales']],
marker = {
'color' : color,
'line_width' : 1,
'line' : {'color' : corporate_colors['light-grey']}
})
)
fig.update_traces(mode='markers', marker= {'sizemode' : 'area', 'sizeref' : sizeref})
corporate_margins_here = corporate_margins
corporate_margins_here['t'] = 65
fig.update_layout(
title={'text' : "Revenue per Client by Country"},
xaxis = {'title' : "Revenue per Client", 'tickangle' : 0},
yaxis = {'title' : "Sales (Units)"},
margin = corporate_margins_here)
return fig
####################################################################################################
# 008 - SALES BY COUNTRY & CITY
####################################################################################################
@app.callback(
dash.dependencies.Output('sales-count-city', 'figure'),
[dash.dependencies.Input('date-picker-sales', 'start_date'),
dash.dependencies.Input('date-picker-sales', 'end_date'),
dash.dependencies.Input('reporting-groups-l1dropdown-sales', 'value'),
dash.dependencies.Input('reporting-groups-l2dropdown-sales', 'value')])
def update_chart(start_date, end_date, reporting_l1_dropdown, reporting_l2_dropdown):
start = dt.strptime(start_date, '%Y-%m-%d')
end = dt.strptime(end_date, '%Y-%m-%d')
# Filter based on the dropdowns
isselect_all_l1 = 'Start' #Initialize isselect_all
isselect_all_l2 = 'Start' #Initialize isselect_all
## L1 selection (dropdown value is a list!)
for i in reporting_l1_dropdown:
if i == 'All':
isselect_all_l1 = 'Y'
break
elif i != '':
isselect_all_l1 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l1 == 'N':
sales_df_1 = sales_import.loc[sales_import[sales_fields['reporting_group_l1']].isin(reporting_l1_dropdown), : ].copy()
else:
sales_df_1 = sales_import.copy()
## L2 selection (dropdown value is a list!)
for i in reporting_l2_dropdown:
if i == 'All':
isselect_all_l2 = 'Y'
break
elif i != '':
isselect_all_l2 = 'N'
else:
pass
# Filter df according to selection
if isselect_all_l2 == 'N':
sales_df = sales_df_1.loc[sales_df_1[sales_fields['reporting_group_l2']].isin(reporting_l2_dropdown), :].copy()
else:
sales_df = sales_df_1.copy()
del sales_df_1
# Filter based on the date filters
df1 = sales_df.loc[(sales_df[sales_fields['date']]>=start) & (sales_df[sales_fields['date']]<=end), :].copy()
del sales_df
# Aggregate df
val_cols = [sales_fields['sales'],sales_fields['sales target']]
df = df1.groupby([sales_fields['reporting_group_l1'],sales_fields['reporting_group_l2']])[val_cols].agg('sum')
df.reset_index(inplace=True)
# Include hover data
hover_text=[]
for index, row in df.iterrows():
hover_text.append(("<i>Country</i>: {}<br>"+
"<i>City</i>: {}<br>"+
"<i>Sales</i>: {:,d}<br>"+
"<i>Targets</i>: {:,d}"+
"<extra></extra>").format(row[sales_fields['reporting_group_l1']],
row[sales_fields['reporting_group_l2']],
row[sales_fields['sales']],
row[sales_fields['sales target']]))
df['hovertext'] = hover_text
df['l1l2'] = df[sales_fields['reporting_group_l1']] + "_" + df[sales_fields['reporting_group_l2']]
# Generate colors
ncolors = len(df[sales_fields['reporting_group_l2']].unique())
colorscale = colorscale_generator(n=ncolors)
# Build graph
data=[]
i = 0
for l in sorted(df['l1l2']):
df_l = df.loc[(df['l1l2']==l), :].copy()
trace= go.Bar(
name = l,
x = df_l[sales_fields['reporting_group_l1']],
y = df_l[sales_fields['sales']],
hovertemplate = df_l['hovertext'],
marker = {
'color' : colorscale[i],
'opacity' : 0.85,
'line_width' : 1,
'line' : {'color' : colorscale[i]}
}
)
i=i+1
data.append(trace)
fig = go.Figure(data=data, layout=corporate_layout)
fig.update_layout(
barmode='stack',
title={'text' : "Sales by Country & City"},
xaxis = {'title' : "Country", 'tickangle' : 0},
yaxis = {'title' : "Sales (Units)"},
showlegend = False)
return fig