Skip to content

Commit 221c31a

Browse files
committed
Refactor
1 parent 7066dd3 commit 221c31a

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

by-year.html

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,65 +27,42 @@
2727
<script src="https://www.gstatic.com/charts/loader.js"></script>
2828
<script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>
2929
<script>
30-
google.charts.load("current", { packages: ["bar"] });
31-
google.charts.setOnLoadCallback(getDataAndDrawChart);
30+
async function fetchCsvData(url) {
31+
const response = await fetch(url);
32+
const csvText = await response.text();
33+
return d3.csvParseRows(csvText, d3.autoType);
34+
}
3235

33-
function getDataAndDrawChart() {
34-
const url =
35-
"https://raw.githubusercontent.com/python/devguide/main/core-developers/developers.csv";
36+
function processData(data) {
37+
const countsPerYear = {};
3638
const today = new Date();
37-
38-
d3.text(url).then(function (csvText) {
39-
// Parse the CSV data into an array of objects
40-
let data = d3.csvParseRows(csvText, d3.autoType, function (d) {
41-
return d;
42-
});
43-
44-
let countsPerYear = {};
45-
46-
data.forEach((item) => {
47-
let joinDate = new Date(item[2]); // Assuming the join date is the third item in the array
48-
let joinYear = joinDate.getFullYear();
49-
50-
if (!countsPerYear[joinYear]) {
51-
countsPerYear[joinYear] = { joins: 0, leaves: 0 };
52-
}
53-
54-
countsPerYear[joinYear].joins++;
55-
56-
if (item[3]) {
57-
// If there is a leave date
58-
let leaveDate = new Date(item[3]); // Assuming the leave date is the fourth item in the array
59-
let leaveYear = leaveDate.getFullYear();
60-
61-
if (!countsPerYear[leaveYear]) {
62-
countsPerYear[leaveYear] = { joins: 0, leaves: 0 };
63-
}
64-
65-
countsPerYear[leaveYear].leaves++;
66-
}
67-
});
68-
69-
// Add zero to any missing years
70-
let startYear = Object.keys(countsPerYear)[0];
71-
let endYear = today.getFullYear();
72-
for (let year = startYear; year <= endYear; year++) {
73-
if (!countsPerYear[year]) {
74-
countsPerYear[year] = { joins: 0, leaves: 0 };
75-
}
39+
data.forEach((item) => {
40+
const joinYear = new Date(item[2]).getFullYear();
41+
countsPerYear[joinYear] = countsPerYear[joinYear] || { joins: 0, leaves: 0 };
42+
countsPerYear[joinYear].joins++;
43+
if (item[3]) {
44+
const leaveYear = new Date(item[3]).getFullYear();
45+
countsPerYear[leaveYear] = countsPerYear[leaveYear] || {
46+
joins: 0,
47+
leaves: 0,
48+
};
49+
countsPerYear[leaveYear].leaves++;
7650
}
77-
78-
let countsPerYearArray = Object.entries(countsPerYear).map(
79-
// ([year, counts]) => [year, counts.joins, counts.leaves],
80-
([year, counts]) => [year, counts.joins],
81-
);
82-
// countsPerYearArray.unshift(["Year", "New core devs", "Number leaving"]);
83-
countsPerYearArray.unshift(["Year", "New core devs"]);
84-
85-
console.log(countsPerYearArray);
86-
87-
drawBarChart(countsPerYearArray);
8851
});
52+
const startYear = Object.keys(countsPerYear)[0];
53+
const endYear = today.getFullYear();
54+
for (let year = startYear; year <= endYear; year++) {
55+
countsPerYear[year] = countsPerYear[year] || { joins: 0, leaves: 0 };
56+
}
57+
return countsPerYear;
58+
}
59+
60+
function convertDataToArray(countsPerYear) {
61+
const countsPerYearArray = Object.entries(countsPerYear).map(
62+
([year, counts]) => [year, counts.joins],
63+
);
64+
countsPerYearArray.unshift(["Year", "New core devs"]);
65+
return countsPerYearArray;
8966
}
9067

9168
function drawBarChart(rows) {
@@ -95,9 +72,19 @@
9572
legend: { position: "none" },
9673
axes: { x: { 0: { label: "" } } },
9774
};
98-
let chart = new google.charts.Bar(document.getElementById("chart_div"));
75+
const chart = new google.charts.Bar(document.getElementById("chart_div"));
9976
chart.draw(data, options);
10077
}
78+
79+
google.charts.load("current", { packages: ["bar"] });
80+
google.charts.setOnLoadCallback(async function () {
81+
const url =
82+
"https://raw.githubusercontent.com/python/devguide/main/core-developers/developers.csv";
83+
const csvData = await fetchCsvData(url);
84+
const processedData = processData(csvData);
85+
const dataArray = convertDataToArray(processedData);
86+
drawBarChart(dataArray);
87+
});
10188
</script>
10289
</head>
10390
<body>

0 commit comments

Comments
 (0)