Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How to Convert csv file data to a Google Line Chart in JavaScript

I am trying to take data from a local csv file and display it as a Google line chart within an html file that uses jQuery. I started an http server using Python 3.9, in the folder that my html file and csv data file are in. But when I open the html file, the chart isn't displayed. The command prompt that the server is running in shows Get requests for the html file as it's being accessed, but doesn't show requests for the csv data file after the html file is opened. Also, I don't get a CORS error when using Firefox, but I do if I use Edge. Yet, neither browser displays the line chart.

Below is a copy of the code in the html file:

<html>
    <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script type="text/javascript">

            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawChart);

            function drawChart() {

                // get the CSV file
                $.get("localhost:8000/data.csv", function(csvString) {
                    
                    // transform the CSV string into a 2-dimensional array
                    var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});

                    // this new DataTable object holds all the data
                    var data = new google.visualization.arrayToDataTable(arrayData);
                    console.log(data);

                    // this view can select a subset of the data at a time
                    var view = new google.visualization.DataView(data);
                    view.setColumns([1,0]);

                    // set chart options
                    var options = {
                        title: 'Graph',
                        legend: { position: 'bottom' }
                    };

                    // create the chart object and draw it
                    var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
                    chart.draw(view, options);
                });
            }
        </script>
    </head>
    <body>
        <div id="line_chart" style="width: 600px; height: 400px"></div>
    </body>
</html>

The data.csv file contains:

55,0,5,6
60,1,8,10
65,2,0,7
70,3,6,2
55,4,6,5
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda