JavaScript - Insert PHP multidimensional array into Javascript class in Google Charts -
i'm using google charts tutorial on how convert array graph.
the data in tutorial in function. want insert own data function php array. managed convert php array javascript array. below code:
$jsarray = array(); foreach($movingaverages $movingaverage) { $jsarray[] = array((int) $movingaverage['unix'], (int) $movingaverage['closing-prices']); }
below sample code:
array ( [0] => array ( [0] => 1505040240 [1] => 3452 ) [1] => array ( [0] => 1505040300 [1] => 3451 ) [2] => array ( [0] => 1505040360 [1] => 3446 ) [3] => array ( [0] => 1505040420 [1] => 3449 )
this current javascript code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> var js_array = <?php echo json_encode($jsarray);?>; alert(js_array); google.charts.load('current', {'packages':['corechart']}); google.charts.setonloadcallback(drawchart); function drawchart() { var data = google.visualization.arraytodatatable([ ['time', 'closing prices'], [js_array] ]); var options = { title: 'btc-eur', curvetype: 'function', legend: { position: 'top' } }; var chart = new google.visualization.linechart(document.getelementbyid('curve_chart')); chart.draw(data, options); } </script> </head> <body> <div id="curve_chart" style="width: 100%; height: 500px"></div> </body> </html>
when call alert(js_array);
array called.
i don't know why chart not being displayed. i'm not sure how insert js_array
function drawchart()
. first time i'm using javascript. thank help.
you're adding 1 more dimension array.
var data = google.visualization.arraytodatatable([ ['time', 'closing prices'], js_array ]);
update:
js_array.unshift(['time', 'closing prices']); var data = google.visualization.arraytodatatable(js_array);
Comments
Post a Comment