我正在尝试使用 JSON 数据创建条形图,并且需要将格式更改为月份系列,但我对如何做到这一点感到困惑。请帮助我如何解决这个问题。
当前 JSON 数据:
{ “数据”: [
{
“名称”:“一月”,
“设施”:“讨论室”,
“价值”:22
},
{
“名称”:“一月”,
“设施”:“卡雷尔室”,
“价值”:102
},
{
“名称”:“一月”,
“设施”:“储物柜”,
“价值”:5
},
{
“名称”:“二月”,
“设施”:“讨论室”,
“价值”:86
},
{
“名称”:“二月”,
“设施”:“卡雷尔室”,
“价值”:155
},
{
“名称”:“Mac”,
“设施”:“卡雷尔室”,
“价值”:224
},
{
“名称”:“Mac”,
“设施”:“储物柜”,
“价值”:3
},
]
}
所需的 JSON 数据:
{ “数据”: [
{
“名称”:“一月”,
“系列”:[
{ "name":"讨论室","value": 22},
{ "name": "Carrel Room", "value": 102},
{“名称”:“储物柜”,“价值”:5},
]
“名称”:“二月”,
“系列”:[
{ "name":"讨论室","value": 86},
{ "name": "Carrel Room", "value": 155},
{“名称”:“储物柜”,“价值”:0},
]
“名称”:“三月”,
“系列”:[
{ "name":"讨论室","value": 0},
{ "name": "Carrel Room", "value": 224},
{“名称”:“储物柜”,“价值”:3},
]
]
}
我的代码:
$db = mysqli_connect($host, $user, $pass, $database) or die("you did not connect");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type');
$facility =($_GET['year']);
$query = "select monthname(datetime) as 'name',
case
when all_items.itype = '127' then 'Discussion Room'
when all_items.itype = '126' then 'Carrel Room'
when all_items.itype = '121' then 'Locker'
else '0'
end as 'facility',
count(*) AS 'value'
from statistics
left join (
select itemnumber, itype from deleteditems
union
select itemnumber, itype from items
) as all_items USING (itemnumber)
where all_items.itype in (127, 126, 121) and
statistics.type = 'issue' and
year(statistics.datetime) = '$facility'
group by month(datetime), all_items.itype desc";
$result = mysqli_query($db, $query)or die(mysqli_error());
$response = array();
$posts = array();
弑天下
素胚勾勒不出你