4

I want to use my PHP variable in Google chart,but the chart couldn't read my PHP tag. As you can see the code below I put my PHP variable in the script. (The PHP variable I have defined at top of the code and the result is correct). What's wrong with my code ? Is there any solution for this ? Do ask me for more information if needed. Thank you.

<script type="text/javascript">
  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Order', 'Amount'],
      ['Completed',     '<?php echo$completed ?>'],
      ['New',      '<?php echo$new ?>']
    ]); 

    var options = {
      title: 'Total Order ' + <?php echo$total; ?>
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }
</script>
6
  • What is the rendered code? Any errors in the console? Commented Nov 24, 2016 at 2:15
  • No, because the chart doesn't show up. But if I change the php variable in the script to Integer, the Google chart works perfectly. @chris85 Commented Nov 24, 2016 at 2:20
  • Not the chart, the source code. Is the page blank? I'd also put spaces between the echo and variable but that should break it. Commented Nov 24, 2016 at 2:21
  • Only the div for the Chart is blank, other element in the page is all fine. @chris85 Commented Nov 24, 2016 at 2:25
  • The generated JS is blank? Commented Nov 24, 2016 at 2:36

4 Answers 4

2

Working Example.

<?php 
$completed = 50;
$new = 10;
$total = 30;
?>
<html>
    <head>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">


            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawChart);
            function drawChart() {

                var data = google.visualization.arrayToDataTable([
                    ['Order', 'Amount'],
                        ['Completed', parseInt('<?php echo $completed; ?>')],
                        ['New',       parseInt('<?php echo $new; ?>')]
                ]); 

                var options = {
                    title: 'Total Order ' + <?php echo$total; ?>
                };

                    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

                    chart.draw(data, options);
                }

        </script>
    </head>

    <body>
        <div id="piechart"></div>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

2

According to docs, for Google visualization pie charts, there must be one datatype string column and one number column.

So you should parse the Amount column to integer or float before rendering the data. You can do it in php itself or in javascript as,

   var data = google.visualization.arrayToDataTable([
      ['Order', 'Amount'],
      ['Completed', parseInt('<?php echo $completed; ?>')],
      ['New',       parseInt('<?php echo $new; ?>')]
    ]); 

    var options = {
      title: 'Total Order ' +  parseInt('<?php echo $total; ?>')
    };

You can also use javascript parseFloat() instead of parseInt() if your amount values containing decimal values.

Comments

0
<span id="json-genderby"><?php echo json_encode($arrayGenderGroupBy); ?></span>

<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>

<script type="text/javascript">

    $(window).on('load', function() {
        google.charts.load('current', {'packages':['corechart']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {
            var json_gender = $("#json-genderby").text()
            
            var arrayGenderBy = [];
            var count = 0;

            $.each(JSON.parse(json_gender), function(i, item) {
                if (count == 0)
                    arrayGenderBy[count] = [item[0], item[1]];
                else 
                    arrayGenderBy[count] = [ item[0], parseInt(item[1]) ];
                count ++
            });
            

            var data    = google.visualization.arrayToDataTable(arrayGenderBy);

            var options = { title: 'Servey Questions Answered by Gender' };
            var chart   = new google.visualization.PieChart(document.getElementById('piechart'));

            chart.draw(data, options);
        }
    });
</script>

Comments

-2

You can use scripts in PHP.

But, You can't use PHP in scripts.

It won't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.