|
27 | 27 | <script src="https://www.gstatic.com/charts/loader.js"></script> |
28 | 28 | <script src=" https://unpkg.com/[email protected]/dist/d3.min.js" ></script> |
29 | 29 | <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 | + } |
32 | 35 |
|
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 = {}; |
36 | 38 | 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++; |
76 | 50 | } |
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); |
88 | 51 | }); |
| 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; |
89 | 66 | } |
90 | 67 |
|
91 | 68 | function drawBarChart(rows) { |
|
95 | 72 | legend: { position: "none" }, |
96 | 73 | axes: { x: { 0: { label: "" } } }, |
97 | 74 | }; |
98 | | - let chart = new google.charts.Bar(document.getElementById("chart_div")); |
| 75 | + const chart = new google.charts.Bar(document.getElementById("chart_div")); |
99 | 76 | chart.draw(data, options); |
100 | 77 | } |
| 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 | + }); |
101 | 88 | </script> |
102 | 89 | </head> |
103 | 90 | <body> |
|
0 commit comments