2011-09-19 7 views
0

私は "トラック" と呼ばれるテーブルを持って、それは次のようになります。統計情報を取得し、チャートにそれを表示 - PHP/HighCharts

  1. ID、
  2. 名、
  3. URL
  4. ヒット

私はhighchartsから、円グラフを持って、コードは次のようになります。

var chart; 
$(document).ready(function() { 
    chart = new Highcharts.Chart({ 
     chart: { 
     renderTo: 'chart', 
     plotBackgroundColor: null, 
     plotBorderWidth: null, 
     plotShadow: false 
     }, 
     title: { 
     text: 'Where users came from, total' 
     }, 
     tooltip: { 
     formatter: function() { 
      return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; 
     } 
     }, 
     plotOptions: { 
     pie: { 
      allowPointSelect: true, 
      cursor: 'pointer', 
      dataLabels: { 
       enabled: false 
      }, 
      showInLegend: true 
     } 
     }, 
     credits: { 
     enabled: false 
     }, 
     series: [{ 
     type: 'pie', 
     name: 'Browser share', 
     data: [ 
      ['Firefox.com', 45.0], 
      ['Default.com',  26.8], 
      ['MSN',  12.8], 
      ['Google.com', 8.5], 
      ['Yahoo.com',  6.2], 
      ['Others', 0.7] 
     ] 
     }] 
    }); 
}); 

私の質問ですが、私は「トラック」テーブルからデータ(ヒット)、および(名)など円グラフの出力を得ることができる方法:

「名前」、「ヒット」を

最後に、ヒット数を%に変換します。

+0

チャートを再描画する予定ですか?(ページの読み込み時に)一度生成すれば十分ですか? – roselan

+0

はい、それで十分です。 –

答えて

0

最も簡単な方法は、MySQLからデータを照会して、

[ 
     ['Firefox.com', 45.0], 
     ['Default.com',  26.8], 
     ['MSN',  12.8], 
     ['Google.com', 8.5], 
     ['Yahoo.com',  6.2], 
     ['Others', 0.7] 
    ] 

と同じ、それを並べ替えることであるあなただけのページの読み込みにチャートを表示したい場合に必要です。 JSONを使用することもできます。

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

     $sql = 'select sum(hits) from tracks'; 
     $result = mysql_query($sql); 
     $row = mysql_fetch_row($result); 
     $totalHits = $row[0]; 

     $sql = 'select name, hits from tracks order by hits desc'; 
     $result = mysql_query($sql); 

     $i = 0; 
     $totalOther = 0; 
     $maxSlices = 5; 
     $minPercent = 10; 

     while ($row = mysql_fetch_row($result)) 
     { 
      $percent = row[1]/$totalHits * 100; 

      if (++$i <= $maxSlices && $percent >= $minPercent) 
      { 
       echo json_encode($row); 
      } 
      else 
      { 
       $totalOther += $row[1]; 
      } 
     } 
     if ($totalOther > 0) echo json_encode(array('Other', $totalOther)); 
     ?> 
     ] 
     }] 

あなたはAJAX thourghそれをリフレッシュしたい場合は...

... 
series: [{ 
     type: 'pie', 
     name: 'Browser share', 
     data: getData(function(result) {return result;}), 
... 

とPHPで

は、あなたが新しいファイル( 'file.php')をしなければなりません第1のコードブロックに示されるコードと、

+0

これは、1行のデータのみを表示できます。ヒット数が0より大きい行を追加するたびに、グラフは表示されません。ソースコードは次のようになります:data:[ ["Superior(Edited)f"、 "53"] ["その他"、 "]" } –

+0

json_encodeを使いこなす。 –

関連する問題