-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (66 loc) · 2.17 KB
/
script.js
File metadata and controls
78 lines (66 loc) · 2.17 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
async function fetchCsvData() {
const url =
"https://raw.githubusercontent.com/python/devguide/main/core-team/core-team.csv";
const response = await fetch(url);
const csvText = await response.text();
return d3.csvParseRows(csvText, d3.autoType);
}
function filterData(data, showCurrent, showFormer) {
// Oldest first
data.reverse();
if (!showFormer) {
// Filter out former team members
data = data.filter(function (d) {
return d[3] === null;
});
}
if (!showCurrent) {
// Filter out current team members
data = data.filter(function (d) {
return d[3] !== null;
});
}
return data;
}
function cleanData(data) {
const today = new Date();
for (let i = 0; i < data.length; i++) {
// Remove the last element: "Notes"
data[i] = data[i].slice(0, -1);
// Set second element to third element
data[i][1] = data[i][2].toISOString().slice(0, 10);
// If no "Left" date, use today
if (data[i][3] === null) {
data[i][3] = today;
} else {
// Append leaving date to second element
data[i][1] += " to " + data[i][3].toISOString().slice(0, 10);
}
}
return data;
}
function drawChart(rows) {
// https://developers.google.com/chart/interactive/docs/gallery/timeline
const container = document.getElementById("chart_div");
const chart = new google.visualization.Timeline(container);
const dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: "string", id: "Name" });
dataTable.addColumn({ type: "string", id: "Term" });
dataTable.addColumn({ type: "date", id: "Start" });
dataTable.addColumn({ type: "date", id: "End" });
dataTable.addRows(rows);
const options = {
timeline: { colorByRowLabel: true },
};
chart.draw(dataTable, options);
}
async function getDataAndDrawChart(showCurrent = true, showFormer = true) {
google.charts.load("current", { packages: ["timeline"] });
google.charts.setOnLoadCallback(async function () {
const csvData = await fetchCsvData();
const filteredData = filterData(csvData, showCurrent, showFormer);
const cleanedData = cleanData(filteredData);
drawChart(cleanedData);
console.table(filteredData);
});
}