1

Im trying to populate a google chart with data that I'm fetching from an API at www.scb.se (The Swedish Statistics Bureau).

I'm using the following code:

  $.ajax({
  type: "POST",
  url: 'http://api.scb.se/OV0104/v1/doris/sv/ssd/START/HA/HA0103/Livs',
  data: '{"query":[{"code": "Varugrupp","selection":{"filter": "vs:VaruTjänstegrCoicopD","values":["01.1.7"]}},{"code": "ContentsCode","selection": {"filter": "item","values":["HA0103A1"]}},{"code": "Tid","selection":{"filter": "item","values": ["2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014"]}}],"response": {"format": "json"}}',
  success: function(data){
      console.log(data);
      google.charts.load('current', {'packages': ['corechart']}); 
      google.charts.setOnLoadCallback(drawChart); 

      function drawChart(){

          var jsonData = data;

          var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

          var chartData = google.visualization.arrayToDataTable(jsonData);

          var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

          chart.draw(chartData, options);
      } 
  }
});

When I "console.log(data)" I can see all the contents in the console but Charts keep telling me that data is not an array. What am I doing wrong?

Cheers!

The console log of "data":

Object {columns: Array[3], comments: Array[1], data: Array[11]}

Status418
  • 23
  • 8
  • I just noticed that I don't provide any "options" for Google Charts, but i still have the same error though. – Status418 Mar 09 '16 at 21:19
  • 1
    check in the console `typeof data`, maybe it's not an array but an object – R. Q. Mar 09 '16 at 21:19
  • typeof data "undefined" – Status418 Mar 09 '16 at 21:22
  • Change `console.log(data);` to `console.log(typeof data);`. What does that produce when you run the code? Also please add the console log of `data` to the question. – jdabrowski Mar 09 '16 at 21:24
  • Sorry I meant replace `console.log(data)` with `console.log(typeof data)` – R. Q. Mar 09 '16 at 21:24
  • This is a hoisting issue. `var data` in the `drawChart()` function shadows the other `data` variable, even if it's defined after `data` is used. You can fix it by using different names for different variables (which is a good idea anyway.) – JJJ Mar 09 '16 at 21:25
  • It says it's an object.. :/ – Status418 Mar 09 '16 at 21:26
  • Possible duplicate of [Surprised that global variable has undefined value in JavaScript](http://stackoverflow.com/questions/9085839/surprised-that-global-variable-has-undefined-value-in-javascript) – JJJ Mar 09 '16 at 21:29
  • All right so I've changed the variable names but I still get the same "Not an array" error. I realise now that it indeed is NOT an array, but an object. Is there some way to push the object into an array? – Status418 Mar 09 '16 at 21:46
  • 1
    Can you change the call to `arrayToDatatable` to the following: `arrayToDatatable([jsonData["columns"], jsonData["data]")` – R. Q. Mar 09 '16 at 21:54
  • All right, tried the above (R.Q) and it seems to be getting the data but it gives me an error "Uncaught Error: Row 0 has 22 columns, but must have 3" I think GCharts want's the data in some specific order? – Status418 Mar 10 '16 at 21:39
  • `jsonData.columns` should have the same number of elements as each row in `jsonData.data` --> but there are so many ways -- take a look at the examples just under the details section of the [DataTable Constructor](https://developers.google.com/chart/interactive/docs/reference#DataTable) -- as well as under [`arrayToDataTable()`](https://developers.google.com/chart/interactive/docs/reference#arraytodatatable) – WhiteHat Mar 11 '16 at 15:47

0 Answers0