Jump to content

Graphing number of people from two times


VrX
Go to solution Solved by Barand,

Recommended Posts

Not 100% sure where to start on this one...

 

Basically I will have a table containing shift start / end time and number of people working e.g.

 

Shift Start | Shift End | Supervisors | Staff

1000 | 2000 | 1 | 5

1200 | 2200 | 1 | 3

etc...

 

(will be other bits such as date but left out as not important atm)

 

What I would then like to do is a simple line / bar graph showing number of workers over a 24 hour period with 0-23 across bottom and then count running across.

 

So with above example for supervisors it will be 0 for 0-9, 1 for 10 & 11, 2 for 12-19, etc...

 

Can anyone think of a simple way of doing this? (also there will be night shifts to make things a bit more akward).

 

EDIT:

 

Forgot to say graphing etc... I can manage, just after a way of returning the raw numbers.

 

Also only see math forum after I posted *doh* sorry for a wrong forum 1st post :(

Edited by VrX
Link to comment
Share on other sites

  • Solution

Set up the array structure that you need for your chart data then process your input, accumulating the data for each hour

//
// INPUT DATA
//
$input = [
        ['0900', '2000', 1, 5],
        ['1000', '2200', 1, 3]
    ];

//
// PREPARE ARRAYS TO STORE CHART DATA
//    
$init = array_fill_keys(range(0,23), 0);
$data = [
        0 => ['name'=>'Supervisors', 'data'=>$init ],
        1 => ['name'=>'Staff', 'data'=>$init ]
    ];

//
// PROCESS THE INPUT
//    
foreach ($input as $row) {
    list($start, $end, $sup, $staff) = $row;
    for ($hr=$start/100; $hr<$end/100; $hr++) {
        $data[0]['data'][$hr]+=$sup;
        $data[1]['data'][$hr]+=$staff;
    }
}

//
// CHECK CHART DATA
//
echo '<pre>',print_r($data, true),'</pre>';

result

Array
(
    [0] => Array
        (
            [name] => Supervisors
            [data] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => 0
                    [7] => 0
                    [8] => 0
                    [9] => 1
                    [10] => 2
                    [11] => 2
                    [12] => 2
                    [13] => 2
                    [14] => 2
                    [15] => 2
                    [16] => 2
                    [17] => 2
                    [18] => 2
                    [19] => 2
                    [20] => 1
                    [21] => 1
                    [22] => 0
                    [23] => 0
                )

        )

    [1] => Array
        (
            [name] => Staff
            [data] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => 0
                    [7] => 0
                    [8] => 0
                    [9] => 5
                    [10] => 8
                    [11] => 8
                    [12] => 8
                    [13] => 8
                    [14] => 8
                    [15] => 8
                    [16] => 8
                    [17] => 8
                    [18] => 8
                    [19] => 8
                    [20] => 3
                    [21] => 3
                    [22] => 0
                    [23] => 0
                )

        )

)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.