pattern negative amounts conditional style

0

Hello,

 

I have a requirement to display currency(a BigDecimal) in red with parenthesis when the respective amount is negative. I am able to achieve it by creating a conditional style and applying the pattern as $#,##0.00 ;$ (#,##0.00). However, I have several Fields(of amount/currency type) for which I have to this conditional style. As the example specified in the code section, for each field, I am creating a conditional style.

 

Now, I have more than 10 fields for which I have to create 10 conditional styles. For all the fields, the requirement is same(red in parenthesis if negative). And, each of them is a BigDecimal. Also, for each field I have a corresponding total field in the pagefooter section. For instance a  $V{TotalAmount1}  for $F{Amount1}. So, I created a new conditional style for the variable. Altogether, there will be 20 conditinal styles for 10 fields and their respective 10 totals.

 

Is there any way I could create one style(where I do not have to refer the field name in the condition like $F{Amount1}.intValue <0 ) and apply that style to all the 10 Fields?

 

In our application we display the report in HTML and export it to several formats such as PDF, XLS and CSV. I would to like display this red amounts in PDF, XLS and HTML.

 

Could anybody suggest if there is any alternative way of doing this apart from creating conditinal style for each field.

Thanks,

Naveen

Code:
<style 	name="Amount1"isDefault="false"	>
        <conditionalStyle>
	<conditionExpression><![CDATA[Boolean.valueOf($V{Amount1}.intValue() < 0)]]></conditionExpression>
<style name="Amount1" isDefault="false" forecolor="#CC0000"></style>
	</conditionalStyle>
</style>
 
<style 	name="Amount2"	isDefault="false">
<conditionalStyle>
	<conditionExpression><![CDATA[Boolean.valueOf($V{Amount2}.intValue() < 0 )]]></conditionExpression>
<style 	name="Amount2"	isDefault="false" forecolor="#CC0000">
</style>
</conditionalStyle>
</style>



Post Edited by Naveen K at 12/11/08 19:35



Post Edited by Naveen K at 12/11/08 19:56
soham's picture
21
Joined: Jul 18 2007 - 1:04am
Last seen: 4 years 2 months ago

6 Answers:

0

this is indeed very annoying - we also would like a feature like this.

new Boolean((BigDecimal)$X{CurrentField4Style}.compareTo(BigDecimal.ZERO) < 0)

 

where $X{CurrentField4Style} is just a reference/placeholder to the field the expressions is actually called by. Of course it should be just an java.lang.Object but with casting you could do the needed expressions. (like my sample)

 

So what do you think Teodor or Lucian, is this feasible ???

 

regards from Germany

C-Box

CBox's picture
934
Joined: Jul 19 2006 - 5:58pm
Last seen: 4 months 4 days ago
0

I think that would be feasible, and it would surely help reusing conditional styles in a report.  The enhancement has been requested some time ago (see this), hopefully we will find the time to implement this soon.

As a note, the feature does pose some issues that are not straightforward to settle.  For instance, the Java type of the $X{CurrentField} placeholder needs to be somehow determined at compile time.

Regards,

Lucian

lucianc's picture
6604
Joined: Jul 17 2006 - 1:10am
Last seen: 5 days 16 min ago
0

Why not just use java.lang.Object and cast it within the expression to BigDecimal or String if needed ... the worst that could happen would be a ClassCastException at runtime when the field has a wrong type for that conditional style.

 

C-Box

CBox's picture
934
Joined: Jul 19 2006 - 5:58pm
Last seen: 4 months 4 days ago
0

So, I was doing a wild goose chase then. Anyways, CBox and Lucian, appreciate your responses. It would be nice if we could have that feature at the earliest.

 

Not sure if it helps anybody checking this post, I was able to fix my problem while exporting to  XLS(only) by applying the pattern as

$#,##0.00 ;[Red]$ (#,##0.00) and setting the custom element property(using IReport), net.sf.jasperreports.print.keep.full.text, of the amount field to true. This solution was posted by someone in the forum.

 

Thanks,

Naveen

soham's picture
21
Joined: Jul 18 2007 - 1:04am
Last seen: 4 years 2 months ago
0

Is there any progress concerning this issue? I have exactly the same problem.

As a workaround I implemented an own MarkupProcessor. I use the <style> pattern attribute for the precision formatting and the <textElement> markup attribute to use an additional MarkupProcessor. The processor just looks for a starting "-" and surrounds the value with <font color="red"></font>.

Works perfect. Maybe someone will find this useful.

Code:
<textField>
 <reportElement x="0" y="0" width="45" height="15" style="summe"/>
 <textElement markup="sc" />
 <textFieldExpression class="java.math.BigDecimal">$V{IST_ERLOS</textFieldExpression>
</textField>
 
 
public class BigDecimalMarkupFactory implements MarkupProcessorFactory {
 
 @Override
 public MarkupProcessor createMarkupProcessor() {
   return new MarkupProcessor() {
     @Override
     public String convert(String arg0) {
       if (arg0 != null && arg0.startsWith("-"))
         return "<font color=\\"red\\">" + arg0 + "</font>";
 
       return arg0;
     }			
    };
  }
}
</td></tr></tbody></table>
pinnau's picture
3
Joined: Nov 24 2009 - 7:23am
Last seen: 3 years 6 months ago
0

UPDATE:

All what was missing is a line in jasperreport.properties:

net.sf.jasperreports.markup.processor.factory.simple=name.of.SimpleMarkupProcessorFactory

Regards Stefan

Hi Pinnau!

I'm trying to wrote my own MarkupProcessorFactory, but i don't know how to make it available to the report engine. I've implemented the class, but when i'm trying to use it, i get an error:

<span style="color: rgb(255, 0, 0);">Exception in thread "main" java.lang.Error: net.sf.jasperreports.engine.JRRuntimeException: No markup processor factory specifyed for 'simple' markup.</span>

Can you please give me a hint what do do next?

Regards Stefan

My report snipped:

<textElement verticalAlignment="Middle" markup="simple"><font size="8"/></textElement>

My Class

public class SimpleMarkupProcessorFactory implements MarkupProcessorFactory {<br /><br />	public static final String PROPERTY_MARKUP_PROCESSOR_FACTORY_PREFIX = "simple";<br /><br />	@Override<br />	public MarkupProcessor createMarkupProcessor() {<br />		return new MarkupProcessor() {<br /><br />			@Override<br />			public String convert(String arg0) {<br />				return "hello world!";<br />			};<br />		};<br />	}<br />}  

pinnau
Wrote:

Is there any progress concerning this issue? I have exactly the same problem.

As a workaround I implemented an own MarkupProcessor. I use the <style> pattern attribute for the precision formatting and the <textElement> markup attribute to use an additional MarkupProcessor. The processor just looks for a starting "-" and surrounds the value with <font color="red"></font>.

Works perfect. Maybe someone will find this useful.

Code:
<textField>
 <reportElement x="0" y="0" width="45" height="15" style="summe"/>
 <textElement markup="sc" />
 <textFieldExpression class="java.math.BigDecimal">$V{IST_ERLOS</textFieldExpression>
</textField>
 
 
public class BigDecimalMarkupFactory implements MarkupProcessorFactory {
 
 @Override
 public MarkupProcessor createMarkupProcessor() {
   return new MarkupProcessor() {
     @Override
     public String convert(String arg0) {
       if (arg0 != null && arg0.startsWith("-"))
         return "<font color="red">" + arg0 + "</font>";
 
       return arg0;
     }			
    };
  }
}



Post Edited by luebke at 01/05/2010 12:54



Post Edited by luebke at 01/05/2010 12:55
luebke's picture
1
Joined: Jan 5 2010 - 2:29am
Last seen: 7 years 7 months ago
Feedback
randomness