Showing images dynamically (traffic light report)

In Chapter 2, Advanced Report Authoring, we created a report in the Formatting negative values recipe. This report shows the month-on-month difference in sales quantity.

Business wants to give this report a "dashboard" look by putting traffic light images (red, yellow, and green) in each row based on whether there is a rise in sales or a fall.

Getting ready

We will use the report based on the Formatting negative values recipe in Chapter 2, Advanced Report Authoring for this recipe.

Open that report in Cognos Report Studio and save a copy with a new name.

Tip

Please note that you will need administrator rights on the Cognos server to complete this recipe. If the server is installed on your personal machine, you will have these rights by default.

How to do it...

In this recipe, we will use three images (red, yellow, and green) as performance indicators in the report. To do this, perform the following steps:

  1. First we need to create three icons or images for red, yellow, and green. They should be already available on the Cognos server in the {Cognos Installation}\webcontent\samples\images folder. If not, then create them using any image editor software or use the images supplied with this book.
  2. Once you have the three images which you need to conditionally show on the report, place them on the Cognos server in the {Cognos Installation}\webcontent\samples\images folder. If the folder is not there, create one.
  3. Now open the report that shows the month-on-month Running Differences as shown in the following screenshot:
  4. Insert a new image from the Toolbox pane on the list report as a new column.
  5. Now go to Condition Explorer and create a new string variable. Define the expression as follows:
    if ([Query1].[Running Difference] > 0)
    then ('green')
    else if ([Query1].[Running Difference] < 0)
    then ('red')
    else ('yellow')
  6. Call this variable Traffic and define three possible values for it (red, yellow, and green).
  7. Now go back to the report page. Select the image. Open its URL Source Variable dialog box. Choose the variable Traffic and click on OK as shown in the following screenshot:
  8. From Condition Explorer, choose the red condition. Now click on the image again. It will allow you to define the image URL for this condition.
  9. Set the URL to../samples/images/Red.jpg as shown in the following screenshot:
  10. Similarly, define the URL for yellow and green conditions as ../samples/images/yellow.jpg and ../samples/images/green.jpg respectively.
  11. Run the report to test it as shown in the following screenshot:

How it works...

Cognos Report Studio allows you to put the images in the report by specifying the URL of the image. The images can be anywhere on the intranet or Internet. They will be displayed properly as long as the URL is accessible from Cognos application server and gateway. You might also need to check the IIS security and allow Anonymous Read and Browse accesses if you have a problem loading the pictures saved on your local server.

In this recipe, we are using a report which already calculates the Running Difference. Hence, we just had to define a conditional variable to trap different possible conditions. The Image component allows us to define the URL for different conditions by attaching it to the Traffic variable in step 7.

There's more...

In this case, though the URL of the image changes dynamically, it is not truly 100 percent dynamic. There are three static URLs already defined in the report, and one is picked up depending on the condition.

We can also use a data item or report expression as a source of the URL value. In that case, it will be totally dynamic and based on the values coming from the database; Cognos will work out the URL of the image and display it correctly.

This is useful when the image filenames and locations are stored in the database. For example, Product Catalog kind of reports.

Tip

Please note that this recipe works fine in HTML, PDF, and Excel formats. Also, we have used relative URLs for the images, so that the report can be easily deployed to other environments where Cognos installation might be in a different location. However, we need to ensure that the images are copied in all environments in the folder mentioned in step 2.