0

This is my php file it contains three arrays and I am passing these three arrays to external JavaScript file called ss.js to draw pie chart. If I directly declared array in external JavaScript file it shows the pie chart but when I passing array from php it simply showing nothing. I tried but I can't figure out the error part.

code.php

<?php 
  $a = array("Apple","Orange","Grape");
  $dataArray = array("Task","Hours Per Day");
  $arr1 = array("Work","Eat","Commute","Watch TV","Sleep");
  $arr2 = array(11,2,2,2,7);
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">
  var jArray =<?php echo json_encode($dataArray); ?>;
  var jArray1 =<?php echo json_encode($arr1); ?>;
  var jArray2 =<?php echo json_encode($arr2); ?>;
</script>

<script type="text/javascript" src="ss.js">
</script>
</head>
<body>
   <div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>

ss.js

google.load("visualization", "1", {
  packages: ["corechart"]
});

function drawData() {
  for (var n = 0; n < jArray2.length; n++) {
    jArray.push([jArray1[n], parseInt(jArray2[n])]);
  }
  var data = new google.visualization.arrayToDataTable(jArray);
  var options = {
    title: 'My Daily Activities'
  };
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
}
google.setOnLoadCallback(drawData);
t.niese
  • 39,256
  • 9
  • 74
  • 101
Happy
  • 1,031
  • 10
  • 26
  • Why can't you use something [native to PHP](https://github.com/bggardner/google-visualization-php)? – tyteen4a03 Jan 14 '15 at 09:38
  • i dont know about that... – Happy Jan 14 '15 at 09:44
  • A PHP array is not the same as a JavaScript array. You're also not using an array, you're using json. Json is not an array. I suggest reading some "how to" tutorials online – user3036342 Jan 14 '15 at 09:49
  • @VijayakumarM As far as I know, a php array object is not the same as a javascript array object. did you take a view into the html source code output? – Reporter Jan 14 '15 at 09:49
  • 1
    Issue you are facing is not about passing data from PHP to JS but its more how you create the JSON. Try the solutions in these questions http://stackoverflow.com/questions/23168129/google-arraytodatatable-invalid-row-type-for-row-0 http://stackoverflow.com/questions/21333727/google-visualization-invalid-row-type-for-row-0 – Jose Antony Jan 14 '15 at 09:53
  • any other way to draw a chart? – Happy Jan 14 '15 at 10:28

0 Answers0