2013-03-02 46 views
-2

創建餅圖,我遇到了下列數據高圖表

Buy_client Amount 

ABC   6597 
XYZ   4479 
PQR   2075 
qqq   1706 
ttt   1030 
Other   450 

我想表明在高圖表餅圖以下數據在MySQL表。有人請幫助我。我是新來的PHP和HighCharts

問題1:我需要從這個數據

問題2創建一個餅圖:我使用HighCharts這一點。有人請幫我做這HighCharts


我用下面的代碼來做到這一點,但它並不顯示一個餅圖中的信息,但只是在Web瀏覽器中顯示的數據

<!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); 

$my_data = mysql_query("SELECT * FROM top_buy_clients"); 

while($row = mysql_fetch_array($my_data)) { 
echo $row['buy_client'] . "\t" . $row['qty']. "\n"; 
} 


?> 

<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: 'Browser market shares at a specific website, 2010' 
      }, 
    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: 'Browser 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> 

回答

1

在你的PHP ...,你做的數據庫查詢.... while循環,你需要構建和字符串,並在循環中的字符串應該看起來像結束:

$my_data = "['ABC', 6597], 
       ['XYZ', 4479], 
       ['PQR', 2075], 
       ['qqq', 1706], 
       ['ttt', 1030], 
       ['Others', 450] "; 

然後在你的ja vascript文件,你應該有這樣的一條線

series: [{ 
      type: 'pie', 
      name: 'Pie Data', 
      data: [ 
       <?php echo $my_data; ?> 
      ] 
     }] 

這很簡單。

Saludos。