Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
a relatively simple question about highlighting
DQM
<p>Hi,</p>
<p>I have a dynamic text field that uses javascript math random to display a number.</p>
<p>Now I would like to display that number in red if it is less than 50, and green if greater than 50 using a highlighting rule. </p>
<p>How would I do that, or where would I find a exhaustive (complete) writeup on highlighting in Birt?</p>
<div> </div>
<p>I googled for hours but did not find a fitting solution.</p>
<p> </p>
Find more posts tagged with
Comments
nobbyktm
<p>Hi,</p>
<p>click on the dynamic text field and look into the Properies Editor View and select Highlights.</p>
DQM
<p>Thanks, but to clarify my question:</p>
<p>What rule should I write in the highlighting field?</p>
<p>for instance, I tried ["number'] is greater than 50, and variants of that, but did not get the required result.</p>
RRK
<p>Hi,</p>
<p> </p>
<p>Try this code in Control's 'OnRender'</p>
<p> </p>
<p>Eg:</p>
<p>if(this.getValue()>20000) </p>
<p> this.getStyle().backgroundColor="Blue"</p>
<p>else if(this.getValue()<20000)</p>
<p> this.getStyle().backgroundColor="Yellow"</p>
<p>else</p>
<p> this.getStyle().backgroundColor="Green"</p>
RRK
<p>Also if you want to change the text/number color then use below property</p>
<p>this.getStyle().color="Blue"</p>
DQM
<p>hi Rajatk, thanks for your suggestion!</p>
<p>I applied your code to my dynamic text, like this:</p>
<p> </p>
<p style="font-size:11px;font-family:Monaco;"><span style="color:#931a68;">if</span>(<span style="color:#931a68;">this</span>.getValue()>50) </p>
<p style="font-size:11px;font-family:Monaco;"> <span style="color:#931a68;">this</span>.getStyle().color=<span style="color:#3933ff;">"Green"</span></p>
<p style="font-size:11px;font-family:Monaco;"><span style="color:#931a68;">else</span> <span style="color:#931a68;">if</span>(<span style="color:#931a68;">this</span>.getValue()<50)</p>
<p> </p>
<p style="font-size:11px;font-family:Monaco;"> <span style="color:#931a68;">this</span>.getStyle().color=<span style="color:#3933ff;">"Red"</span></p>
<p style="font-size:11px;font-family:Monaco;"> </p>
<p style="font-size:11px;font-family:Monaco;"><span style="font-family:'Helvetica Neue', Arial, Verdana, sans-serif;font-size:14px;">but to my dismay the text renders "red" regardless of the value (if math random returns 7 (<50) the text turns red, and if it returns 54 (>50) the text also turns red. </span></p>
RRK
<p>Could you please share your report so that I can take a look at the condition ?</p>
DQM
<p>here it is.</p>
<p>A colleague suggested that the problem could be that the number is seen as a string, instead of an integer?</p>
<p> </p>
DQM
<p>Update: it appears the solution is to use a data object instead of dynamic text. Unlike data objects dynamic text cannot be formatted as integer or string.</p>
<p>attached example solves it (data in left column, dynamic text in right column).</p>
<p>Your script works as expected now, rajatk
Thanks!</p>
RRK
<p>Good to hear this..
</p>
Matthew L.
<p>I wanted to add this for future reference.</p>
<p>It is possible to change the element properties of a dynamic text element in the browser side (See attached rptdesign).</p>
<p> </p>
<p>While there are a few ways to accomplish this, I've attached an example that doesn't require modifying the code in the dynamic text element itself.</p>
<p>This example works for the iHub3+ professional versions of BIRT only.</p>
<p>To accomplish this, first add a "Bookmark" to the dynamic text element properties.</p>
<p>Then in the designs "clientInitalize" method, add the following code to get the viewer id:</p>
<pre class="_prettyXprint _lang-js">
try{getViewerID = this._service.id + "_";}catch(err){getViewerID="";}
</pre>
<p>Then in the designs "onContentUpdate" method, add the following code to locate your "Bookmarked" element and extract the innerHTML value to conduct style logic against its value:</p>
<pre class="_prettyXprint _lang-js">
if (window["__first_load"] === undefined) { //Check to see if this code has run previously for this report
window["__first_load"] = true; //If not, create a value to prevent infinite loops
var ObjElement = document.getElementById(getViewerID + "MyDynamicTextElement"); //Find our bookmarked element (with viewer ID).
if(ObjElement.innerHTML < 50) //If innerHTML value is less than 50
ObjElement.style.color = 'red'; //Change the text color to red (or '#FF0000')
if(ObjElement.innerHTML >= 50) //If innderHTML value is equal to or more than 50
ObjElement.style.color = '#008000'; //Change the text color to green (or '#008000')
}
</pre>
<p>This should produce the output you are looking for in your example.</p>