Chris Kemp
Chris Kemp

Reputation: 2199

Is it possible to colour a cell with value from data in Cognos BI?

Let me first be clear. I'm not asking about how I do conditional formatting in Cognos BI. If there were a simple Red/Amber/Green colour scheme, based upon value ranges then I could do that. If it were a static list of colours, which never changed, I could also do that.

What I am after is accessing a hex colour code that is stored in my database, and I want to use that colour as my table cell background colour. This is something I commonly do in SSRS reports, but cannot see a method for in Cognos BI.

Is this even possible?

Upvotes: 2

Views: 2444

Answers (2)

Dirk Brys
Dirk Brys

Reputation: 1

I know it's quite and old post but just for completeness I'll add the references to get this working in html, pdf and excel.

To get this working not only for html but also for pdf and excel use a rich text item instead of a html item.

You can use following code in a query item for instance:

<span style="display:block; background-color:' + [Query Subject].[Query Item] + '"> </span>

The query item must then contain a valid color (e.g. rgb(255,0,0)) etc. which is defined by your data source.

Dragging a rich text item in a list and changing it to data item value and selecting the query item will work.

By using the span it will work for excel too, however to make sure it follows the size of the upper object in the hierarchy (the list column or a table etc) you want the display:block style.

Instead of the space in between the > < you can use any other query item that you want to appear as text.

Upvotes: 0

Johnsonium
Johnsonium

Reputation: 2005

You can do this via the HTML object in Cognos.

The HTML object can get its definition from one of the three main ways:

1) Hard-coded text
2) Data Item Value
3) Report Expression

Obviously the first method provides no way to dynamically set the value. I couldn't get the second one to work at all. I'm not yet sure why. However, I was able to use the third type to work to allow dynamic setting of a visual style.

For the solution we'll assume you have a data item called [Color] which pulls a string value from a database in the standard hex form that is used in CSS: #xxxxxx, e.g. #CCCCCC. For the purpose of this example we'll assume it is in query Query1. The following steps describe how to set it up.

1) Add an HTML item right above your list
2) Add another HTML item at the bottom of your list
3) In the top HTML item add a span tag with a unique id such as:

<span id="list">

4) In the bottom HTML item add a closing span tag

</span>

5) Add a third HTML item before all of the other HTML items
6) Set the 'Source Type' property of the HTML item to 'Report Expression'
7) In the Report Expression put the following code:

'<style>
#list td {
 background-color: ' + [Query1].[Color] + '
}
</style>'

8) Select the Page object and set the Query property to Query1
9) Click on the Properties property. Check the Color column to give the page access to that query-sourced value.

Now you can dynamically set the column color based on a database provided value. We used the span to give us a way to isolate just the table cells we want to manipulate.

The technique isn't perfect. For instance, the header cells also get their background changed to the color in question, which may or may not be desirable. This is because Cognos doesn't use the th tag for headers but instead renders them as normal cells (td).

Upvotes: 1

Related Questions