Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement average for table totals #973

Closed
PhilippGrashoff opened this issue Mar 3, 2020 · 8 comments
Closed

Implement average for table totals #973

PhilippGrashoff opened this issue Mar 3, 2020 · 8 comments
Assignees

Comments

@PhilippGrashoff
Copy link
Collaborator

PhilippGrashoff commented Mar 3, 2020

Currently, there is no "avg" for table totals. Implementation would be easy, I can take care of it when I update to current versions. Code is quite finished:

  1. new property avgCount for table: $this->avgCount = 0;
  2. that code in updateTotals:
case 'avg':
    if($this->model[$key] !== null) { //only use non null values for sensible average calculation
        $this->totals[$key] = (($this->totals[$key] * $this->avgCount) + $this->model[$key]) / ($this->avgCount + 1);
        $this->avgCount++;
    }
    break;
@DarkSide666
Copy link
Member

Well I guess it's a bit to much calculations.
Probably better would be to use two protected (for internal use) table properties:

case 'avg':
    if($this->model[$key] !== null) { //only use non null values for sensible average calculation
        $this->avgSum += $this->model['key'];
        $this->avgCount++;
        $this->totals[$key] = $this->avgSum / $this->avgCount;
    }
    break;

@PhilippGrashoff
Copy link
Collaborator Author

definitely the more sensible approach!

@PhilippGrashoff
Copy link
Collaborator Author

aaaah, avgSum and avgCount need to be arrays, as we might calculcate an avg for more than one column :)

@DarkSide666
Copy link
Member

DarkSide666 commented Mar 9, 2020

Oh, yes, correct.
Can you create PR for this?

case 'avg':
    if (is_numeric($this->model[$key])) { //only use numeric values
        $this->avgSum[$key] = ($this->avgSum[$key] ?? 0) + $this->model['key'];
        $this->avgCount[$key] = ($this->avgCount[$key] ?? 0) + 1;
        $this->totals[$key] = $this->avgSum[$key] / $this->avgCount[$key];
    }
    break;

@PhilippGrashoff
Copy link
Collaborator Author

Will do when I switch to newest ATK versions, still on 1.7

@romaninsh
Copy link
Member

Related #286

@mvorisek
Copy link
Member

mvorisek commented Jun 8, 2020

@PhilippGrashoff should be possible with extra expression column, but of course only as workaround for now

@mvorisek
Copy link
Member

mvorisek commented Oct 3, 2022

closing in favor of #286

@mvorisek mvorisek closed this as completed Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants