简体   繁体   中英

HighCharts, Json Format

So i'm attempting to use highcharts with the pie chart drilldown option. 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.

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.

[
 {"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. 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. 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:

$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) . Try write in double quotes ( "14.383333333333" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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