开发者

Get stats and show it in chart - PHP/HighCharts

I have a table called "tracks",it looks like this:

  1. id,
  2. name,
  3. url
  4. hits

I have a pie charts, from highcharts, the code looks like this:

 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开发者_Python百科'
      },
      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]
         ]
      }]
   });
});

My question is, how can I get data from the "tracks" table (hits), and the (name) and output it in the pie chart like:

'name','hits'

And then, at last, convert # of hits, to %.


 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));
         ?>
         ]
      }]

if you want to refresh it thourgh ajax...

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

and

function getData(setDataCallback)
{
    $.getJSON('file.php', args, function(result)  {  // use of jquery for clarity.
        setDataCallback(result);
    });
}

in php you have to do a new file ('file.php') with the code shown in the first code block;


Simplest way is to query data from mySql and then sort it same as

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

Requires if you want to show chart just on page loading. You can also use JSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜