Adrian
Adrian

Reputation: 2656

Create Google Chart from CSV input

I am trying to create a Google Chart from CSV input.

Here is my current code (found in a tutorial), unfortunately isn't working -the result is an empty chart:

<!DOCTYPE html>
<html>
<head>
   <title>Google Chart Example</title>
   <script src="https://www.google.com/jsapi"></script>
   <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
   <script src="jquery.csv-0.71.min.js"></script>
  <script type='text/javascript'>
        // wait till the DOM is loaded
      $(function() {
         // grab the CSV
         $.get("post.csv", function(csvString) {
            // display the contents of the CSV
            $("#chart").html(csvString);
         });
      });

// load the visualization library from Google and set a listener
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

   function drawChart() {
   // grab the CSV
   $.get("miez.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);
    // this view can select a subset of the data at a time
  var view = new google.visualization.DataView(data);
      view.setColumns([0,1]);
  var options = {
   title: "Weight tracking",
   hAxis: {title: data.getColumnLabel(0), minValue: data.getColumnRange(0).min, maxValue: data.getColumnRange(0).max},
   vAxis: {title: data.getColumnLabel(1), minValue: data.getColumnRange(1).min, maxValue: data.getColumnRange(1).max},
   legend: 'none'
    };
  var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
chart.draw(view, options);
     });
    }
   </script>

</head>
<body>
   <div id="chart">

   </div>
</body>
</html>

The CSV is pretty simple, there is just date and weight.

Date,Weight
2014-10-25 09:58:30 +0000,86.025
2014-10-25 10:14:38 +0000,88.44836
2014-10-25 13:08:13 +0000,84.04703

Upvotes: 2

Views: 5230

Answers (2)

void
void

Reputation: 36703

The jquery-csv library provides the ability to translate a string of csv into an array to be used by google.visualization.arrayToDataTable() (their example here). To make this work, add jquery.csv.js to your server (in the example below I assume it is in the same folder as your HTML) and link to it in your <head>. The following is a simple script you can add to your <head> to get started. I assume a scatter chart, but this process works for any of the charts here. The div in which the chart is rendered has id="chart".

// load the visualization library from Google and set a listener
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

// this has to be a global function
function drawChart() {
   // grab the CSV
   $.get("example.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);

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

     // set chart options
     var options = {
        title: "A Chart from a CSV!",
        hAxis: {title: data.getColumnLabel(0), minValue: data.getColumnRange(0).min, maxValue: data.getColumnRange(0).max},
        vAxis: {title: data.getColumnLabel(1), minValue: data.getColumnRange(1).min, maxValue: data.getColumnRange(1).max},
        legend: 'none'
     };

     // create the chart object and draw it
     var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
     chart.draw(view, options);
  });
}

Upvotes: 1

Warren R.
Warren R.

Reputation: 497

I think part of your problem might be the date format you are using. According to the documentation, arrayToDataTable can not deal with DateTime format; all data must be numerical.

The solution they suggest is to create an empty table and manually add data using addColumn().

I quick test you can do is just change your csv file to be:

Date,Weight
0,86.025
1,88.44836
3,84.04703

If you get a chart, just without dates you know that was the problem.

Upvotes: 0

Related Questions