Switching from localhost to nodejs and express

i use the
index.html from
https://github.com/CodingTrain/Intro-to-Data-APIs-JS/blob/source/module1/02_fetch_csv_exercise_multiple/index.html ,
and a dummy csv file and see


i am sure that dummy file was read and used, the inspect console was not very helpful
update with the original data see:


RPI setup details here


i had a hard time to get your above script tested,
and after it run not have your data file… anyhow
test2.html

<!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);
        async function setup(){
        const ctx = document.getElementById('chart').getContext('2d');
                                console.log("start getData");
        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");
                const response = await fetch('assets/test.csv');
                //const response = await fetch('KentJobHist.csv');
                const data = await response.text();
                const years = [];
                const amount = [];
                const rows = data.split('\n').slice(1);
                        rows.forEach(row => {
                                const cols = row.split(',');
                                console.log(cols);
                                //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> 

produce


1 Like