Using PHP to Populate Google Charts Data Breaks Interactivity

I am currently using Google Charts with PHP to populate the data but the interactivity of the chart ceases to work and I don't know why.

The code is as follows:

<?php
// Connect to DB and execute while loop to get values
...
...

echo '<div id="chart_div"></div>';

?>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Review Score');
    data.addColumn('number', 'Number of Reviews');
    data.addRows([
      ['1 Star Reviews', <?php echo $one; ?>],
      ['2 Star Reviews', <?php echo $two; ?>],
      ['3 Star Reviews', <?php echo $three; ?>],
      ['4 Star Reviews', <?php echo $four; ?>],
      ['5 Star Reviews', <?php echo $five; ?>]
    ]);

    // Set chart options
    var options = {'title':'Breakdown of Review Scores',
                   'is3D':true,
                   'width':600,
                   'height':400};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

Even if I move the chart_div to after the javascript it doesn't work.

Incase anyone asks the the js isn't in the head tags it's because I am also displaying a table populated with the results of the sql query and calculating totals based off those results for use in the chart.

I have since found this page but don't see why that would alter anything: https://developers.google.com/chart/interactive/docs/php_example

Answers


I meant to update a lot sooner but the problem lay elsewhere - Google Charts works perfectly fine when using php to populate the chart data.


Need Your Help

Eclipse RCP: Actions vs Commands - would like an update

java eclipse eclipse-rcp

I know this question has been asked before but it was in 2009 and I haven't found anything more recent either on the web. I was wondering if the answer in that still holds or can it be updated?

How to get wordpress custom fields rss feed into zend?

wordpress zend-framework rss

My web site is based on zend framework that have wordpress blog. I want to get new post details in the site. Using zend Zend_Feed_Reader function I can get title, link, description ... But there is...