我有一个查询。我想使用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);
});
}
答案 0 :(得分:0)
因此,您的数据采用的是可怕的“实体属性值”格式,即... 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来存储您的数据。
答案 1 :(得分:0)
这是使用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);