Hitachi Vantara Pentaho Community Forums
Results 1 to 16 of 16

Thread: Formatting/Color-coding based on the value in a cell

  1. #1
    Join Date
    Mar 2003
    Posts
    4,947

    Default Formatting/Color-coding based on the value in a cell

    I would like to be able to format the output of a printed cell in a table based on the value in the cell, for example turning the background for cells in a particular column red when the value is greater than 100.

    I don't get the impression that this is supported directly.

    I see in a previous topic regarding rotating text 90 degrees a reference to org.jfree.ui.Drawable.

    Am I on the right track looking at this interface? Can you give me any pointers on where in the config .xml or class architecture I should be trying to define and insert custom rendering code? Is it complicated to set this sort of thing up?

    Thanks.

  2. #2
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    Hi,

    setting colors can be done easily with a small function or you can use the ElementColorFunction to do the job for you.

    This function requires a boolean column or expression to control what color should be set.

    So the first thing is: Create a expression that returns a boolean value if the column value is greater than 100:

    Code:
    <expression name="compareValues" class="org.jfree.report.modules.misc.beanshell.BSHExpression">
    <properties>
    <property name="expression">
    Object getValue &#40;&#41;
    &#123;
       if &#40;dataRow  == null&#41; return null;
       Number columnValue = &#40;Number&#41; dataRow.get &#40;"yourColumnHere"&#41;;
       if &#40;columnValue == null&#41; return Boolean.FALSE;
       if &#40;columnValue.intValue > 100&#41; return Boolean.TRUE;
       return Boolean.FALSE;
    &#125;
    </property>
    </properties>
    </expression>
    The ElementColorFunction will read the boolean value from that expression and set the color of the element depending on the returned value.

    Code:
    <function name="ColorFunction" class="org.jfree.report.functions.ElementColorFunction">
    <properties>
    
    <property name="field">compareValues</property>
    
    <property name="colorTrue">#ff0000</property>
    <property name="colorFalse">black</property>
    
    <property name="element">ElementName</property>
    </properties>
    </function>
    And finally give your number element that should be colored a name, so that the ElementColorFunction can identify the target element.

    Code:
    <number-element name="ElementName" ... />
    Rotating text is far more complicated than printing text in a different color. Rotating has complex effects on the layout computation, and the current engine is not able to handle this. But colors have to effect on any layout - an element uses the same space regardless which color it has.

    Have mo' fun,
    said Thomas

  3. #3
    Join Date
    Mar 2003
    Posts
    4,947

    Default Wow. Thank you.

    I never expected that much information that quickly.

    Your response is very helpful. I will try it out.

    Thanks again.

  4. #4
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    How can I use java code to make it?

    Thanks.

  5. #5
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    The code would be:

    Code:
    public class CompareExpression extends AbstractExpression
    &#123;
      public CompareExpression&#40;&#41;
      &#123;
      &#125;
    
     public Object getValue &#40;&#41;
     &#123;
       DataRow dataRow = getDataRow&#40;&#41;;
       Number columnValue = &#40;Number&#41; dataRow.get &#40;"yourColumnHere"&#41;;
       if &#40;columnValue == null&#41; return Boolean.FALSE;
       if &#40;columnValue.intValue > 100&#41; return Boolean.TRUE;
       return Boolean.FALSE;
      &#125; 
    &#125;
    Add it to the report:

    Code:
    CompareExpression ce = new CompareExpression&#40;&#41;;
    ce.setName &#40;"compareValues"&#41;;
    report.addExpression &#40;ce&#41;;
    and now the ElementColorFunction ...

    Code:
    ElementColorFunction ec = new ElementColorFunction&#40;&#41;;
    ec.setName &#40;"ColorFunction"&#41;;
    ec.setField &#40;"compareValues"&#41;;
    ec.setColorTrue &#40;new Color &#40;0xff0000&#41;&#41;;
    ec.setColorFalse &#40;Color.black&#41;;
    ec.setElement &#40;"ElementName"&#41;;
    report.addFunction &#40;ec&#41;;
    Have more fun,
    said Thomas

  6. #6
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    Thanks so much...

  7. #7
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    Sorry I couldn't get it out, the error message like:

    com.jrefinery.report.function.FunctionInitializeException: Failed to parse colors
    at com.jrefinery.report.function.ElementColorFunction.initialize(Unknown Source)
    at com.jrefinery.report.function.ExpressionCollection.add(Unknown Source)
    at com.jrefinery.report.JFreeReport.addFunction(Unknown Source)

  8. #8
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    First, the method getValue never used with ur code. Then I added one use for getValue, but the getDataRow() return is null, why is it? I used a TableModel for report data.

    Thanks.

  9. #9
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    Hi,

    OK, it seems that the ElementColorFunction want the colors and everything else set by the properties interface.

    Code:
    ec.setName &#40;"ColorFunction"&#41;;
    ec.setProperty &#40;"field", "compareValues"&#41;;
    ec.setProperty &#40;"colorTrue", "#ff0000"&#41;;
    ec.setProperty &#40;"colorFalse", "black"&#41;;
    ec.setProperty &#40;"element", "ElementName"&#41;;
    The expression will be called (indirectly) from the function. Outside of the report processing, you won't have a DataRow object, as there exists no valid report processor.

    Btw. Is there a special reason, why you are still using JFreeReport 0.8.3? This version is heavily out of date and you won't see any updates or bugfixes for this one anymore - never.

    Have mo' fun,
    said Thomas

  10. #10
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    Many thanks...

  11. #11
    Join Date
    Jun 2004
    Posts
    5

    Default

    Hi,

    I have a table listing different string values, and I want to set the colors of the values based on an algorithm in my code.

    I can provide the colorstring for each value with the dataset, or maybe someone has other suggestions?

    How can I use this to set the colors of the stringvalues?

    Thanks.

    John

  12. #12
    Join Date
    Mar 2003
    Posts
    4,947

    Default Trying almost the same

    Hi there!!

    I'm trying to to almost the same thing....but instead of change colours I'd like to switch the visibility of the botton line of a given cell.
    In my case, I want to make invisible the botton lines of the cells (of a column) when the value of the cell is empty.
    Could help me, please?

    Thank you very much,

    []'s

    Rafael

  13. #13
    Join Date
    Oct 2004
    Posts
    14

    Default

    Why is the getValue() method in the CompareExpression class never being called ?

  14. #14
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    Hi,

    expressions will only be called, if they are referenced either in an other expression or a field.

    Whenever the ElementColor function tries to read the value from the assigned field, the expression's "getValue()" method will be called and the result of that will be returned to the caller.

    Have mo' fun,
    said Thomas

  15. #15
    Join Date
    Sep 2014
    Posts
    8

    Default

    Hi , I am on same page .
    Did u got , how to do it?

  16. #16
    Join Date
    Aug 2016
    Posts
    142

    Default

    Probably don't need to resurrect an ancient thread but it is very simple to do this

    Select your cell and in Style go to the text-colour option. Click the plus symbol

    Paste something like this in where field is the name of the field you want to colour

    =IF([field]<1;"red";"green")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.