2011-09-19 75 views
0

我有一个名为 “轨道” 表,它看起来像这样:获取统计数据,并显示在图表 - PHP/HighCharts

  1. ID,
  2. 名,
  3. 网址
  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)); 
     ?> 
     ] 
     }] 
如果你想刷新thourgh阿贾克斯

...

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

在PHP你必须做一个新的文件( 'file.php')与在第一个代码块中显示的代码;

+0

这只能显示一行的数据。每当我添加大于0的命中行时,图表就不会显示。源代码如下所示:data:[ [“Superior(Edited)f”,“53”] [“sdafdsafdsafsf”,“24”] [“Other”,3]] }] –

+0

我觉得它搞乱了json_encode。 –

相关问题