2013-03-12 91 views
0

我很努力从MySql表创建饼图。从MySQL中绘制饼图db

我的表像之下

buy_trader qty 

TKS3G 2069 
MSB1G 4417 
JKB6 4021 
FWS2 3507 
ASI1G 2578 
other 18228 

我用下面的代码生成从表中的饼图。请帮我这个

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Highcharts Example</title> 

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) { 
die('Could not connect: ' . mysql_error()); 
    } 
    mysql_select_db("offlinesurv", $con); 
    $result = mysql_query("SELECT * FROM top_buy_trades"); 
    while($row = mysql_fetch_array($result)) { 
    echo $row['buy_trader'] . "\t" . $row['qty']. "\n"; 
    } 

    mysql_close($con); 
    ?> 
    <script type="text/javascript" src="js/jquery.js"></script> 
    <script type="text/javascript"> 



$(function() { 
    var chart; 
    $(document).ready(function() { 
    chart = new Highcharts.Chart({ 
    chart: { 
    renderTo: 'container', 
    plotBackgroundColor: null, 
    plotBorderWidth: null, 
    plotShadow: false 
     }, 
    title: { 
     text: 'Top Buy Traders' 
     }, 
     tooltip: { 
     pointFormat: '{series.name}: <b>{point.percentage}%</b>', 
     percentageDecimals: 1 
     }, 
     plotOptions: { 
      pie: { 
      allowPointSelect: true, 
      cursor: 'pointer', 
      dataLabels: { 
      enabled: true, 
      color: '#000000', 
        connectorColor: 'green', 
        formatter: function() { 
         return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; 
        } 
       } 
      } 
     }, 
     series: [{ 
      type: 'pie', 
      name: 'Broker share', 
      data: [<?php echo $row ?>] 
     }] 
    }); 
}); 

}); 
    </script> 
</head> 
<body> 
<script src="js/highcharts.js"></script> 
<script src="js/exporting.js"></script> 

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

</body> 
</html> 

请帮我这个,因为我一直在努力这个从一段时间。

什么错在此代码,请

+0

我想你应该将

0

你不可错过的值是这样的。试试下面的代码,

更改while循环之中,

while($row = mysql_fetch_array($result)) { 
    $traders[] = array(
     'trader' => $row['buy_trader'], 
     'qty' => $row['qty'] 
    ); 
} 

然后改变你的脚本,

series: [{ 
      type: 'pie', 
      name: 'Broker share', 
      data: [ 
        <?php 

         foreach($traders as $trader) { 
         echo "[".$trader['trader'].",". $trader['qty']."],"; 
        } 
        ?> 
      ] 
     }] 
+0

我按照你所说的改变了,但没有运气。请帮助我 – user1793864 2013-03-13 04:20:49

+0

你会得到什么?你有什么错误吗? – 2013-03-13 04:26:42

+0

上述代码的空白页面 – user1793864 2013-03-13 04:41:15