jacobappleton
jacobappleton

Reputation: 538

Google AnnotatedTimeLine DateTime JSON Error

I am having an issue when trying to build an Annotated Timeline Graph using the Google Charts API.

In the JSON, for the first "date" column, if I use:

"v": new Date(2010, 01, 01)

Then I get a JavaScript error from my page saying that I have invalid JSON.

If instead I use:

"v": "new Date(2010, 01, 01)" then I get the error TypeError: 'undefined' is not a function (evaluating 'M[y]()').

My JavaScript code is simply a modification of the example code for a Pie Graph found at: http://code.google.com/apis/chart/interactive/docs/php_example.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['annotatedtimeline']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "getData.php",
          dataType:"json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }

    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div" style="height: 200px; width:200px;"></div>
  </body>
</html>

I am aware that people have had similar problems:

http://groups.google.com/group/google-visualization-api/browse_thread/thread/4cfe7f07e5ef4bcc

http://www.mail-archive.com/[email protected]/msg02940.html

However in these threads/pages, the answer seems to be to use "v": new Date(2010, 01, 01) however this does not work for me.

I'm not sure what I am missing here.

Thanks

Upvotes: 2

Views: 2818

Answers (2)

lok
lok

Reputation: 21

well tis RAW and MESSY, but it works!!

tested on jquery 1.4.2

google.load('visualization', '1', {packages: ['annotatedtimeline']});
google.load('jquery','1.4');
window.row = Array();

$.getJSON( "https://spreadsheets.google.com/feeds/list/YOURDOCSKEYID/od6/public/values?alt=json-in-script&callback=?",
  function (data) {   
  $.each(data.feed.entry, function(i,entry) {  
    row.push([new Date(entry.gsx$date.$t),+entry.gsx$column1.$t,+entry.gsx$column2.$t]); 
  });
});

function drawVisualization() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('number', 'Column 1');
  data.addColumn('number', 'Column 2');
  data.addRows(row);

  var annotatedtimeline = new google.visualization.AnnotatedTimeLine(
      document.getElementById('visualization'));
  annotatedtimeline.draw(data, {'displayAnnotations': true});
}

google.setOnLoadCallback(drawVisualization);

Upvotes: 2

jacobappleton
jacobappleton

Reputation: 538

So I finally figured it out with a little help from the Google Visualisation API user group.

This both validates as JSON and renders correctly when using "v": "Date(2010, 01, 01)" - note the lack of the "new" keyword.

Hopefully this helps someone else in the future.

Upvotes: 5

Related Questions