Michael_Roempel Posted March 1, 2020 Share Posted March 1, 2020 Hello, I am new to pHp and javascript languages and I am trying to do some math calculations in pHp, then display the results with google charts. However, I got stuck with inserting data from pHp using json_encode. It works for the most simple array, with using 2 values. When using array of arrays, it does not work. I believe it could be a syntax errors with all those brackets but I could not figure it out. Thanks for any ideas! <?php // some arrays with dim 2x2 $testarr0 = array( 0.1, 2.5); $testarr = array( 1.0, 3.5); $testarr = array( $testarr0, $testarr ) ; // this is how it looks in pHp $json = json_encode($testarr); echo($json); echo '<br/>'; echo json_encode($json); ?> <html> <head> <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 data = new google.visualization.DataTable(); data.addColumn('number', 'x values'); data.addColumn('number', 'y1 '); //var dataArray = <?php echo json_encode($testarr);?>; //document.body.innerHTML = " Data Array with json: " + dataArray ; // defining array here works well /* data.addRows( [ [0.1 ,2.5], [1.0 ,3.5] ] ); */ // this here does not work: data.addRow( <?php echo json_encode($testarr);?> ); // some options for the chart var options = { chart: { title: 'Points over time', }, width: 600, height: 400, axes: { x: { 0: {side: 'top'} } } }; var chart = new google.charts.Line(document.getElementById('line_top_x')); chart.draw(data, google.charts.Line.convertOptions(options)); } </script> </head> <body> <div id="line_top_x" style="width: 900px; height: 500px"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/310165-json_encode-for-google-charts/ Share on other sites More sharing options...
requinix Posted March 1, 2020 Share Posted March 1, 2020 It's "PHP". All caps. You're calling data.addRows in the version that works and data.addRow in the version that does not. Quote Link to comment https://forums.phpfreaks.com/topic/310165-json_encode-for-google-charts/#findComment-1575023 Share on other sites More sharing options...
Michael_Roempel Posted March 2, 2020 Author Share Posted March 2, 2020 Many thanks for your quick help. That was an easy fix, if you know how to; I guess I have a long way ahead of me. Quote Link to comment https://forums.phpfreaks.com/topic/310165-json_encode-for-google-charts/#findComment-1575024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.