|
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 | +} |
5 | 6 |
|
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; |
10 | 15 | }); |
| 16 | + } |
11 | 17 |
|
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 | + } |
14 | 24 |
|
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 | +} |
21 | 27 |
|
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(); |
28 | 30 |
|
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); |
41 | 42 | } |
42 | | - drawChart(data); |
43 | | - }); |
| 43 | + } |
| 44 | + |
| 45 | + return data; |
44 | 46 | } |
45 | 47 |
|
46 | 48 | function drawChart(rows) { |
47 | 49 | // 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(); |
51 | 53 | dataTable.addColumn({ type: "string", id: "Name" }); |
52 | 54 | dataTable.addColumn({ type: "string", id: "Term" }); |
53 | 55 | dataTable.addColumn({ type: "date", id: "Start" }); |
54 | 56 | dataTable.addColumn({ type: "date", id: "End" }); |
55 | 57 |
|
56 | 58 | dataTable.addRows(rows); |
57 | 59 |
|
58 | | - let options = { |
| 60 | + const options = { |
59 | 61 | timeline: { colorByRowLabel: true }, |
60 | 62 | }; |
61 | 63 |
|
62 | 64 | chart.draw(dataTable, options); |
63 | 65 | } |
| 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