Jump to content
Changes to the Jaspersoft community edition download ×

Conditional Formatting


concurrent

Recommended Posts

Hello,

I'm using the Java API to build a report that uses an ArrayList of ArrayList of Objects (Strings or Doubles) as a DataSource and am having trouble mastering the conditional formatting at the cell level.

I've searched the forums and based on what I have learned I have been able to create ConditionalStyles and apply them to a row -- the alternating row colour example is well-documented, for example.

I am trying to have conditional cell-formatting for a Double value exceeding a high or a low threshold, I am very close, except that when a cell matches, the entire row of data where that cell is located is formatted with the condition format, not the individual cell.

Any help appreciated, I haven't found a spelled-out API-based cell-specific conditional formatting example, but I have found plenty of row-specific examples.

Thanks for any advice! 

 

Code:
I am adding the fields to a band as follows (data is showing correctly in the report):   for (JRField field : fields) {      textField.setStyle(normalStyle);      expression = new JRDesignExpression();      expression.setValueClass(field.getValueClass());      expression.setText("$F{"+field.getName()+"}");      textField.setExpression(expression);      band.addElement(textField);   } And the conditional formatting definition looks like:   for (JRField field:fields) {      JRDesignExpression conditionExpression = new JRDesignExpression();      JRDesignConditionalStyle cs = new JRDesignConditionalStyle();      conditionExpression.setValueClass(java.lang.Boolean.class);      conditionExpression.setText("new Boolean($F{"+field.getName()+"} < 0.0)");      cs.setConditionExpression(conditionExpression);      cs.setBold(true);      cs.setBackcolor(new Color(0xFF, 0xC0, 0xC0));      normalStyle.addConditionalStyle(cs);   }
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

This is because in the first iteration all fields are set with the same style: normalStyle. The normalStyle contains multiple conditional styles ( added one by one for each field in the second iteration). When one of the style conditions becomes true, its conditional style will override the normalStyle settings and this will affect all the fields with normalStyle.

Try to set only one conditional style per field, using a single iteration:

   for (JRField field : fields) {

      JRDesignStyle fieldStyle = new JRDesignStyle();
      fieldStyle.setName(field.getName()+"Style");

      JRDesignExpression conditionExpression = new JRDesignExpression();
      JRDesignConditionalStyle cs = new JRDesignConditionalStyle();
      conditionExpression.setValueClass(java.lang.Boolean.class);
      conditionExpression.setText("new Boolean($F{"+field.getName()+"} < 0.0)");
      cs.setConditionExpression(conditionExpression);
      cs.setBold(true);
      cs.setBackcolor(new Color(0xFF, 0xC0, 0xC0)); 

      fieldStyle.addConditionalStyle(cs);  

      textField.setStyle(fieldStyle);
      expression = new JRDesignExpression();
      expression.setValueClass(field.getValueClass());
      expression.setText("$F{"+field.getName()+"}");
      textField.setExpression(expression);
      band.addElement(textField);
   }

 

Hope this helps,

sanda




Post Edited by shertage at 09/17/2010 11:36
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...