good, same would be better also for the columns,
not ask for data you not know they exist
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Coding Train: Data and APIs Project 2</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
</head>
<body>
<h1>script code https://discourse.processing.org/t/switching-from-localhost-to-nodejs-and-express/14377</h1>
<canvas id="chart" width="400" height="200"></canvas>
<script>
window.addEventListener('load',setup); // add kll
async function setup(){
const ctx = document.getElementById('chart').getContext('2d');
console.log("start getData"); // add kll
const dataSalary = await getData();
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: dataSalary.years,
datasets: [{
label: 'Salary',
data: dataSalary.amount,
fill: false,
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
options: {
}
});
}
async function getData(){
console.log("read assets/test.csv"); // add kll
const response = await fetch('assets/test.csv'); // mod kll
//const response = await fetch('KentJobHist.csv');
const data = await response.text();
const years = [];
const amount = [];
const rows = data.split('\n').slice(1); // ?? kill header?
console.log(" found rows: "+rows.length); // add kll
rows.forEach(row => {
if ( row.length > 0 ) { // Kent
const cols = row.split(',');
console.log(" found cols: "+cols.length); // add kll
if ( cols.length >= 19 ) { // add kll
const changedate = cols[17].split(" ");
console.log(changedate);
years.push(cols[16]+ ' '+ changedate[0]);
amount.push(parseFloat(cols[18]));
}
}
});
return {years, amount};
}
</script>
</body>
</html>