Thursday, November 14, 2013

cognos : 100% stacked Bar chart displaying percentages in bars using Java Script

We can't display percentages in middle of the bars in cognos 10.2, 100% stacked bar chart. But the same has been fixed in Cognos 10.2.1.

We can do the same thing using Java Script. just follow the below steps..

Suppose we have the following columns in List Report..

Region, Actual, 100% stacked bar chart with percentages, Budget as follows..

1) take the columns Region, Actual, and budget into List Report
2) goto Query Explorer
3) Open Query1
4) Add Data Item, enter the following expression to calculate Actual percentage over total of actual and budget
                [actual]/([actual]+[Budget])* 100
5) Name this data Item in properties as  "actp'
6) Add another Data Item to calculate Budget percentage and enter the below expression
                  [Budget]/([actual]+[Budget])* 100

7) Name this data Item in properties as  "budp'

8) Goto Page explorer and select Page1 to see the list report

9) Drag one HTML item and drop between the columns Actual and Budget as a column

10) Click LOCK icon in tool bar  to unlock

11) Add 4 more HTML items next to previously added HTML item, so you should have 5 HTML items

12) double click First HTML item and add the following script

     <script>
           var bar1Percent =
13) Click OK to close the HTML box

14) Just click to second HTML item to select (dont  Open)

15) Goto properties, got to the property "Source Type"

16) Change to "Data Item Value" 

17) For the Property Data Item Value, select the column  "actp"

18) Double click 3rd HTML item and enter the following expression      (dont forget semicolon as first line)
          
                 ;     
                 var bar2Percent = 
       
            
 19) Select 4th HTML item, goto properties and change the following propertiesr

                  Source Type: Data Item Value
                  Data Item Value: budp  (this is data item that we have created in 6 the step)

20) double click 5th HTML item and enter the following script as it is (dont forget about semicolon in first line)

;

var bar1=bar1Percent;
var bar2=bar2Percent;
var barL=bar1Percent+bar2Percent;
var width_driver = 200;
var barheight = ((width_driver) / 10);
            
           
var myTotalBar = '<div style="background-color:red;width:' + barL + ';height:' + barheight + '">';
            
var percent1 = '<div style="background-color:#F15A29; font-family:Source Sans Pro; font-size:9px; color:white; float:left; text-align:center; display:table-cell; vertical-align: middle;  width:' + bar1Percent + ';height:' + barheight + '"> <span style="display:table-cell; width:inherit;height:inherit; vertical-align: middle"> <strong>' + bar1.toFixed(1) + '%</strong></span></div>';
var percent11 = '<div style="background-color:#F15A29; font-family:Source Sans Pro; font-size:9px; color:white; float:left; text-align:center; display:table-cell; vertical-align: middle;  width:' + bar1Percent + ';height:' + barheight + '"> <span style="display:table-cell; width:inherit;height:inherit; vertical-align: middle"> </span></div>';
            
var percent2 = '<div style="background-color:#2B3990; font-family:Source Sans Pro; font-size:9px;color:white; float:right;text-align:center; width:' + bar2Percent + ';height:' + barheight + '"> <span style="display:table-cell; height:inherit; width:inherit; vertical-align: middle"> <strong>' + bar2.toFixed(1) + '%</strong></span></div>';
var percent22 = '<div style="background-color:#2B3990; font-family:Source Sans Pro; font-size:9px;color:white; float:right;text-align:center; width:' + bar2Percent + ';height:' + barheight + '"> <span style="display:table-cell; height:inherit; width:inherit; vertical-align: middle"> </span></div>';

if (bar1Percent<20)
{
percent1=percent11;
}

if (bar2Percent<20)
{
percent1=percent22;
}

myTotalBar = myTotalBar + percent1 + percent2 + '</div>';




document.write(myTotalBar);



</script>



You will get the output as below...



Note: This chart does not show percentage on bar if percentage < 20 as the width bar is very low so we can't show text on it.


Please note that, we can do the same thing using Micro Chart, but we will get extra space around the bar therefore, the lines appear with unnecessary space that we can't avoid
































1 comment:

  1. Very nice post.Really great post.It's very helpful for me.
    Thank you.
    Keep updating more concepts.

    cognos training online
    cognos online classes
    cognos online training

    ReplyDelete