I am attempting to build a Latency Area Chart using php and json to take datapoints I have in a MySQL database, and average them into 5 minute intervals. I am wondering if something like this is even possible.
Current Code:
mysql_select_db($mysql_db);
$query = ("select date, (delta_msec * .001) from www where agent_id in (45517,45655,42189,38583,45872,38678,38289,39074,38069,42096,37987,45182,38974,44956,38630,38297,38571,39181,39063,41992,38293,37978) and date >='" . date("Y-m-d H:i:s", strtotime("30 minutes ago")) . "' and date <= '" . date("Y-m-d H:i:s", strtotime("now")) . "' order by date asc");
$result = mysql_query($query) or die($query. "<br/><br/>" .mysql_error());
while ($row = mysql_fetch_array($result)){
$www[$q] = array("label" => $row[0], "value" => $row[1]);
$q++;
}
When using
print_r ($www);
I am able to see the following (correct) Multi-dimentional array:
Array
(
[0] => Array
(
[label] => 2012-12-14 12:47:17
[value] => 0.618
)
[1] => Array
(
[label] => 2012-12-14 12:47:17
[value] => 0.890
)
[2] => Array
(
[label] => 2012-12-14 12:47:22
[value] => 1.908
)
[3] => Array
(
[label] => 2012-12-14 12:47:37
[value] => 2.912
)
[4] => Array
(
[label] => 2012-12-14 12:48:18
[value] => 1.275
)
[5] => Array
(
[label] => 2012-12-14 12:48:25
[value] => 0.449
)
[6] => Array
(
[label] => 2012-12-14 12:48:30
[value] => 7.831
)
)
The Array is MUCH larger than this, but I wanted to give a snippet of how it looks.
With this information I can convert this into JSON and FusionCharts will read all the values perfectly. However, it’s really just a reformatted “scatterplot” graph which doesn’t read well. I would like to be able to create another array from this Array so that I can basically make 5 minute buckets and average all of the values and do this for the past 4 hours.
I have only begun learning PHP, MySQL and fusioncharts (all at once) for about 2-3 weeks now and this is far beyond what I have learned. Would anyone be able to assist in creating an array like this? (or any other method?) Or would I only be able to do this by reformatting my SQL query to pull the data averaged already, and then make this call again for smaller versions of the graph?
You just need a function that will iterate over your array and group 5 items together and calculate the average. I threw this together:-
To test this I generated an array similar to yours:-
and called it like this:-
I have just put a value of 1 in to make checking easy, I have tested it with one or two other values and it all seems to work OK.
If you wished you could seed it with
$times[$i]['value'] = rand(0, 1000)/100to give a better range of values, but checking will be harder.A sample of the output:-
Obviously, you can adjust this to suit your preferences, but this should get you started on the right track. A the moment this function will ignore any leftover times at the end that don’t form a group of 5. If you want to change that behaviour it will be a good exercise for you.