-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvizro_adv.py
123 lines (100 loc) · 4.14 KB
/
vizro_adv.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
from typing import List, Literal
import dash_core_components as dcc
import vizro.models as vm
import vizro.plotly.express as px
from dash import html
from vizro import Vizro
from vizro.models.types import capture
from vizro.models import Action
from vizro.models._action._actions_chain import _action_validator_factory
import pandas as pd
import plotly.graph_objects as go
df = pd.read_csv('number-of-deaths-by-risk-factor.csv')
#df['Year'] = pd.to_datetime(df['Year'], format='%Y')
class YearRangeSelector(vm.RangeSlider):
type: Literal["year_range_selector"] = "year_range_selector"
def build(self):
range_slider_build_obj = super().build()
range_slider_build_obj[self.id].value = list(range(1990,2020,1))
range_slider_build_obj[self.id].allowCross = False
range_slider_build_obj[self.id].tooltip = {"always_visible": True,
"template": "{value}"
}
return range_slider_build_obj
vm.Filter.add_type("selector", YearRangeSelector)
vm.Parameter.add_type("selector", YearRangeSelector)
filters = [
vm.Filter(column="Entity", selector=vm.Dropdown(title="Country")),
vm.Filter(column="Year", selector=YearRangeSelector(
min = 1990,
max = 2019,
marks = {i: {'label': "'{}".format(str(i)[-2:]),
'style': {"fontSize": "10px"}} for i in range(1990, 2021, 2)}
))
]
components = list()
for col in df.columns[3:7]:
fig_col = px.scatter(df, x="Year", y=col,
color="Entity", title=col.replace('Deaths that are from all causes attributed to ', '').title(),
labels={"Year": "", col: ""}
)
components.append(vm.Graph(figure=fig_col))
page_0 = vm.Page(
title="Health-Related Deaths Dashboard",
layout=vm.Layout(grid=[[0, 1],
[2, 3]]),
components=components,
controls=filters,
)
@capture("graph")
def stacked_bar(data_frame):
values_to_remove = ['G20', 'World', '(WHO)', '(WB)', 'OECD']
data_frame = data_frame[~data_frame['Entity'].str.contains('|'.join(values_to_remove))]
data_frame = data_frame.drop(columns=["Entity", "Code"])
df_agg = data_frame.groupby('Year').sum().reset_index()
df_agg.columns = df_agg.columns.str.replace('Deaths that are from all causes attributed to ', '')
df_agg.columns = df_agg.columns.str.split(',').str[0]
# Sort the columns by the sum of values in descending order excluding 'Year' column
sorted_cols = df_agg.drop(columns=['Year']).sum().sort_values(ascending=False).index
df_agg = df_agg[['Year'] + sorted_cols.tolist()]
# Combine the lowest 5 causes into 'Others'
if len(df_agg.columns) > 6: # Ensure there are more than 6 columns
others_sum = df_agg.iloc[:, -8:].sum(axis=1)
df_agg = pd.concat([df_agg.iloc[:, :-8], pd.DataFrame({'Others': others_sum})], axis=1)
# Create the stacked bar chart
fig = go.Figure()
for i, col in enumerate(df_agg.columns[1:]): # Exclude 'Year' column
fig.add_trace(go.Bar(
x=df_agg['Year'],
y=df_agg[col],
name=col,
))
# Update layout
fig.update_layout(
title='Stacked Bar Chart of Causes of Death (Worldwide)',
xaxis_title='Year',
yaxis_title='Death Count',
barmode='stack' # Stacked bar chart
)
return fig
filters_2 = [
vm.Filter(column="Entity", selector=vm.Dropdown(title="Country")),
vm.Filter(column="Year", selector=YearRangeSelector(
min = 1990,
max = 2019,
marks = {i: {'label': "'{}".format(str(i)[-2:]),
'style': {"fontSize": "10px"}} for i in range(1990, 2021, 2)}
))
]
page_1 = vm.Page(
title="Custom Year on Year Deaths bar chart",
path="my-custom-url",
components=[
vm.Graph(
figure=stacked_bar(data_frame=df),
),
],
controls=filters_2,
)
dashboard = vm.Dashboard(pages=[page_0,page_1])
Vizro().build(dashboard).run()