This is the first of a series of articles on extending the usability of Active Reports.
Active Reports makes a wonderful addition to the Cognos suite. Locally processed interactivity (my Brio alter ego is shouting, “so what!”; just ignore him) means complex, though predefined, data exploration scenarios can be offered to ever more demanding users. Just by clicking on a row in a list, the entire report can be refreshed instantly. Standard web-based reports are simply incapable of that behavior.
Periodically I get requests for things Active Reports can’t do. For some reason, saying that something is not possible completely renders all of the other features moot. One such request was: “I want an animated chart that will loop through all of the months in the database”. The user wanted a chart that would show 12 months at a time, incrementing one month per second, looping back to the start once it hits the last month.
As a mockup, I built an example chart in a data deck that shows that behavior.
I stuck a Data Iterator under it. By pressing the next button you can make it look like it’s animated. Sadly, this wasn’t enough. The needed it to be automatic.
Now we can start playing with JavaScript. This presents a few interesting problems, some of which I’m still trying to solve.
Problem 1. Cognos rewrites IDs.
To demonstrate, I create a text item and an HTML item – a div with text inside it:
But when I run it, and examine the HTML:
That’s interesting! The ID of the div changed from myDiv to v9! This means that any JavaScript written will have to not use getElementById(). There is no guarantee that the div will be v9 tomorrow. Especially if there are data changes in the report. That’s fine though. I could do a getElementsByTagName(‘div’) and loop through them until I find one that has the correct name, or some other attribute I set. It’ll be slow, but better than nothing.
Let’s see what happens if I use JavaScript to count the number of divs. It should be a simple:
<script>alert(document.getElementsByTagName('div').length)</script>
I just add it to the existing HTML item:
And yet when I run it, I’m not getting the alert. The JS is sound, and if I copy it to the script console in IE Dev Toolbar I get back the expected value. So what is going on?
Unfortunately this is still one of the things that I’m still trying to figure out. My guess is that every element (list, chart, HTML item) is stored in an object and loaded into the page after the page has been loaded as needed. When running a report with a data deck, we can see that the individual cards aren’t loaded until another control references them. This allows the page to be significantly smaller on the first load, and far more responsive than if everything was rendered. Of course, this is just a guess and I could be completely wrong (but that hardly ever happens).
Effectively this means that onload events are out. And predefined functions are out. Everything will have to be inline events on elements hand written with HTML items.
Getting back to the original problems, the user said that he would be okay with a start/stop button. He presses it to start the “animation”, and clicks it again to stop it whenever he wants. This means we can define the function in the onclick event. And once again I’ve turned to my good friend Dan Freundel for help. And once again he turned 20 lines of spaghetti code into 5 lines of pure JS genius.
Since we can’t loop through divs that don’t yet exist, we’ll have to find a way to recreate the action of clicking on the next/previous buttons on the iterator. One way is to set the parameter directly. Any control that uses that param will be effected. Unfortunately, that way requires referencing minified JS functions. If we do it this way, any upgrade or fix pack will kill the report. Instead, I opted to literally click on the next/prev buttons in the loop.
First, the JS:
<button onclick=" if(!document.runAnim) { runAnim = function(buttonElement) { var intervalTracker, doStuff = function() { var iter = buttonElement.previousSibling , ibuttons = iter.getElementsByTagName('button'); if(ibuttons[2].getAttribute('disabled')) {ibuttons[0].setAttribute('disabled',false);ibuttons[0].click();} else {ibuttons[2].click()} } buttonElement.onclick = function() { if(intervalTracker) { clearInterval(intervalTracker); intervalTracker = false; } else { intervalTracker = setInterval(doStuff,1000); } }; intervalTracker = setInterval(doStuff,1000); };} runAnim(this); this.value='stop'; " value="Start animation">Start</button>
First this defines the function. Since we can’t use inline HTML functions will have to be defined like this. On the first run, if the runAnim function doesn’t exist, it will create it. Next it will call that function in the scope of the button. That means the setInterval timer will exist only for that button, and it will not interfere with any other timers on the page. As mentioned before, because the IDs get rewritten, it is impossible to easily reference an element on the page. For this, I simply placed the button directly after the iterator. Next, when an iterator is first loaded, it has no value set and you can’t click any of the buttons. But if, somehow, the disabled=true was removed from the First button, clicking it would select the first item in the list.
So we have the script. The timer function is created the first time the button is clicked, that function and all associated variables are invoked in the scope of the button and it immediately starts working.
The list on the right acts both as a highlighter for the current row, and as a control to select which month you want to see in the chart.
Update: Some people have reported issues with the previously attached XML. I’ve updated the report with 10.2.2.
AR-JS.txt (751 downloads)
Any chance you have found a way to put filters on a list similar to what Excel allows so you can filter by each column in a list? The code I found that worked in Cognos 8 (prior to active reports) doesn’t work inside active reports. Any direction you can point me would be greatly appreciated. Thank you!
Shawna Tugman
What a great post, I’m a big fan of your work. I’m decent with report studio but I’m just getting started with using Javascript to push the tool to solve some of the user requests that aren’t so easy out of the box. (And because it’s fun). Got a long way to go yet.
I used this example as a starting point to make some animated widgets. If you save the report output they are available for use in Workspace where with the transitions enabled they look really slick and work great.
I haven’t tried them in the Native Cognos App on the iPad, but I have a feeling that there might be some limitations once I do. Have you had any success with this approach on the iPad?
Hi Denis! I’m glad to hear it’s working for you. To be honest, I also haven’t tried them on the iPad yet. I do have an iPad, and it is for work, but it’s currently filled with games for my four year old. If you try it, let me know how it goes.
Hello,
I salute you for the work you done, it’s just
This is exactly what I wanted for a while. unfortunately the XML does not work for me, I am cognos version 10.2.2 🙁
thank you
Fouad
In which version of cognos this has been designed? I am trying to open this in 10.2 & 10.1 versions. But can’t open this xml
I’ve updated the post with report XML from 10.2.2. Hopefully you should be able to open it now.