0

Here is what I'm trying to do.

I'm trying to load a bunch of Google pie charts in a PHP loop based with the following code:

<?php
 foreach ($currentTeamLeaders as $key=> $currentTeamLeader) {
     $chartId = 'chart-'.$key;
?>
     <script>
         google.charts.load("current", {packages:["corechart"]});
         google.charts.setOnLoadCallback(drawChart);
    
         function drawChart() {

                         var data = google.visualization.arrayToDataTable([
                            ['Task', 'Hours per Day'],
                            ['Work',     11],
                            ['Eat',      2],
                            ['Commute',  2],
                            ['Watch TV', 2],
                            ['Sleep',    7]
                         ]);

                         var options = {
                            title: 'My Daily Activities',
                            chartArea:{right:0,top:0,width:"90%",height:"100%" },
                            height: 150,
                            width: 250,
                         };

                        var chart = new google.visualization.PieChart(document.getElementById(<?php echo $chartId ?>));
                        chart.draw(data, options);

                    }

        </script>

        <div id="<?php echo $chartId ?>" style="width: 300px; height: 300px;"></div>

     <?php
         }
     ?>

But for some reason, I'm getting an Uncaught Error: Container Is Not Defined despite having the div defined.

Thank you

Kevin
  • 159
  • 1
  • 11
  • 1
    You will need quotes around the string output by `echo $chartId`. Here's the full thing: `var chart = new google.visualization.PieChart(document.getElementById(''));` In future, when you get errors like this, check the source output in devtools. – Rory McCrossan May 24 '21 at 19:04
  • 1
    Thank you.... That did the trick. – Kevin May 24 '21 at 19:05
  • The business logic is questionable. What's the point in loading the JS over and over again? – Martin Zeitler May 24 '21 at 19:10

1 Answers1

0

Resolved:

I needed to add quotes around the following the PHP echo statement.

Kevin
  • 159
  • 1
  • 11