Gauthier
Gauthier

Reputation: 1256

Dynamic array for googlechart json javascript

I want to make a line chart whith googlechart ( https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart ). But i'm not able to format the array for the chart.

The array must looks like this :

var data = google.visualization.arrayToDataTable([
  ['Year', 'Sales', 'Expenses'],
  ['2004',  1000,      400],
  ['2005',  1170,      460],
  ['2006',  660,       1120],
  ['2007',  1030,      540]
]);

And I'm trying to make it that way :

var data = google.visualization.arrayToDataTable([['Year', 'Marque1', 'Marque2', 'Marque3', 'Marque4']]);
var i=1;
msg.forEach(function(entry) {               
    data.addRows([
      ['2004',  i-1,      0,      i,      6],
      ['2004',  i,      0,      i,      6],
      ['2004',  i+1,      0,      i,      6]
    ]);                 
    i++;
});

It's not the real values but i'll set them when i'll be able to make an array.

I have this error in the browser console when I run :

Uncaught Error: Type mismatch. Value 0 does not match type string in column index 1 

When I put some strings instead, google send me a error message instead of the chart :

Data column(s) for axis #0 cannot be of type string

Upvotes: 0

Views: 248

Answers (1)

Joe
Joe

Reputation: 353

I don't know if I got what you mean, but try this:

var rows = new Array();
rows.push(new Array('Year', 'Marque1', 'Marque2', 'Marque3', 'Marque4'));
var i = 1;
msg.forEach(function(entry) {
    rows.push(new Array(5, 5, 5, 5, 5)); // replace it with your values
}
var data = google.visualization.arrayToDataTable(rows);

Upvotes: 2

Related Questions