-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomponent_setup_charts.py
More file actions
270 lines (248 loc) · 8.97 KB
/
component_setup_charts.py
File metadata and controls
270 lines (248 loc) · 8.97 KB
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
# ## Charts Setup
#
# *You must run the cells in this section each time you connect to a new runtime. For example, when you return to the notebook after an idle timeout, when the runtime crashes, or when you restart or factory reset the runtime.*
# ! pip install altair pyarrow==14.0.0 >> pip.log
# Import chart packages and define chart functions. The currently available chart functions are:
#
# * Release count
# * Objects per stage
# * Releases by month
# * Objects per year
# * Top buyers
# * Usability indicators
# +
# @title Chart functions { display-mode: "form" }
import altair as alt
class MissingColumnsError(Exception):
def __init__(self, columns):
super().__init__(f"The source data is missing one or more of these columns: {columns}")
chart_properties = {
"width": 600,
"height": 350,
"padding": 50,
"title": alt.TitleParams(text="", subtitle=[""], fontSize=18),
}
chart_axis = {
"titleFontSize": 14,
"labelFontSize": 14,
"labelPadding": 5,
"ticks": False,
"domain": False,
}
def check_columns(columns, data):
# check if input contains the right columns
if not columns.issubset(data.columns):
raise MissingColumnsError(columns)
def plot_release_count(release_counts):
check_columns({"collection_id", "release_type", "release_count", "ocid_count"}, release_counts)
return (
alt.Chart(release_counts)
.mark_bar()
.encode(
x=alt.X(
"release_count",
type="ordinal",
axis=alt.Axis(title="release count", labelAngle=0),
),
y=alt.Y(
"ocid_count",
type="quantitative",
axis=alt.Axis(title="ocid count", format="~s", tickCount=5),
),
color=alt.Color(
"release_type",
type="nominal",
title="release type",
scale=alt.Scale(range=["#D6E100", "#FB6045", "#23B2A7", "#6C75E1"]),
),
tooltip=[
alt.Tooltip("release_count", title="release count"),
alt.Tooltip("ocid_count", title="ocid count", format="~s"),
alt.Tooltip("release_type", title="release type"),
alt.Tooltip("collection_id", title="collection id"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)
def plot_objects_per_stage(objects_per_stage):
check_columns({"stage", "object_count"}, objects_per_stage)
stages = ["planning", "tender", "awards", "contracts", "implementation"]
return (
alt.Chart(objects_per_stage)
.mark_bar(fill="#d6e100")
.encode(
x=alt.X(
"stage",
type="ordinal",
scale=alt.Scale(domain=stages),
sort=stages,
axis=alt.Axis(title="stage", labelAngle=0),
),
y=alt.Y(
"object_count",
type="quantitative",
axis=alt.Axis(title="number of objects", format="~s", tickCount=len(stages)),
),
tooltip=[
alt.Tooltip("stage", title="stage"),
alt.Tooltip("object_count", title="number of objects"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)
def plot_releases_by_month(release_dates):
check_columns({"date", "collection_id", "release_type", "release_count"}, release_dates)
max_rows = 5000
# check if number of rows is more than 5000
if release_dates.shape[0] > max_rows:
alt.data_transformers.disable_max_rows()
# draw chart
return (
alt.Chart(release_dates)
.mark_line(strokeWidth=3)
.encode(
x=alt.X("date", timeUnit="yearmonth", axis=alt.Axis(title="year and month")),
y=alt.Y(
"release_count",
type="quantitative",
aggregate="sum",
axis=alt.Axis(title="number of releases", format="~s", tickCount=5),
scale=alt.Scale(zero=False),
),
color=alt.Color(
"release_type",
type="nominal",
scale=alt.Scale(range=["#D6E100", "#FB6045", "#23B2A7", "#6C75E1"]),
legend=alt.Legend(title="release type"),
),
tooltip=[
alt.Tooltip("date", timeUnit="yearmonth", title="date"),
alt.Tooltip("release_count", aggregate="sum", title="number of releases"),
alt.Tooltip("release_type", title="release type"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)
def plot_objects_per_year(objects_per_year):
check_columns({"year", "tenders", "awards"}, objects_per_year)
stages = ["tenders", "awards"]
return (
alt.Chart(objects_per_year)
.transform_fold(stages)
.mark_line(strokeWidth=3)
.encode(
x=alt.X(
"year",
type="quantitative",
axis=alt.Axis(title="year", format=".0f", tickCount=objects_per_year.shape[0]),
),
y=alt.Y(
"value",
type="quantitative",
axis=alt.Axis(title="number of objects", format="~s", tickCount=5),
scale=alt.Scale(zero=False),
),
color=alt.Color(
"key",
type="nominal",
title="object type",
scale=alt.Scale(domain=stages, range=["#D6E100", "#FB6045"]),
),
tooltip=[
alt.Tooltip("year", title="year", type="quantitative"),
alt.Tooltip("value", title="number of objects", type="quantitative"),
alt.Tooltip("key", title="object type", type="nominal"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)
def plot_top_buyers(buyers):
check_columns({"name", "total_tenders"}, buyers)
return (
alt.Chart(buyers)
.mark_bar(fill="#d6e100")
.encode(
x=alt.X(
"total_tenders",
type="quantitative",
axis=alt.Axis(title="number of tenders", format="~s", tickCount=5),
),
y=alt.Y(
"name",
type="ordinal",
axis=alt.Axis(title="buyer", labelAngle=0),
sort=alt.SortField("total_tenders", order="descending"),
),
tooltip=[
alt.Tooltip("name", title="buyer", type="nominal"),
alt.Tooltip("total_tenders", title="number of tenders", type="quantitative"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)
def plot_usability_indicators(data, lang="English"):
labels = {
"English": {
"nrow": "row_number(indicator)",
"sort": "calculation",
"y_sort": "indicator",
"groupby": "Use case",
"title": "number of indicators",
"tooltip_missing": "Missing Fields",
},
"Spanish": {
"nrow": "row_number(Indicador)",
"sort": "¿Se puede calcular?",
"y_sort": "Indicador",
"groupby": "Caso de Uso",
"title": "Número de indicadores",
"tooltip_missing": "Campos faltantes",
},
}
return (
alt.Chart(data)
.transform_window(
nrow=labels[lang]["nrow"],
frame=[None, None],
sort=[{"field": labels[lang]["sort"]}],
groupby=[labels[lang]["groupby"]],
)
.mark_circle(size=250, opacity=1)
.encode(
x=alt.X(
"nrow",
type="quantitative",
axis=alt.Axis(title=[labels[lang]["title"], ""], orient="top", tickCount=5),
),
y=alt.Y(
labels[lang]["groupby"],
type="nominal",
sort=alt.Sort(field=labels[lang]["y_sort"], op="count", order="descending"),
),
color=alt.Color(
labels[lang]["sort"],
type="nominal",
scale=alt.Scale(range=["#fb6045", "#d6e100"]),
legend=alt.Legend(title=[labels[lang]["sort"]]),
),
tooltip=[
alt.Tooltip(labels[lang]["y_sort"], type="nominal"),
alt.Tooltip(labels[lang]["groupby"], type="nominal"),
alt.Tooltip(labels[lang]["sort"], type="nominal"),
alt.Tooltip(labels[lang]["tooltip_missing"], type="nominal"),
],
)
.properties(**chart_properties)
.configure_axis(**chart_axis)
.configure_view(strokeWidth=0)
)