3
votes

I need some help with Google API data array creation. See the details below: I have the below arrays which I am creating dynamically.

schoolYear = [ 2014,2015,];
schoolReading = [420.5,520.5,];
schoolWriting = [436.5,536.5,];
schoolSpelling = [425.1,525.1,];
schoolNumeracy = [395.5,495.5,];
schoolGramPunc = [436,536,];
schoolTotAvg = [437,537,];

I am trying to create google.visualization.arrayToDataTable like this:

for (i = 0; i < schoolTotAvg.length; i++) {
  var data = google.visualization.arrayToDataTable([
    ['Year', 'Reading', 'Numeracy', 'Grammar/Punctuation', 'Writing', 'Spelling', 'Total Average'],
    [schoolYear, schoolReading, schoolNumeracy, schoolGramPunc, schoolWriting, schoolSpelling, schoolTotAvg]
  ]);
}

I am not getting correct array when I see the page source.

Can someone please help?

--Pankaj

1
Could you please edit your question to explain the expected result and what's actually happening? If you can post a minimum workable example that would be good. - nhinkle
Thanks nhinkle.I found solution.see my answer below.However I am now facing another issue in display of google chart. The year is coming on X axis and it shows 5 point increment.Something like this : 2,014, 2014.25 , 2014.50, 2014.75,2015..How can i restrict it to only two point 2014 and 2015? - Pankaj Srivastava

1 Answers

5
votes

I found the solution :-)

var dataArray = [['Year', 'Reading', 'Numeracy', 'Grammar/Punctuation', 'Writing', 'Spelling', 'Total Average']];

for (var n = 0; n < schoolTotAvg.length; n++) {
    dataArray.push([schoolYear[n], schoolReading[n], schoolNumeracy[n], schoolGramPunc[n], schoolWriting[n], schoolSpelling[n], schoolTotAvg[n]]);
}

var data = new google.visualization.arrayToDataTable(dataArray);

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

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

chart.draw(data, options);