【问题标题】:Dynamically red negative numbers in report报告中的动态红色负数
【发布时间】:2016-09-02 16:43:33
【问题描述】:

我的报告中的所有负数都必须是红色的。有什么办法可以通过数字格式来做到这一点?如果没有,是否可以在某处创建一个动态条件来检查当前文本字段?

例如:

If this Textfield < 0 then this texfield font is red

请记住,它需要是动态的,因为我可以在不改变条件的情况下将它放在所有文本字段上。

我确实了解条件样式,但是我希望允许将条件拖到任何文本字段并区分是否它小于 0。

【问题讨论】:

  • 是的,我理解条件样式,但是我不使用特定字段,就像在那个例子中那样,我希望在我可以将这个条件拖到许多不同的文本字段和它会检查它是否小于 0。这个问题比另一个问题要复杂一些。

标签: jasper-reports


【解决方案1】:

使用条件样式无法实现这一点。

解决方案是创建一个自定义格式化程序,该格式化程序将输出带样式的文本,并将 textField 配置为markup="styled",如下所示:

<textField>
  <reportElement x="72" y="16" width="100" height="24" uuid="698866c8-7d26-4bc7-8727-b4a56d239a53"/>
  <textElement markup="styled"/>
  <textFieldExpression><![CDATA[NegativeNumberFormatter.format($F{A1}, "#,##0.###")]]></textFieldExpression>
</textField>

NegativeNumberFormatter 可能看起来像这样(使用默认包):

import java.text.DecimalFormat;

public class NegativeNumberFormatter {

  public static String format(Number n, String pattern) {
    if (n != null) {
      if (n.doubleValue() < 0) {
        DecimalFormat df = new DecimalFormat();

        if (pattern != null) {
          df.applyPattern(pattern);
        }

        return "<font color=\"red\">" + df.format(n) + "</font>";
      } else {
        return "" + n;
      }
    }

    return null;
  }
}

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 1970-01-01
    • 2015-09-28
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多