Skip to main content
lisajIHV
Participant
March 27, 2019
Answered

HELP! Script needed to get largest %age and fill field with colour based on answer

  • March 27, 2019
  • 1 reply
  • 756 views

Hi everyone

Hoping one of you can help me please....

I have a pdf doc which is a questionnaire with a drop down list of responses for each answer (Yes, No and Partly). I then have a summary page at the end which counts the number of answers and calculates the percentage against each answer, i.e. Yes 34% No 23% Partly 43% ( i have a field for each of these).  What I would now like to do is have a field which calculates the largest percentage and fills the field with colour depending on the largest percentage, i.e if Yes is largest percentage then field is filled with Green, No with Red and Partly with yellow.

can someone help me with the script for this please?

Thank you so much in advance!!!

This topic has been closed for replies.
Correct answer try67

OK, then you can use this code as the custom calculation script of your field:

var newFillColor = color.transparent;

var values = [Number(this.getField("D1Yes%age").valueAsString), Number(this.getField("D1No%age").valueAsString), Number(this.getField("D1Partly%age").valueAsString)];

var colors = [color.green, color.red, color.yellow];

var max = 0;

for (var i in values) {

    if (values>max) {

        max = values;

        newFillColor = colors;

    }

}

event.value = max;

event.target.fillColor = newFillColor;

1 reply

try67
Community Expert
Community Expert
March 27, 2019

So you already have a script that calculates the percentage of each answer? If so, what are the names of those fields?

lisajIHV
lisajIHVAuthor
Participant
March 27, 2019

Yes I do - those fields are called D1Yes%age, D1No%age and D1Partly%age

Thank you!!!

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 27, 2019

OK, then you can use this code as the custom calculation script of your field:

var newFillColor = color.transparent;

var values = [Number(this.getField("D1Yes%age").valueAsString), Number(this.getField("D1No%age").valueAsString), Number(this.getField("D1Partly%age").valueAsString)];

var colors = [color.green, color.red, color.yellow];

var max = 0;

for (var i in values) {

    if (values>max) {

        max = values;

        newFillColor = colors;

    }

}

event.value = max;

event.target.fillColor = newFillColor;