【问题标题】:DynamicJasper: background color based on valueDynamicJasper:基于值的背景颜色
【发布时间】:2017-10-17 13:25:46
【问题描述】:

是否可以使用 DynamicJasper API 为每一行使用给定颜色设置特定单元格的背景颜色?

如果对象 Product 具有 { id, price, name, color} 属性 那么 name 字段的背景颜色需要更改为 color 字段的值。 (JasperReports 支持这种行为)

【问题讨论】:

    标签: java jasper-reports dynamic-jasper


    【解决方案1】:

    指南:

    在此解决方案中,会在创建报告之前生成所有可能颜色的列表。 colouredCell 列有它自己的值和颜色值(表示为一个字段)。 所有颜色都是HexString。 DynamicJasperReport.java

       public class DynamicJasperReport{
         public DynamicJasperReport(List<String> color){
            DynamicReportBuilder drb = new DynamicReportBuilder();
            ArrayList<ConditionalStyle> listCondStyle = StyleUtils.getConditonalStyles(color);
            AbstractColumn col = ColumnBuilder.getNew()
                                .setColumnProperty("colordCell", Object.class.getName())
                                .setTitle("Colored Cell")
                                .addConditionalStyles(listCondStyle)
                                .build();
       //adding the color value
            drb.addField("color", Boolean.class.getName());
        }
    
    
       }
    

    ConditionStyleExpression: BackgroundCondition.java

       public class BackgroundCondition  extends ConditionStyleExpression implements CustomExpression {
           private String fieldName;
           private String colorValue;
    
           public BackgroundCondition(String fieldName, String colorValue) {
        this.fieldName = fieldName;
        this.colorValue = colorValue;
    }
    
           public Object evaluate(Map fields, Map variables, Map parameters) {
        boolean condition = false;
        Object currentValue = fields.get(fieldName);
        if (currentValue instanceof String) {
            String s = (String) currentValue;
            condition = colorValue.equals(currentValue);
        }
        return condition;
    }
    
           public String getClassName() {
             return Boolean.class.getName();
          }
       }
    

    StyleUtils.java:

      public abstract class StyleUtils {
       public static Style backgroundColorStyle (String hexColor){
         Style cellBackgroundStyle = new Style();
         cellBackgroundStyle.setTransparency(Transparency.OPAQUE);
         cellBackgroundStyle.setBackgroundColor(Color.decode(hexColor));
         cellBackgroundStyle.setTextColor(Color.BLACK);
         cellBackgroundStyle.setVerticalAlign(VerticalAlign.TOP);
         return cellBackgroundStyle;
        }
    
       public static ArrayList<ConditionalStyle> getConditonalStyles(List<String> Color) {
        ArrayList<ConditionalStyle> conditionalStyles = new ArrayList<ConditionalStyle>();
        Color.forEach(c-> {
            BackgroundCondition backgroundCondition = new BackgroundCondition("color", c);
            ConditionalStyle cs = new ConditionalStyle(backgroundCondition, StyleUtils.backgroundColorStyle(c));
            conditionalStyles.add(cs);
    
        });
        return conditionalStyles;
    }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-22
      • 2014-05-24
      • 1970-01-01
      • 2016-10-25
      • 2014-08-17
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      相关资源
      最近更新 更多