File size: 7,237 Bytes
a760588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Example to show dashboard configuration."""

import pandas as pd
import vizro.models as vm
from vizro import Vizro
import vizro.plotly.express as px
from dash import get_asset_url, html
import dash_bootstrap_components as dbc

from vizro.models.types import capture
from vizro.tables import dash_ag_grid
from utils.helper import categorize_price, convert_price_to_numeric, columnDefs
from vizro.figures import kpi_card


# Custom charts are required because of the post-update calls
@capture("graph")
def bar(data_frame, top_n=20, **kwargs):
    data_top = data_frame.head(top_n)
    fig = px.bar(data_top, **kwargs)
    fig.update_layout(title=f"Top {top_n} Games by {kwargs['x']}", yaxis_title="", yaxis_autorange="reversed")
    return fig


@capture("graph")
def scatter(data_frame, top_n=20, **kwargs):
    data_top = data_frame.head(top_n)
    fig = px.scatter(data_top, **kwargs)
    fig.update_layout(
        title=f"Top {top_n} games by {kwargs['y']}. Each bubble represents a single game. The bigger the bubble, the more players."
    )

    return fig


@capture("graph")
def treemap(data_frame, top_n=20, **kwargs):
    data_top = data_frame.head(top_n)
    fig = px.treemap(data_top, **kwargs)
    fig.update_traces(root_color="lightgrey")
    fig.update_layout(title=f"Top {top_n} games by {kwargs['values']}. The bigger the square, the more players.")
    return fig


# Tidy data set
players = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/main/2025/week-5/Steam%20Top%20100%20Played%20Games%20-%20List.csv"
)
players["Price Category"] = players["Price"].apply(categorize_price)
players["Price Numeric"] = players["Price"].apply(convert_price_to_numeric)
players["Current Players"] = pd.to_numeric(players["Current Players"].str.replace(",", ""))
players["Peak Today"] = pd.to_numeric(players["Peak Today"].str.replace(",", ""))


tabs = vm.Tabs(
    tabs=[
        vm.Container(
            title="Bar",
            components=[
                vm.Graph(
                    id="bar",
                    figure=bar(
                        players,
                        y="Name",
                        x="Current Players",
                        title="Current Players by Game",
                        orientation="h",
                        color="Price Numeric",
                    ),
                )
            ],
        ),
        vm.Container(
            title="Treemap",
            components=[
                vm.Graph(
                    id="treemap",
                    figure=treemap(
                        players, path=[px.Constant("All"), "Name"], values="Current Players", color="Price Numeric"
                    ),
                )
            ],
        ),
        vm.Container(
            title="Bubble",
            components=[
                vm.Graph(
                    id="bubble",
                    figure=scatter(
                        players,
                        x="Price Numeric",
                        y="Current Players",
                        size="Current Players",
                        size_max=50,
                        opacity=0.5,
                        color="Price Numeric",
                    ),
                )
            ],
        ),
    ],
)


players_page = vm.Page(
    title="Total Players 🎮",
    layout=vm.Layout(grid=[[0, 1, 2, 3, 4]] + [[5, 5, 5, 5, 5]] * 5),
    components=[
        vm.Figure(
            id="kpi-1",
            figure=kpi_card(
                data_frame=players[players["Price Category"] == "Free To Play"],
                value_column="Current Players",
                value_format="{value:,}",
                icon="groups",
                title="Free to play",
            ),
        ),
        vm.Figure(
            id="kpi-2",
            figure=kpi_card(
                data_frame=players[players["Price Category"] == "Less than £15"],
                value_column="Current Players",
                value_format="{value:,}",
                icon="group",
                title="Less than £15",
            ),
        ),
        vm.Figure(
            id="kpi-3",
            figure=kpi_card(
                data_frame=players[players["Price Category"] == "£15-£30"],
                value_column="Current Players",
                value_format="{value:,}",
                icon="person",
                title="£15-£30",
            ),
        ),
        vm.Figure(
            id="kpi-4",
            figure=kpi_card(
                data_frame=players[players["Price Category"] == "£30-£45"],
                value_column="Current Players",
                value_format="{value:,}",
                icon="people",
                title="£30-£45",
            ),
        ),
        vm.Figure(
            id="kpi-5",
            figure=kpi_card(
                data_frame=players[players["Price Category"] == "More than 45£"],
                value_column="Current Players",
                value_format="{value:,}",
                icon="groups",
                title="More than 45£",
            ),
        ),
        tabs,
    ],
    controls=[
        vm.Parameter(
            targets=[
                "bar.x",
                "treemap.values",
                "bubble.y",
                "kpi-1.value_column",
                "kpi-2.value_column",
                "kpi-3.value_column",
                "kpi-4.value_column",
                "kpi-5.value_column",
            ],
            selector=vm.RadioItems(
                options=["Current Players", "Peak Today"],
                title="Select metric in all charts",
            ),
        ),
        vm.Parameter(
            targets=["bar.top_n", "bubble.top_n", "treemap.top_n"],
            selector=vm.Slider(min=10, max=50, step=5, value=20, title="Select top N:"),
        ),
        vm.Filter(
            column="Price Category",
            selector=vm.Checklist(
                options=["Free To Play", "Less than £15", "£15-£30", "£30-£45", "More than 45£"],
                title="Select price category:",
            ),
        ),
        vm.Filter(column="Name", selector=vm.Dropdown(title="Select game:")),
    ],
)

games_page = vm.Page(
    title="Most played games 👾",
    components=[
        vm.AgGrid(
            figure=dash_ag_grid(players, columnDefs=columnDefs, dashGridOptions={"rowHeight": 56, "pagination": True})
        )
    ],
)

dashboard = vm.Dashboard(
    title="Figure Friday - Week 5",
    pages=[players_page, games_page],
    navigation=vm.Navigation(
        nav_selector=vm.NavBar(
            items=[
                vm.NavLink(label="Players", pages=["Total Players 🎮"], icon="Person Play"),
                vm.NavLink(label="Games", pages=["Most played games 👾"], icon="Stadia Controller"),
            ]
        )
    ),
)

app = Vizro().build(dashboard)
app.dash.layout.children.append(
    dbc.NavLink(
        ["Made with ", html.Img(src=get_asset_url("logo.svg"), id="banner", alt="Vizro logo"), "vizro"],
        href="https://github.com/mckinsey/vizro",
        target="_blank",
        className="anchor-container",
    )
)


server = app.dash.server

if __name__ == "__main__":
    app.run()