2

I use Views Aggregator Plus to calculate sum of a column.

----------------------
Item        Data
----------------------
A              20
B              30
C              50
----------------------
                100
----------------------

Then I want to add additional column for percentage.

-----------------------------------------
Item        Data        Percentage
-----------------------------------------
A              20
B              30
C              50
-----------------------------------------
                100
----------------------

I know Global:Math expression. But I don't know how to get the sum, the sum field is not available. Only [value], [value_1] and [value_2] available for A, B and C respectively.

Is there a way to add a percentage column?

2

Now this problem has been solved, after VAP add percentage function
http://cgit.drupalcode.org/views_aggregator/commit/?id=2539ac5

Now I can add additional column calculating the percentage (as an example below): enter image description here

|improve this answer|||||
  • I enabled the Views Aggregator Plus More Functions module, but I don't see new functions in Group Agregation column. I created the Count without problem, but I can't find where is the percentage function (Cleared Cache and executed cron several times, even I tried from other browser). Can you bring me the light? – HEDMON Jun 22 '16 at 10:48
  • I think you also need to enable "Views Aggregator Plus More Functions" – Waluyo Adi Siswanto Aug 15 '16 at 23:08
1

You could use preprocess functions and templates for that. Just add an extra field in your view and preprocess the value inside template_preprocess_views().

Add your logic, for example:

function template_preprocess_views(&$variables) {
    $variables['percentage'] = $some_value % 100 . '%';
}

Then you can use the $percentage in your template which will contain your preprocessed value.
I'm just going through this very quick. If something is not clear, please ask and I'll go in further detail on how to tackle this with preprocessors and templates.

|improve this answer|||||
  • I have added an extra field (Global: Custom text). I don't have any idea where and how I have to write the function. Is it a new module, I have to create a new module? – Waluyo Adi Siswanto Jun 24 '15 at 10:29
  • Hello Michiel, could you please explain in a more detail. How to do that. – Waluyo Adi Siswanto Nov 26 '15 at 2:10
0

You can install views_php module, This module adds a PHP field to the view and There are some tokens available. You can use the current row values to calculate percentage.

|improve this answer|||||
  • Using PHP in views is very risky since it simply process user input... I would not recommend using this if you don't know the content managers... – Michiel Jun 24 '15 at 9:28
  • @Michiel calculating a simple percentage wont expose the site to any risk. The PHP by itself is not risky, the important thing is how much the codes are sensitive and what information are available through them – M a m a D Jun 24 '15 at 9:31
  • 100% agreed, but generally taken, it's not an optimal solution. I've had some bad experience with a (legacy) project using PHP inside Views. It's an open exposure for someone with thoroughly knowledge. But I'm sure I'm overthinking this for something simple like calculating a percentage. I'ld go for preprocessors. – Michiel Jun 24 '15 at 9:34
  • May be it loses a tiny portion of security for the sake of being straightforward, But in such case I myself prefer to use views_php. – M a m a D Jun 24 '15 at 9:41
  • Yes I have Global:PHP now. I don't know how to calculate percentage. What would be the value code and output code. – Waluyo Adi Siswanto Jun 24 '15 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.