Google Charts 饼图显示其他 100%

<html>

  <head>

    <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([

          ['Task', 'Hours per Day'],


          <?php

            $query = "SELECT name, score FROM `Users` order by score desc limit 5";

            $result = mysqli_query($conn, $query);

            if ($result->num_rows > 0) {

              while($row = mysqli_fetch_array($result))

              {

                echo "['".$row['name']."', '".$row['score']."'],";

              } 

            }

          ?>


        ]);


        var options = {

          title: 'My Daily Activities'

        };


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


        chart.draw(data, options);

      }

    </script>

  </head>

  <body>

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

  </body>

</html>

我的代码有什么问题吗?我读到我的值存储为字符串,以便图表显示 100% 其他。


我该如何让它发挥作用?我尝试使用(int)$varPHP 函数,但这还不能使它工作..


              while($row = mysqli_fetch_array($result))

              {

                $score = $row['score'];

                $int = (int)$score;

                echo "['".$row['name']."', '".$row['score']."'],";

              } 

            }


慕侠2389804
浏览 48回答 1
1回答

慕的地6264312

不要自己生成json。json_encode 始终使用:<?php$query = "SELECT name, score FROM `Users` order by score desc limit 5";$result = mysqli_query($conn, $query);$chartData = [&nbsp; &nbsp; ['Task', 'Hours per Day'],];if ($result->num_rows > 0) {&nbsp; &nbsp; while($row = mysqli_fetch_array($result))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $chartData[] = [$row['name'], $row['score']];&nbsp; &nbsp; }&nbsp;}?><script type="text/javascript">&nbsp; google.charts.load('current', {'packages':['corechart']});&nbsp; google.charts.setOnLoadCallback(drawChart);&nbsp; function drawChart() {&nbsp; &nbsp; var data = google.visualization.arrayToDataTable(<?php echo json_encode($chartData)?>);&nbsp; &nbsp; var options = {&nbsp; &nbsp; &nbsp; title: 'My Daily Activities'&nbsp; &nbsp; };&nbsp; &nbsp; var chart = new google.visualization.PieChart(document.getElementById('piechart'));&nbsp; &nbsp; chart.draw(data, options);&nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP