Commit 2539ac53 authored by RdeBoer's avatar RdeBoer

Add Percentage aggregation function to views_aggregator_more_functions

parent 97e9a840
......@@ -187,7 +187,7 @@ function views_aggregator_group_and_compress($view_results, $field_handler, $cas
* @param array $groups
* an array of groups of rows, each group indexed by group value
* @param object $field_handler
* the handler for the view column to find the minimum in
* the handler for the view column to find the average in
* @param int $precision_group
* the number of decimals, if specified
* @param int $precision_column
......@@ -389,7 +389,7 @@ function views_aggregator_expression($groups, $field_handler, $group_exp = NULL,
$value = $math->evaluate($expression);
}
}
// Should we call number_format($value, $precision, $decimal, $separator)
// Should we call: number_format($value, $precision, $decimal, $separator) ?
$values['column'] = $value;
return $values;
}
......
......@@ -16,6 +16,11 @@ function views_aggregator_more_functions_views_aggregation_functions_info() {
'column' => NULL,
'is_renderable' => TRUE,
),
'views_aggregator_percentage' => array(
'group' => t('Percentage'),
'column' => t('100%'),
'is_renderable' => TRUE
),
);
return $functions;
}
......@@ -35,4 +40,37 @@ function views_aggregator_group_seq_number($groups, $field_handler = NULL, $star
$values[$group] = $count++;
}
return $values;
}
/**
* Aggregates a field group total as the percentage of the column total.
*
* @param array $groups
* an array of groups of rows, each group indexed by group value
* @param object $field_handler
* the handler for the view column to calculate percentages in
*
* @return array
* an array of values, one for each group and one for the column (always 100%)
*/
function views_aggregator_percentage($groups, $field_handler, $arg_group, $arg_column) {
$values = array();
$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] = 100 * $values[$group] / $sum_column;
}
$values['column'] = 100;
return $values;
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment