You are here

function views_aggregator_percentage in Views Aggregator Plus 8

Same name and namespace in other branches
  1. 7 views_aggregator_more_functions/views_aggregator_more_functions.module \views_aggregator_percentage()

Aggregates a field group total as the percentage of the column total.

Parameters

array $groups: An array of groups of rows, each group indexed by group value.

object $field_handler: The handler for the view column to calculate percentages in.

Return value

array An array of values, one for each group and one for the column (always 100%).

File

views_aggregator_more_functions/views_aggregator_more_functions.module, line 113
views_aggregator_more_functions.module

Code

function views_aggregator_percentage(array $groups, $field_handler, $arg_group, $arg_column) {
  $values = [];
  $sum_column = 0.0;
  foreach ($groups as $group => $rows) {
    $sum = 0.0;
    foreach ($rows as $num => $row) {

      // Do not count empty or non-numeric cells.
      $cell = vap_num(views_aggregator_get_cell($field_handler, $num, FALSE));
      if ($cell !== FALSE) {
        $sum += $cell;
      }
    }
    $values[$group] = $sum;
    $sum_column += $sum;
  }
  foreach ($groups as $group => $rows) {
    $values[$group] = $sum_column == 0.0 ? '' : 100 * $values[$group] / $sum_column;
  }
  $values['column'] = 100;
  return $values;
}