dssk35460 2015-11-08 06:32
浏览 44
已采纳

谷歌饼图+ PHP + mySQL

Thanks for mbelton. My code works good now, and I also modified the following code I post.

I'm trying to use Google Pie chart to perform the year of sales. First I have HTML file which to let user choose the year they wish to review; Second, based on the $year be selected, I code PHP to connect to mysql to grab related sales data; Third I create a pieData table to store the String Quarter and double sales number; and then JavaScript to get pie char. I run the code doesn't show any errors but I don't see pie chart showing up. Can please someone tell me where to modify to make it work? Thanks.

 $year = $_POST['year'];  
 $mysqli = mysqli_connect("root", "account", "passwd", "testDB")
      or die(mysqli_error());
 $query = "SELECT Q1,Q2,Q3,Q4 from sales where year LIKE '$year'";
 $result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));

 /*get the values of each quarter, and store in new table:pieData.
 * if user choose year of 2012, then the new table should look like:
 * Quarter     Number
 * ------------------
 * Q1         127.24
 * Q2         106.54
 * Q3          88.04
 * Q4         120.89
*/
 while ($info = mysqli_fetch_array($result)) {
               $Q1 = $info['Q1'];
               $Q2 = $info['Q2'];
               $Q3 = $info['Q3'];
               $Q4 = $info['Q4'];

            }

 $pieData = array(
                  array('Quarter', 'Number'),
                  array('Q1', (double)$Q1),
                  array('Q2', (double)$Q2),
                  array('Q3', (double)$Q3),
                  array('Q4', (double)$Q4)
    );

 $jsonTable = json_encode($pieData);

 <script type="text/javascript" src="http://www.google.com/jsapi"</script>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
 <script type="text/javascript">
        //load package
        google.load("visualization", "1", {packages: ["corechart"]});
        google.setOnLoadCallback(drawChart);

        function drawChart() {
            // Create and populate the data table.
            var data = google.visualization.arrayToDataTable(
                <?php echo $jsonTable; ?>

            );


            var options = {
                title:"Sales Pie"
            };

            // Create and draw the visualization.
            var chart = new google.visualization.PieChart(document.getElementById("piechart"));
            chart.draw(data, options);
        }

    </script>
  • 写回答

1条回答 默认 最新

  • dqask02082 2015-11-08 08:54
    关注

    I think error in your code is in opening/closing <script tag and in .arrayToDataTable you need to remove square brackets [ ]

    Change code to:

    <?php
    
     $Q1 = 127.24;
     $Q2 = 106.54;
     $Q3 = 88.04;
     $Q4 = 120.89;
    
     $pieData = array(
                  array('Quarter', 'Number'),
                  array('Q1', (double)$Q1),
                  array('Q2', (double)$Q2),
                  array('Q3', (double)$Q3),
                  array('Q4', (double)$Q4)
    );
    
     $jsonTable = json_encode($pieData);
    ?>  
    
    <html>
      <head>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
    
        var data = google.visualization.arrayToDataTable(
            <?php  echo $jsonTable; ?>
        );
    
        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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化