Animated dropdown checkboxes and creating custom on-hover events

For those who missed it, PerformanceG2 is sponsoring my trip to the IOD this year. In return, I’ve written a couple of articles for their blog.

The first is a fancy way of converting
needing-to-scroll-for-prompts-make-users-angry

To
sliders-are-awesome

This is a very easy technique to use, simply copy in the main script into an HTML item at the top of the page, and add another script at the bottom to call the functions. Since it doesn’t use the 10.2 Prompt API, you can implement it in every version since 8.4. To learn more about that technique, read the post here.

The next technique is a bit more interesting (at least for me). There have been many times where clients have asked for a way to add more insights to the tooltips in charts. For example, users may want to hover over a micro chart and see a magnified version:
Magnifying Microcharts

In that image each chart had the country ID hidden right next to it. Hovering over the chart would then unhide a div with the same ID and position it directly over the cursor. Moving away from the chart would then hide it.

The same fading functions were used for this next technique:
filtering a list

Each area in the map has an onhover event attached. There are some internal Cognos JS functions that will pull the contextual data of that area. So if you’re hovering over a dot, the functions would return the series, category, and measure of the dot. Hovering over a legend would return that specific series. Since this is referencing internal Cognos JS functions I can’t guarantee that it will work in previous (or future) versions.

Read up on that technique here.

It looks like the post is down. The popups report XML can be found here: Popups on Hover Report XML (1681 downloads)

updated animated checkbox prompt: updated-animated-checkbox.txt (998 downloads)

Quickie: PDF Report in New Window

One of the biggest frustrations people have with Cognos is that it will use the same window to export a report to PDF. Excel will open a new page, but not PDF. Why? Who knows? (IBMers, please feel free to comment below.)

Fortunately we can use JavaScript to force Cognos to do our bidding.

Since the JavaScript API changed in 10.2 I have two implementations, one for 10.1.1 and lower and one for 10.2 and higher. Simply paste the correct JavaScript into an HTML item, and fix up the button to meet your needs.

10.1.1

<script>
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if (!fW || fW == undefined) {
  fW = (formWarpRequest_THIS_ ? formWarpRequest_THIS_:formWarpRequest_NS_);
}
 
  var preFix = "";
  if (fW.elements["cv.id"]) { preFix = fW.elements["cv.id"].value;}
  var nameSpace = "oCV" + preFix;

CViewerManager.prototype.viewInPDF = function(){
                var oReq =new CCognosViewerRequest("render")
                oReq.addOption("run.outputFormat", "PDF");
                this.viewPDFInNewWindow(oReq);
}  
</script>
<input type="button" onclick="window[nameSpace].getRV().viewInPDF()" value="Export to PDF"/>

10.2 is a bit easier to work with:

<script>
CViewerManager.prototype.runPDF = function ()
{
  var oReq = new ViewerDispatcherEntry(this.getCV());
  oReq.addFormField("ui.action","render");
  oReq.addFormField("run.outputFormat","PDF");
  this.viewPDFInNewWindow(oReq);
};
</script>
<input type="button" onclick="window['oCV'+'_THIS_'].getRV().runPDF()" value="Export to PDF"/>

Remember, use only one of them. The 10.1.1 version won’t work in 10.2, but should work in previous versions. I don’t have access to the 8 versions any more, so I’d appreciate if someone could leave a comment saying if it works or not.

Export to PDF Report XML (856 downloads)

EDIT:
Many people have experienced issues with the version posted above on 10.2.1 and above. The following version works perfectly for me on 10.2.1 with IE 8 and Firefox.
Export-to-PDF-10.21.txt (1019 downloads)

Dynamic Time Groups in an OLAP Cube

One of the challenges presented to me while I was helping out at the DHHS in New Hampshire was to find a way to make user defined year groups in the report. While it’s trivial to create year groupings in a cube, it becomes impossible when you need to account for every possible combination reports require. Sometimes you may need sets of 5 years starting at 1991. Other times the requirement may be every 2 years starting at 1980. Sometimes the users may want to see each year, 1991-1995, 1992-1996, 1993-1997; while other times the overlap is unnecessary, 1991-1995, 1996-2000, 2001-2006. Each measure would have to be aggregated for each grouping, increasing the size of the cube.

At first glance my solution is a bit complex. It uses JavaScript to control the appearance of the prompt and OLAP functions to set up the groups. Finally report expressions are used to control the labels in the chart and crosstab. Since the Cognos PowerCube samples have a limited set of years, I’ve adapted the technique to work on months instead.

In this example, the user is presented with three prompts. The first and second prompts allow the users to select the months they want. The first prompt has no overlap, if the group size selected is 6, it will show Jan-Jun and Jul-Dec. The second prompt will show Jan-Jun, Feb-Jul, Mar-Aug and so on. The third prompt shows the group size, defaulting to 6.
Prompts

The non-overlapping month prompt takes some work to get working. It is, essentially, filtering the month level where mod(monthNumber,groupSize) is 0. It is a little more complex, as that alone won’t work. First, the mod function isn’t supported by the cube, and will result in local processing. Second, it should be monthNumber – 1:

(((total(
    [One] within set periodsToDate(
        [sales_and_marketing].[Time].[Time].[Time]
      , currentMember([sales_and_marketing].[Time].[Time])
    )
  )-1)
  / #prompt('Group Size','integer','6')#)
  -
  floor((( total(
    [One] within set periodsToDate(
        [sales_and_marketing].[Time].[Time].[Time]
      , currentMember([sales_and_marketing].[Time].[Time])
    )
  )-1)
   / #prompt('Group Size','integer','6')#)))
  * #prompt('Group Size','integer','6')#

If you want the group to have a different start month, change the -1.

That will give us a set of every sixth month. 2010/Jan, 2010/Feb, 2011/Jan, 2011/Feb. But now we need to modify the appearance of the prompt values. For that we need JavaScript.

This JS will loop through the prompt, convert the month name into a numeric value, add the selected group size, then convert that number back into a month and concatenate it onto the label again.

<script>
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]); 
if ( !fW || fW == undefined) 
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );} 

function parseMonthName(name) {
  var Months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  for (i=0;i<12;i++){ if(Months[i]==name) {return i+1; break;}}
}

function addMonths(month,add){
  add=add?add:0;
  month = (month + add)%12;
  month=month==0?12:month;
  return month;
}

function getMonthName(month) { 
  var Months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  return Months[month-1];
}


function setMonthNames(startPrompt,endPrompt){

var Size
  , Start = startPrompt
  , End = endPrompt
  , Month , newMonth, newYear

// Loop through the groupSize list to set the size variable. Subtract 1 from the value since the value includes the current month.
  for(var i =0;i<fW._oLstChoices_groupSize.length;i++){if(fW._oLstChoices_groupSize[i].selected) Size=fW._oLstChoices_groupSize[i].value -1}

  for (var i = 0;i<Start.length;i++){
    if(!Start[i].getAttribute('Orig')) {Start[i].setAttribute('Orig' , Start[i].getAttribute('dv')); };
    Month = Start[i].getAttribute('Orig');

    // If the group size is set to 0, use the original label. This is for on the fly changes.
    if(Size==0) {
      Start[i].setAttribute('dv' , Month);
    }
    else {
      newMonth = parseMonthName(Month.substr(5,3)) ; 
      newYear = addMonths(newMonth,Size)<Size?parseInt(Month.substr(0,4))+1:Month.substr(0,4);
      Start[i].setAttribute('dv' , Month + '-' +  newYear + '/' + getMonthName(addMonths(newMonth,Size)));
    }
    Start[i].innerHTML= Start[i].getAttribute('dv') ;
    //if(i>=Size-1) {Start[i].display='none';} else {Start[i].display='';} //Really, IE? Really?!
  }
}
</script>

So far this has only served to make it easier for the end user to understand how he’s filtering his report. The prompts themselves are still passing single member unique names to the query. Another calculated data item will be needed to group the years.

member(
  total(
    currentMeasure 
    within set 
    lastPeriods(
      0-#prompt('Group Size','integer','5')#
      ,currentMember([sales_and_marketing].[Time].[Time])
    )
  )
  ,'Grouped Total'
  ,'Grouped Total'
  ,[sales_and_marketing].[Time].[Time])

The lastPeriods will return the next Group Size members on the same level. This calculated member can then be used as a tuple on any measure that needs to be grouped. In the attached report I simply added it as the default measure in the charts and crosstabs.

That will create the groupings, but the labels on the chart and crosstab will still only show a single month. To fix that we can use a report expression with the same logic as the JavaScript from the prompt page:

case 
  when ParamValue('Group Size') = '1' then [Report].[No Overlap] 
  else 
    [Report].[No Overlap]  + '-' 
    + number2string(
      string2int32(substring([Report].[No Overlap],1,4))
      + case 
        when mod (
          case substring([Report].[No Overlap],6,3)
            when 'Jan' then 1
            when 'Feb' then 2
            when 'Mar' then 3
            when 'Apr' then 4
            when 'May' then 5
            when 'Jun' then 6
            when 'Jul' then 7
            when 'Aug' then 8
            when 'Sep' then 9
            when 'Oct' then 10
            when 'Nov' then 11
            when 'Dec' then 12
          end 
          + string2int32(ParamValue('Group Size'))-1,12) between 1 and (string2int32(ParamValue('Group Size'))-1) 
        then 1 
        else 0 
      end
    )
    +'/'+
    case mod (
      case substring([Report].[No Overlap],6,3)
        when 'Jan' then 1
        when 'Feb' then 2
        when 'Mar' then 3
        when 'Apr' then 4
        when 'May' then 5
        when 'Jun' then 6
        when 'Jul' then 7
        when 'Aug' then 8
        when 'Sep' then 9
        when 'Oct' then 10
        when 'Nov' then 11
        when 'Dec' then 12
      end + string2int32(ParamValue('Group Size'))-1,12)
      when 1 then 'Jan'
      when 2 then 'Feb'
      when 3 then 'Mar'
      when 4 then 'Apr'
      when 5 then 'May'
      when 6 then 'Jun'
      when 7 then 'Jul'
      when 8 then 'Aug'
      when 9 then 'Sep'
      when 10 then 'Nov'
      when 11 then 'Oct'
      when 0 then 'Dec'
    end
end

Simply change the text source on the crosstab or chart node member to Report Expression and paste that in.

Charts and xtab

Report XML