javascript - php variable to google charts api no JSON

376

I'm trying to pass a few PHP variables into a Google Chart, and I cannot seem to get the syntax right. I don't want to invoke JSON (need I?).

Stock example: This works:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

    google.load('visualization', '1.0', {'packages':['corechart']});

    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Number');
        data.addColumn('number', 'Frequency');
    data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1], 
            ['Zucchini', 1],
            ['Pepperoni', 2]
        ]);
        var options = {
            'title':'Title',
            'width':400,
            'height':300
        };

    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}
</script>

However, this does not (assume all the same surrounding code)

  function drawChart() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Number');
  data.addColumn('number', 'Frequency');
     data.addRows([
    ['Mushrooms', <?php echo $phpvariable ?>],
    ['Onions', 1],
    ['Olives', 1], 
    ['Zucchini', 1],
    ['Pepperoni', 2]
  ]);

My thoughts: Positioning of the calculation of$phpvariable is wrong? or do I just need to use JSON somehow, and PHP variables cannot be passed?

People are also looking for solutions to the problem: Failing to query mysql using PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.