简体   繁体   English

HighCharts,Json格式

[英]HighCharts, Json Format

So i'm attempting to use highcharts with the pie chart drilldown option. 因此,我尝试将highcharts与饼图明细选项一起使用。 Working with static data this is working perfectly. 使用静态数据可以完美地工作。 However, as I would like to use the Pie chart as a form of reporting, Ideally It needs to run with dynamic data. 但是,正如我想将饼图用作报告的一种形式一样,理想情况下,它需要与动态数据一起运行。

The top level data is made up of requests. 顶层数据由请求组成。 Each request is made up of subsequent tasks. 每个请求由后续任务组成。

This is the php I have which retrieves the tasks and requests. 这是我拥有的php,用于检索任务和请求。

foreach($getRequests as $key=> $val){
    $timeArr = explode(':', $val['duration']);
    $decTime = ($timeArr[0]) + ($timeArr[1]/60); // this is purely to convert hh:mm to decimal time

    $pieData['name'] = $val['name'];
    $pieData['y'] = $decTime;
    $pieData['drilldown'] = $key;
    $pie[]=$pieData;
    // This creates the first level of data which the $pie[] array gives the correct format, so when json_encode is applied, the data is usable


    $getTasks = $task->getReportTasks($user, $status, $key, $dateRange, $date);

    foreach($getTasks as $taskKey => $taskVal){
        $pieTasks['id']=$key;
        $pieTasks['name'] = "Tasks";
        $timeArrTask = explode(':', $taskVal['duration']);
        $decTimeTask = ($timeArrTask[0]) + ($timeArrTask[1]/60);

            $pieTasks['data'] = array($taskVal['name'], $decTimeTask);
            $pie2[] = $pieTasks;
        }
}

However by applying the same logic to tasks and using json_encode, I end up with the following. 但是,通过将相同的逻辑应用于任务并使用json_encode,我得到以下结果。

[
 {"id":25684
 ,"name":"Tasks"
 ,"data":["test task1",3]
 }
,{"id":25684
 ,"name":"Tasks"
 ,"data":["testtask2",14.383333333333]
 }
,{"id":25689
 ,"name":"Tasks"
 ,"data":["testtask3",1]}
]

But the format I need is for tasks with the same request ID, the "id" field to be contained within the same data field. 但是我需要的格式是具有相同请求ID的任务,“ id”字段包含在同一数据字段中。 Like so 像这样

[
{"id":25684
 ,"name":"Tasks"
 ,"data":[
         ["test task1",3]
        ,["testtask2",14.383333333333]
         ]
 }
,{"id":25689
 ,"name":"Tasks"
 ,"data":[
         ["testtask3",1]
         ]
}
]

where because testtask2 has the same id, it is contained within the same data field. 因为testtask2具有相同的ID,所以它包含在同一数据字段中。 I hope this makes sense and any help anyone can provide so I can structure this correctly would be greatly appreciated. 我希望这是有道理的,并且任何人都可以提供任何帮助,以便我可以正确地构建此结构,将不胜感激。

Not tested, but try to replace the last foreach with this code: 未经测试,但尝试用以下代码替换最后的foreach:

$pieTasks['id'] = $key;
$pieTasks['name'] = "Tasks";
$pieTasks['data'] = array();
foreach($getTasks as $taskKey => $taskVal){
    $timeArrTask = explode(':', $taskVal['duration']);
    $decTimeTask = ($timeArrTask[0]) + ($timeArrTask[1]/60);

    $pieTasks['data'][] = array($taskVal['name'], $decTimeTask);
}
$pie2[] = $pieTasks;

Standart JSON parser can't parse double (14.383333333333) . Standart JSON解析器无法解析double(14.383333333333)。 Try write in double quotes ( "14.383333333333" ) 尝试用双引号(“ 14.383333333333”)书写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM