Skip to content

Commit be4bca5

Browse files
committed
Refactor into smaller functions
1 parent 221c31a commit be4bca5

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

script.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,75 @@
1-
function getDataAndDrawChart(showCurrent = true, showFormer = true) {
2-
const url =
3-
"https://raw.githubusercontent.com/python/devguide/main/core-developers/developers.csv";
4-
const today = new Date();
1+
async function fetchCsvData(url) {
2+
const response = await fetch(url);
3+
const csvText = await response.text();
4+
return d3.csvParseRows(csvText, d3.autoType);
5+
}
56

6-
d3.text(url).then(function (csvText) {
7-
// Parse the CSV data into an array of objects
8-
let data = d3.csvParseRows(csvText, d3.autoType, function (d) {
9-
return d;
7+
function filterData(data, showCurrent, showFormer) {
8+
// Oldest first
9+
data.reverse();
10+
11+
if (!showFormer) {
12+
// Filter out former core developers
13+
data = data.filter(function (d) {
14+
return d[3] === null;
1015
});
16+
}
1117

12-
// Oldest first
13-
data.reverse();
18+
if (!showCurrent) {
19+
// Filter out current core developers
20+
data = data.filter(function (d) {
21+
return d[3] !== null;
22+
});
23+
}
1424

15-
if (!showFormer) {
16-
// Filter out former core developers
17-
data = data.filter(function (d) {
18-
return d[3] === null;
19-
});
20-
}
25+
return data;
26+
}
2127

22-
if (!showCurrent) {
23-
// Filter out current core developers
24-
data = data.filter(function (d) {
25-
return d[3] !== null;
26-
});
27-
}
28+
function cleanData(data) {
29+
const today = new Date();
2830

29-
for (let i = 0; i < data.length; i++) {
30-
// Remove the last element: "Notes"
31-
data[i] = data[i].slice(0, -1);
32-
// Set second element to third element
33-
data[i][1] = data[i][2].toISOString().slice(0, 10);
34-
// If no "Left" date, use today
35-
if (data[i][3] === null) {
36-
data[i][3] = today;
37-
} else {
38-
// Append leaving date to second element
39-
data[i][1] += " to " + data[i][3].toISOString().slice(0, 10);
40-
}
31+
for (let i = 0; i < data.length; i++) {
32+
// Remove the last element: "Notes"
33+
data[i] = data[i].slice(0, -1);
34+
// Set second element to third element
35+
data[i][1] = data[i][2].toISOString().slice(0, 10);
36+
// If no "Left" date, use today
37+
if (data[i][3] === null) {
38+
data[i][3] = today;
39+
} else {
40+
// Append leaving date to second element
41+
data[i][1] += " to " + data[i][3].toISOString().slice(0, 10);
4142
}
42-
drawChart(data);
43-
});
43+
}
44+
45+
return data;
4446
}
4547

4648
function drawChart(rows) {
4749
// https://developers.google.com/chart/interactive/docs/gallery/timeline
48-
let container = document.getElementById("chart_div");
49-
let chart = new google.visualization.Timeline(container);
50-
let dataTable = new google.visualization.DataTable();
50+
const container = document.getElementById("chart_div");
51+
const chart = new google.visualization.Timeline(container);
52+
const dataTable = new google.visualization.DataTable();
5153
dataTable.addColumn({ type: "string", id: "Name" });
5254
dataTable.addColumn({ type: "string", id: "Term" });
5355
dataTable.addColumn({ type: "date", id: "Start" });
5456
dataTable.addColumn({ type: "date", id: "End" });
5557

5658
dataTable.addRows(rows);
5759

58-
let options = {
60+
const options = {
5961
timeline: { colorByRowLabel: true },
6062
};
6163

6264
chart.draw(dataTable, options);
6365
}
66+
67+
async function getDataAndDrawChart(showCurrent = true, showFormer = true) {
68+
const url =
69+
"https://raw.githubusercontent.com/python/devguide/main/core-developers/developers.csv";
70+
71+
const csvData = await fetchCsvData(url);
72+
const filteredData = filterData(csvData, showCurrent, showFormer);
73+
const cleanedData = cleanData(filteredData);
74+
drawChart(cleanedData);
75+
}

0 commit comments

Comments
 (0)