doushihu5475 2013-10-24 18:38
浏览 47
已采纳

将json数据从PHP传递到javascript - Google Pie Chart

I'm trying to pass a json data from php after a mysql query to my javascript that loads the google charts api. Query is successful however the data format doesn't seem to be correct. Any idea on what i'm doing wrong here? I saved it as .html file and just opening it via browser. Hope that should it.

< ?php
$DB_hostname = "localhost";
$DB_Name = "root";
$DB_pass = "root";
$tbl_name="tblname"; // Table name 




$con = mysql_connect($DB_Hostname,$DB_Name,$DB_pass) or die(mysql_error());

mysql_select_db("dbname", $con);
$sql="SELECT *  FROM $tbl_name WHERE is86 = 1";
$result=mysql_query($sql);

$count1 = mysql_num_rows($result);


$sql="SELECT *  FROM $tbl_name WHERE is86 = 0";
$result=mysql_query($sql);

$count2 = mysql_num_rows($result);
$data[0] = array("Changes","count1");
$data[1] = array("Apps with IA Architecture",.$count1.);
$data[2] = array("Apps with no IA Architecture",.$count2.); 

$data = json_encode($data);
? >


<html>
  <head>
    <!--Load the AJAX API-->
    <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">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

   // Set a callback to run when the Google Visualization API is loaded.
   google.setOnLoadCallback(drawChart);

   function drawChart() {
     var data = new google.visualization.DataTable(<?=$data?>);


    var options = {
        title: 'Architecture Changes',
        is3D: 'true',
        width: 800,
        height: 600
      };

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

   <body>
   <!--Div that will hold the pie chart-->
  <div id="chart_div"></div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • douyun7718 2013-10-24 19:02
    关注

    Your data format is incorrect for using the DataTable constructor like that, but you should be able to use the arrayToDataTable method:

    var data = google.visualization.arrayToDataTable(<?=$data?>);
    

    Also, you shouldn't include the .'s before and after the $count variables when adding them to the arrays:

    $data[0] = array("Changes", "count1");
    $data[1] = array("Apps with IA Architecture", $count1);
    $data[2] = array("Apps with no IA Architecture", $count2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号