Array for Google Charts

I am trying to pass an array of numbers to google charts, this array is pulled by this code from a div with class .likescount

var i, $mvar = $('.likescount'); // using the $ prefix to use the "jQuery wrapped var" convention function logit( string ) { var text = document.createTextNode( string ); $('.alllikesinrow').append(text); $('.alllikesinrow').append("<br>"); } logit($mvar.length); for (i=0; i<$mvar.length; i++) { logit($mvar.eq(i).html()); } 

The array works because I use append to print it and its work, the hard part is to pass this data to data.addRows, heres the complete code that I use worked around it, but never received it, I follow this guy that got it how to add data to google charts using javascript? but without luck any help wold would be great.

 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['line']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var i, $mvar = $('.likescount'); // using the $ prefix to use the "jQuery wrapped var" convention x = []; for (i=0; i<$mvar.length; i++ ) { x.push( ['1', parseFloat($($mvar[i]).text())] ); } var data = new google.visualization.DataTable(); data.addColumn('string', 'Day'); data.addColumn('number', 'Likes'); data.addRows(x); var options = { chart: { title: 'Likes vs Comentarios', subtitle: 'Data Magnus by Optimum' }, height: 500 }; var chart = new google.charts.Line(document.getElementById('curve_chart')); chart.draw(data, google.charts.Line.convertOptions(options), {pointSize: 1}); } </script> 

Thanks Rodrigo

EDIT: Edited by a working press of WhiteHat

+5
source share
2 answers

addRows accepts an array of arrays that you build here ...

 x = []; for (i=0; i<$mvar.length; i++) { logit($mvar.eq(i).html()); x.push( ['August', $mvar[i]] ); } 

as such, just use x for addRows ...

 var data = new google.visualization.DataTable(); data.addColumn('string', 'Day'); data.addColumn('number', 'Likes'); data.addRows(x); 

EDIT

not sure if $('.likescount') contains,
but if each item is numbered, try ...

 x.push( ['August', parseFloat($($mvar[i]).text())] ); 
+1
source

try using google.visualization.arrayToDataTable (array)

the array contains an array of row values โ€‹โ€‹displayed in the chart

+1
source

Source: https://habr.com/ru/post/1270840/


All Articles