Skip to content

Commit 7703985

Browse files
committed
Added custom template (to be completed) for Plotly figures.
1 parent d48ca34 commit 7703985

File tree

1 file changed

+58
-72
lines changed

1 file changed

+58
-72
lines changed

app/algo.py

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
import humanize
1111
from wordcloud import WordCloud, get_single_color_func
1212

13+
14+
# Define custom template
15+
custom_template = {
16+
"layout": {
17+
"autosize": True,
18+
"plot_bgcolor": "white",
19+
"font": {"family": "Roboto, sans-serif"},
20+
"title": None,
21+
"margin": dict(l=0, r=0, b=0, t=0),
22+
}
23+
}
24+
25+
1326
# Define color sequence for plots
1427
COLOR_SEQUENCE = ["#D9D9D9", "#1E2E5C"]
1528

@@ -84,26 +97,18 @@ def number_of_tracked_reports_over_time(
8497

8598
# Create figure
8699
fig = px.bar(
87-
data, x="year", y="mnc", color_discrete_sequence=COLOR_SEQUENCE, text_auto=True
100+
data, x="year", y="mnc", text_auto=True, color_discrete_sequence=COLOR_SEQUENCE
88101
)
89102

90103
# Force position and color of bar values
91104
fig.update_traces(textposition="outside", textfont=dict(color="black"))
92105

106+
# Define axes settings
107+
fig.update_xaxes(title=None, tickvals=data["year"].unique())
108+
fig.update_yaxes(title=None, visible=False, range=[0, data["mnc"].max() * 1.1])
109+
93110
# Update layout settings
94-
fig.update_layout(
95-
autosize=True,
96-
height=360,
97-
font_family="Roboto, sans-serif",
98-
title=None,
99-
xaxis=dict(title=None, tickvals=data["year"].unique()),
100-
yaxis=dict(
101-
title=None,
102-
visible=False,
103-
),
104-
plot_bgcolor="white",
105-
margin=dict(l=0, r=0, b=0, t=0),
106-
)
111+
fig.update_layout(template=custom_template, height=360)
107112

108113
# Define style of hover on bars
109114
fig.update_traces(
@@ -178,10 +183,14 @@ def breakdown_of_reports_by_sector(df: pd.DataFrame) -> go.Figure:
178183
orientation="h", # Horizontal orientation
179184
labels={"percent": "Percentage of Companies (%)", "sector": "Sector"},
180185
text="percent", # Show the percentage as text label
181-
hover_data={"unique_company_count": True, "percent": ":.2f%"}, # Add tooltip for count and rounded percentage
186+
hover_data={
187+
"unique_company_count": True,
188+
"percent": ":.2f%",
189+
}, # Add tooltip for count and rounded percentage
182190
)
183191

184-
fig.update_layout(title=None)
192+
# Update layout settings
193+
fig.update_layout(template=custom_template)
185194

186195
return go.Figure(fig)
187196

@@ -228,8 +237,8 @@ def breakdown_of_reports_by_hq_country(df: pd.DataFrame) -> go.Figure:
228237
# Add tooltip for count and rounded percentage
229238
)
230239

231-
# Update layout to display the title above the chart
232-
fig.update_layout(title=None)
240+
# Update layout settings
241+
fig.update_layout(template=custom_template)
233242

234243
return go.Figure(fig)
235244

@@ -269,11 +278,8 @@ def breakdown_of_reports_by_sector_over_time(df: pd.DataFrame) -> go.Figure:
269278
category_orders={"Sectors": chart_order},
270279
)
271280

272-
# Reverse the order of legend items
273-
fig.update_layout(
274-
title=None,
275-
legend=dict(traceorder="reversed")
276-
)
281+
# Update layout settings
282+
fig.update_layout(template=custom_template, legend=dict(traceorder="reversed"))
277283

278284
# Adjusting the legend order and formatting the legend labels
279285
for i, trace in enumerate(fig.data):
@@ -455,22 +461,19 @@ def top_jurisdictions_revenue(df: pd.DataFrame, company: str, year: int) -> go.F
455461
x="total_revenues_%",
456462
y="jur_name",
457463
orientation="h",
458-
color_discrete_sequence=COLOR_SEQUENCE,
459464
text_auto=".1%",
465+
color_discrete_sequence=COLOR_SEQUENCE,
460466
)
461467

462468
# Set figure height (min. 480) depending on the number of jurisdictions
463469
fig_height = max(480, (48 * len(df["jur_name"])))
464470

471+
# Update axis layout
472+
fig.update_xaxes(title="Percentage of total revenue", tickformat=".0%")
473+
fig.update_yaxes(title=None)
474+
465475
# Update layout settings
466-
fig.update_layout(
467-
font_family="Roboto",
468-
xaxis=dict(title="Percentage of total revenue", tickformat=".0%"),
469-
yaxis_title=None,
470-
plot_bgcolor="white",
471-
height=fig_height,
472-
margin=dict(l=0, r=0, t=0, b=0),
473-
)
476+
fig.update_layout(template=custom_template, height=fig_height)
474477

475478
# Define position of text values
476479
values_positions = [
@@ -541,18 +544,17 @@ def pretax_profit_and_employees_rank(df: pd.DataFrame, company: str, year: int)
541544
else:
542545
max_x_value = 1
543546

547+
# Update axis layout
548+
fig.update_xaxes(title=None, tickformat=".0%", range=[0, max_x_value])
549+
fig.update_yaxes(title=None)
550+
544551
# Update layout settings
545552
fig.update_layout(
546-
font_family="Roboto",
547-
title=None,
548-
xaxis=dict(title=None, tickformat=".0%", range=[0, max_x_value]),
549-
yaxis_title=None,
550553
legend=dict(
551-
x=0.1, y=1.05, xanchor="center", yanchor="top", title=dict(text=""), orientation="h"
554+
orientation="h", yanchor="bottom", y=1.01, xanchor="left", x=0, title=dict(text="")
552555
),
553-
plot_bgcolor="white",
556+
template=custom_template,
554557
height=fig_height,
555-
margin=dict(l=0, r=0, t=10, b=0),
556558
)
557559

558560
# Add annotations for NaN values where there should have been a bar
@@ -639,24 +641,17 @@ def pretax_profit_and_profit_per_employee(
639641
custom_data=["jur_name"],
640642
)
641643

644+
# Update axis layout
645+
fig.update_xaxes(title="Percentage of profit", tickformat=".0%")
646+
fig.update_yaxes(title="Profit per employee")
647+
642648
# Update layout settings
643649
fig.update_layout(
644-
title=None,
645-
font_family="Roboto",
646-
autosize=True,
647-
height=360,
648-
xaxis=dict(
649-
title="Percentage of profit",
650-
tickformat=".0%",
651-
),
652-
yaxis=dict(
653-
title="Profit per employee",
654-
),
655650
legend=dict(
656-
x=0.1, y=1.05, xanchor="center", yanchor="top", title=dict(text=""), orientation="h"
651+
orientation="h", yanchor="bottom", y=1.01, xanchor="left", x=0, title=dict(text="")
657652
),
658-
plot_bgcolor="white",
659-
margin=dict(l=0, r=0, t=0, b=0),
653+
template=custom_template,
654+
height=380,
660655
)
661656

662657
# Define hover
@@ -739,15 +734,13 @@ def related_and_unrelated_revenues_breakdown(
739734
text_auto=".0%",
740735
)
741736

737+
# Update axis layout
738+
fig.update_xaxes(title=None, tickformat=".0%")
739+
fig.update_yaxes(title=None)
740+
742741
# Update layout settings
743742
fig.update_layout(
744-
title=None,
745-
xaxis=dict(title=None, tickformat=".0%"),
746-
yaxis_title=None,
747-
legend=dict(title=dict(text=""), orientation="h"),
748-
plot_bgcolor="white",
749-
width=800,
750-
height=480,
743+
legend=dict(title=dict(text=""), orientation="h"), template=custom_template
751744
)
752745

753746
# Define position of text values
@@ -915,17 +908,13 @@ def tax_havens_use_evolution(df: pd.DataFrame, company: str) -> go.Figure:
915908
barmode="group",
916909
text_auto=".1%",
917910
)
911+
# Update axis layout
912+
fig.update_xaxes(title=None)
913+
fig.update_yaxes(title=None, tickformat=".0%")
918914

919915
# Update layout settings
920916
fig.update_layout(
921-
title=None,
922-
xaxis_title=None,
923-
yaxis_title=None,
924-
yaxis_tickformat=".0%",
925-
legend=dict(title=dict(text=""), orientation="h"),
926-
plot_bgcolor="white",
927-
width=800,
928-
height=480,
917+
legend=dict(title=dict(text=""), orientation="h"), template=custom_template
929918
)
930919

931920
return go.Figure(fig)
@@ -1250,7 +1239,7 @@ def transparency_score_over_time(df: pd.DataFrame, company: str):
12501239

12511240
# Update layout settings
12521241
fig.update_layout(
1253-
title="Transparency score over time",
1242+
template=custom_template,
12541243
xaxis=dict(title=None, tickvals=df["year"].unique()),
12551244
yaxis=dict(
12561245
title=None,
@@ -1262,9 +1251,6 @@ def transparency_score_over_time(df: pd.DataFrame, company: str):
12621251
tickvals=[0, 25, 50, 75, 100],
12631252
ticktext=[0, "", "", "", 100],
12641253
),
1265-
plot_bgcolor="white",
1266-
width=800,
1267-
height=480,
12681254
)
12691255

12701256
# Force position and color of bar values

0 commit comments

Comments
 (0)