我有一个问题。我想使用highcharts api显示饼图。数据来自MySQL数据库。我的桌子就像(这是我的表格格式):
city|area|blank
A |100 |50
B |50 |20
我的PHP代码是
<?php
include "con.php";
$id = $_GET['city'];
$result = mysqli_query($con,"SELECT area AS A , blank AS B from `table` WHERE city = '".$id."' ");
$rows['type'] = 'pie';
$rows['name'] = 'area';
//$rows['innerSize'] = '50%';
while ($r = mysqli_fetch_array($result)) {
$rows['data'][] = array($r['A'], $r['B']);
}
$rslt = array();
array_push($rslt,$rows);
print json_encode($rslt, JSON_NUMERIC_CHECK);
mysqli_close($con);
我一直在显示饼图,但我的数据是这样的(这是示例):
id|category|value
1 |area |100
1 |blank |20
2 |area |50
2 |blank |20
但正如我之前提到的关于我的表结构的那样,饼图没有显示出来。
我的js代码:
var c = $('#City :selected').text();
getAjaxData(c);
var opt = {
chart: {
renderTo: 'container1',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'final chart'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>' + this.point.name + '</b>: ' + this.y;
}
},
showInLegend: true
}
},
series: []
};
function getAjaxData(c) {
$.getJSON("file.php", {city:c},function(json) {
opt.series = json;
chart = new Highcharts.Chart(opt);
});
}
因此,您的数据采用可怕的“实体 - 属性 - 值”格式,即...... complicated。
这意味着不要像写一个好的查询
select area, blank from cities where id=1
您现在必须在所有查询中将行中的数据与行进行争论
select c1.value as area,
c2.value as blank
from cities c1
inner join cities c2 on c1.id=c2.id
where c1.category='area'
and c2.category='blank'
and c1.id=1
我建议不要使用EAV存储您的数据。
这里是使用highcharts api绘制饼图的答案。当数据是横向格式时。
$result = mysqli_query($con,"SELECT area AS A , blank AS B from `table` WHERE city = '".$id."' ");
$rows['type'] = 'pie';
$rows['name'] = 'values';
$p = 'area';//add this variable for the datalables
$a = 'blank';//add this variable for the datalables
//$rows['innerSize'] = '50%';
while ($r = mysqli_fetch_array($result)) {
$rows['data'][] = array($p, $r['A']);
$rows['data'][] = array($a, $r['B']);
}
$rslt = array();
array_push($rslt,$rows);
print json_encode($rslt, JSON_NUMERIC_CHECK);