gpt4 book ai didi

php - 获取统计数据并在图表中显示 - PHP/HighCharts

转载 作者:行者123 更新时间:2023-12-02 20:05:33 25 4
gpt4 key购买 nike

我有一个名为“tracks”的表,它看起来像这样:

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

我的问题是,如何从“轨道”表(点击数)和(名称)中获取数据并将其输出在饼图中,如下所示:

'名称','点击数'

最后,将点击次数转换为 %。

最佳答案

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

关于php - 获取统计数据并在图表中显示 - PHP/HighCharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com