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

  • SEARCH
  • Home
  • 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 6822945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:43:26+00:00 2026-05-26T21:43:26+00:00

I’m creating a graph in php using the google graphing library to show StarCraft

  • 0

I’m creating a graph in php using the google graphing library to show StarCraft II APM data, and I’m having trouble thinking about the logic of it all. I’m not sure this is the best place to ask, but I’m out of options. This is a bit hard to explain so if any clarification is needed, let me know.

I have an array of keys and values. The key is the time in seconds. The value is the actions performed that second. Seconds with 0 actions are omitted by my data source.
Example:

seconds     actions
1           10
3           5
4           7
7           15
etc         etc

What I want to do is graph this data, showing the average actions per minute, at each second interval. Up until the first minute I would just use the available seconds that have passed.

Here is my current code

$p1_total = 0;
foreach ($p1 as $seconds => $actions) {
    $p1_total = $p1_total + $actions;
    $data['player1'][$seconds] = round($p1_total * 60 / $seconds);
}

However this calculates a rolling average as it goes on adding up $p1_total, what I really need is the average over the last 60 seconds at each interval.

I just cant fathom how to code this cleanly. I know I need to replace $p1_total with the count of actions from the last 60 seconds. But gathering that is whats getting me. How should I dynamically set this up without dealing with an insane amount of variables? Keep in mind my seconds aren’t every second, so I can’t just grab the last 60 entries in the array, as they might be more than 60 seconds long.

I thought about inserting values into the array to make up for the blank spots, but there is no reasonable way I can think of without skewing the average.
Or is there a way I can do this cleaner mathematically.

I appreciate everyone that can help. Please let me know if any more info would help. On the surface this feels like a really easy problem, but it really has be stumped. 🙁

  • 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-05-26T21:43:27+00:00Added an answer on May 26, 2026 at 9:43 pm

    You can insert zeros to make up for the blank spots, because each time that isn’t represented in the data corresponds to a second where zero actions were performed. This is probably the conceptually simplest way. You don’t need to actually insert the zeros in; what you could do is create a function which returns $p1[$t] if it exists, otherwise 0. Then just use your function instead of accessing $p1 directly. (My PHP is quite rusty, so consider this PHP-like pseudocode if necessary)

    function actions_at_second($t) {
        return ($t in $p1) ? $p1[$t] : 0;
    }
    

    Keep in mind that even though you don’t have raw data for certain seconds, there will still be a nonzero total action count for those seconds. So you should be creating a data point in the final APM array for every second, whether there were any actions performed then or not.

    As for actually computing the rolling average, what you can do is iterate over the first 60 seconds and store the cumulative total in the output array:

    for ($t = 1; $t <= 60; $t++) {
        $total += actions_at_second($t);
        $data['player_1'][$t] = $total * 60 / $t;
    }
    

    (This would need adjusting if your raw data included an entry for zero seconds) Then after the first 60 seconds, each time you add the number of actions at a given second, subtract the number of actions performed 60 seconds earlier:

    for ($t = 61; $t <= $tmax; $t++) {
        $total += actions_at_second($t) - actions_at_second($t - 60);
        $data['player_1'][$t] = $total;
    }
    

    ($tmax would be the highest second at which you have data, basically the length of the game.) I suppose in this latter sample, $total no longer represents a full cumulative total, so you could rename it – but it does have to be the same variable as $total in the previous code sample.

    By the way, I’m assuming $tmax will never be less than 61. If you’re going to be releasing this code “into the wild” for other people to use, make sure you check for that case (just in case of an early surrender or something).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.