我正在尝试产生嵌套的json
类似nvd3 StackedAreaChart
用途:
[
{
"key" : "North America" ,
"values" : [ [ 1025409600000 , 23.041422681023] , [ 1028088000000 , 19.854291255832] ]
},
{
"key" : "Africa" ,
"values" : [ [ 1025409600000 , 7.9356392949025] , [ 1028088000000 , 7.4514668527298] ]
},
]
来源:http ://plnkr.co/edit/CIGW0o?p=preview
我想使用我的数据库中的数据。
我怎么能做到那种json
?我对嵌套不太熟悉,而且json
我在它们的结构中注意到的一件事json
是对象中的值values
是纯整数。它不像"1025409600000"
. 当我尝试引用它时,图表无法正确读取数据。
问题
如何产生嵌套
json
的类似nvd3
用途?我已经进行了一些研究,但没有任何反应。我发现了一些我猜它会的东西,但无法让它工作。这里和这个也是。unquote
嵌套json
结构的值是否可能?如果是,怎么做?
这是我目前正在做的事情:
<?php
require_once('conn.php');
$sql = "SELECT ua.user_id,(UNIX_TIMESTAMP(dt.transac_date)*1000) AS transac_date,
CONCAT(ui.fname,' ',ui.lname) AS fullname,
SUM((dt.item_price - dt.item_srp) * dt.sold) as profit,
SUM((dt.item_price) * dt.sold) as total_sales
FROM dsp_transactions dt
INNER JOIN user_acct ua ON dt.user_id=ua.user_id
INNER JOIN user_info ui ON ua.ui_id=ui.ui_id
GROUP BY ua.user_id";
$qry = $con->query($sql);
$data = array();
if($qry->num_rows > 0) {
while($row = $qry->fetch_object()) {
$data[] = array(
'key' => $row->fullname,
'values' => $row->user_id
);
}
} else {
$data[] = null;
}
$con->close();
echo json_encode($data);
?>
这给了我这个价值:
[{"key":"Juan Dela Cruz","values":["1461772800000","5665.00"]},{"key":"Maria Gonzales","values":["1461772800000","275.00"]},{"key":"Apolinario Mabini","values":["1461772800000","100.00"]}]
提前致谢 :)
编辑
有关更多信息,我希望发生这样的事情:
dsp | sales | profit | date
--------------+--------------+-------------+--------------
Juan | 500 | 100 | 04/24/2016
--------------+--------------+-------------+--------------
Maria | 600 | 200 | 04/24/2016
--------------+--------------+-------------+--------------
Apolinario | 700 | 300 | 04/24/2016
--------------+--------------+-------------+--------------
Juan | 550 | 150 | 04/25/2016
将以这样的json
格式返回
[
{
"key" : "Juan",
"values" : [ ["04/24/2016", "500"], ["04/25/2016", "550"] ] // "values" loop twice because "juan" has two sales
},
{
"key" : "Maria",
"values" : [ ["04/24/2016", "600"] ]
},
{
"key" : "Apolinario",
"values" : [ ["04/24/2016", "700"] ]
}
]