Time for action – styles with formulas

We will configure the rectangle's Style.bg-color property, which we have previously placed in the Details section, so that its color will be defined based on a row-by-row evaluation of the sum_amount field. We will use the formula editor and the IF function to do so. Next, we will create two parameters so that the final user can establish the conditions based on which the rectangle will be colored. What we will do now is create a formula so that the background color of the rectangle we added earlier changes according to certain values.

  1. We select Rectangle, and in the Style tab, press the Time for action – styles with formulas button found on the right of bg-color:
    Time for action – styles with formulas
  2. This will open the formula editor. In Formula:, we type the following:
    =IF([sum_amount]<4;"#990000";IF([sum_amount]<6;"#cccc00";"#66cc00"))
  3. Next, we click on OK to continue.

We will explain each part of this calculation, but first we will talk a bit about the function IF. The IF function receives the following three parameters that are separated by semicolons (;):

  • The first is a logical expression that will return a true or false value. A comparison is often placed here, for example [sum_amount]<4, where the result of the comparison will return true if [sum_amount] is less than (<) 4, else it will return false.
  • The second value is what the function will return when the first value is true.
  • The third value is what the function will return when the first value is false.

So the IF function could be explained as follows:

  • If sum_amount is less than 4 then the color will be red (#990000); else evaluate what to do according to the second IF function)

And the second IF function can be explained as follows:

  • IF([sum_amount] is less than 6?; color yellow (#cccc00); color green (#66cc00)

We can see how our report is coming along by previewing the following screenshot:

Time for action – styles with formulas

The limit values that we have chosen in making the previous formula are totally arbitrary. What we will do now is create two parameters so that the end user can define the value of these limits.

The first parameter will collect the value that will be used to divide the bottom range from the middle, and the second parameter will divide the middle from the top.

Next, we will create the first parameter:

  • Name = SelectFirstThreshold
  • Label = First Threshold:
  • Value Type = Number
  • Default Value = 4
  • Display Type = Text Box

Now we will create the second parameter:

  • Name = SelectSecondThreshold
  • Label = Second Threshold:
  • Value Type = Number
  • Default Value = 6
  • Display Type = Text Box

In the formula that we have just applied to Style.bg-color of the rectangle, we will modify the calculation as follows:

=IF([sum_amount]<[SelectFirstThreshold];"#990000";IF([sum_amount]<[SelectSecondThreshold];"#cccc00";"#66cc00"))

If we preview the report and modify the values of the parameters SelectFirstThreshold and SelectSecondThreshold, we will see how the background color of the rectangle varies.

Lastly, we will change the order the parameters appear in so that the two new parameters are in the top part of the list of selectors when we execute our report.

We go to the Data tab, select the SelectFirstThreshold parameter, right-click on it, and choose the Send Forward option. This will make the chosen parameter move up one level in the hierarchy. We should repeat this procedure until SelectFirstThreshold is first in the list.

Then we will use the same procedure to position SelectSecondThreshold second in the list.

Time for action – styles with formulas

If we preview our report, we can see the changes we have made:

Time for action – styles with formulas

What just happened?

We created a new formula to calculate our rectangle's Style.bg-color property according to the value of the sum_amount field in each row. We used the IF function to do so, which was explained in detail through the example. Later we created the parameters SelectFirstThreshold and SelectSecondThreshold so that the end user is able to choose the values of the limits used by the formula to calculate the color of our rectangle. Lastly, we placed these two new parameters at the top of the selector list.

Have a go hero

Next, to sharpen the skills we have learned, we will modify the report created in this chapter and adapt it to meet new requirements.

The report that we propose, which you have to create, will have an image with a stoplight in the Group Footer section, and the following screenshot will be shown at the end of each group, in this case at the end of the analysis of the films of each rating. Let's see the last page of the rating G:

Have a go hero

The steps you should follow, broadly speaking, are as follows:

  1. Create a copy of the 09_Using_Formulas.prpt report and save it with the name 10_Using_Formulas_Plus.prpt.
  2. Create a function of the type Running | Average (Running) and configure it as follows:
    • Function Name = AvgAmount
    • Field Name = sum_amount
    • Reset On Group Name = RatingGroup

If you are using Windows, you should use the backslash instead of the forward slash to separate folders; that is, you should use instead of /.

This formula will insert the path corresponding to an image based on a comparison made to the average of sum_amount (function AvgAmount). This formula can be explained as follows:

  • If [AvgAmount] is less than [SelectFirstThreshold], then insert a red stoplight (traffic_red.png); else if [sum_amount] is less than [SelectSecondThreshold], then insert a yellow stoplight (traffic_yellow.png), and insert a green stoplight (traffic_green.png)

Pop quiz – formulas

Q1. Which of the following statements is true?

  1. If we enter a value in a parameter's Default Value field, while the report is being executed, this value will always appear to be selected.
  2. PRD implements OpenDocument to manage formulas.
  3. The formula editor includes functions for making mathematical calculations but does not include functions for making statistical calculations.
  4. There are two ways to create formulas in PRD, one for general use and another for specific use.
  5. To create a formula in PRD, you just drag this object from the insertable objects' area.
  6. The formula editor is a wizard that will help us to create our formulas step by step.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset