我正在使用 Highcharts制作气泡图。这是我的数据示例:
name | price | quantity | count
--------+-------+----------+-------
Female | 2 | 3 | 5
Female | 3 | 12 | 10
Female | 5 | 6 | 15
Female | 1 | 7 | 25
Male | 3 | 5 | 7
Male | 2 | 9 | 11
Male | 5 | 7 | 23
Male | 4 | 4 | 14
我正在使用 PHP 来查询数据并编码为 JSON:
$query = "SELECT name, price, quantity, count FROM sales WHERE id = $1";
$result = pg_prepare($db, "report", $query);
$result = pg_execute($db, "report", array($ID));
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
$response['xdata'][$row['name']]['x'][] = $row['price'];
$response['xdata'][$row['name']]['y'][] = $row['quantity'];
$response['xdata'][$row['name']]['radius'][] = $row['count'];
}
echo json_encode($response);
但是,为了正确绘制图形,所需的 JSON 格式如下:
series: [{
name: 'Female',
marker:{
symbol:'circle',
fillColor:'rgba(24,90,169,.5)',
lineColor:'rgba(24,90,169,.75)',
lineWidth:1,
color:'rgba(24,90,169,1)',
states:{
hover:{
enabled:false
}
}
},
data: [{x:2,y:3,marker:{radius:5}},
{x:3,y:12,marker:{radius:10}},
{x:5,y:6,marker:{radius:15}},
{x:1,y:7,marker:{radius:25}}]
},{
name: 'Male',
marker:{
symbol:'circle',
fillColor:'rgba(238,46,47,.5)',
lineColor:'rgba(238,46,47,.75)',
lineWidth:1,
color:'rgba(238,46,47,1)',
states:{
hover:{
enabled:false
}
}
},
data: [{x:3,y:5,marker:{radius:7}},
{x:2,y:9,marker:{radius:11}},
{x:5,y:7,marker:{radius:23}},
{x:4,y:4,marker:{radius:14}}]
}]
我的问题是,如何$query
在 PHP 中正确处理以获得所需的 JSON 格式,并将其传递给类似的series
东西optionsBubble.series = data.xdata
?非常感谢!