获取统计数据并在图表中显示- PHP/HighCharts
Get stats and show it in chart - PHP/HighCharts
我有一个名为"tracks"的表,它看起来像这样:
- id,
- ,
- li>点击
我有一个饼状图,来自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]
]
}]
});
});
我的问题是,我如何从"tracks"表(hits)和(name)中获取数据并将其输出到饼状图中,如:
"名称","打"
然后,最后,将# of hits转换为%。
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刷新它…
...
series: [{
type: 'pie',
name: 'Browser share',
data: getData(function(result) {return result;}),
...
和
function getData(setDataCallback)
{
$.getJSON('file.php', args, function(result) { // use of jquery for clarity.
setDataCallback(result);
});
}
在PHP中,您必须使用第一个代码块中显示的代码创建一个新文件('file. PHP ');
最简单的方法是从mySql中查询数据,然后像
一样排序[
['Firefox.com', 45.0],
['Default.com', 26.8],
['MSN', 12.8],
['Google.com', 8.5],
['Yahoo.com', 6.2],
['Others', 0.7]
]
需要,如果你想显示图表只是在页面加载。
相关文章:
- 如何使用JavaScript函数在网页中显示PHP变量
- 如何避免试图用php+jquery显示php起始页的无限循环
- 在Javascript代码中显示PHP数组变量
- 使用jquery和javascript显示PHP中的变量
- 显示php中的函数javascript
- 显示php中的加载百分比文本
- 如何在javascript中显示php
- 如何在刷新时显示PHP表单中的随机文本和图像
- 无法在javascript中显示php变量
- 按钮,显示 PHP 中表的选定行的完整信息
- 在另一个 PHP 文件的 JavaScript 中显示 PHP 文件内容
- 如果禁用了 js,如何显示 php 生成的网站
- 谷歌图表没有显示 PHP 中的 Ajax 调用
- 如何在表单旁边显示 php 表单验证错误
- 使用 ajax 显示 php 响应文本
- 在 Javascript 警报框中的新行上显示 PHP 数组的每个条目
- jquery显示php-json编码数组的值时出现未定义错误
- 通过jquery;显示php文件中的记录;
- 如何在基于JS的Google图表中显示PHP数组值
- 以 js 格式显示 php 数组