Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:30:12+00:00 2026-06-16T09:30:12+00:00

I am attempting to build a Latency Area Chart using php and json to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:30:13+00:00Added an answer on June 16, 2026 at 9:30 am

    You just need a function that will iterate over your array and group 5 items together and calculate the average. I threw this together:-

    function get5MinAv(array $times)
    {
        $result = array();
        $counter = 0;
        $temp['sum'] = 0;
        foreach($times as $time){
            if($counter == 0)$temp['start'] = $time['label'];
            $temp['sum'] += $time['value'];
            $counter ++;
            if($counter  == 5){
                $temp['av'] = $temp['sum']/5;
                $temp['end'] = $time['label'];
                $result[] = $temp;
                $temp['sum'] = 0;
                $counter = 0;
            }
        }
        return $result;
    }
    

    To test this I generated an array similar to yours:-

    $interval = new DateInterval('PT1M');
    $date = new DateTime();
    $times = array();
    
    for($i = 0; $i < 120; $i++){
        $times[$i]['label'] = $date->format('Y-m-d h:i:s');
        $times[$i]['value'] = 1;
        $date->add($interval);
    }
    

    and called it like this:-

    var_dump(get5MinAv($times));
    

    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)/100 to give a better range of values, but checking will be harder.

    A sample of the output:-

    array (size=24)
      0 => 
        array (size=4)
          'sum' => int 5
          'start' => string '2012-12-28 12:13:24' (length=19)
          'av' => int 1
          'end' => string '2012-12-28 12:17:24' (length=19)
      1 => 
        array (size=4)
          'sum' => int 5
          'start' => string '2012-12-28 12:18:24' (length=19)
          'av' => int 1
          'end' => string '2012-12-28 12:22:24' (length=19)
      2 => 
        array (size=4)
          'sum' => int 5
          'start' => string '2012-12-28 12:23:24' (length=19)
          'av' => int 1
          'end' => string '2012-12-28 12:27:24' (length=19)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to build a currency converter function in PHP. I have all my
I am attempting to build a database for inventory control using a large number
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
I'm attempting to build a method call from strings that have been passed into
I'm attempting to build a JSF/Hibernate application using Java 7 and Tomcat 7. I
I'm attempting to build an app using the Sencha Touch 2 builder tools. I
I am attempting to build a dynamic layout using the new autolayout api in
While attempting to build a website, i have gone through many online tutorials. Thanks
I am attempting to build Chromium for windows (using Visual Studio 2008 SP1) and
I am attempting to build a simple content management system using Twitter's Bootstrap for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.