2017-10-17 9 views
0

Est-il possible de définir la couleur d'arrière-plan d'une cellule spécifique en utilisant une couleur donnée pour chaque ligne à l'aide de l'API DynamicJasper?DynamicJasper: couleur d'arrière-plan basée sur la valeur

Si l'objet produit a {id, prix, nom, couleur} attributs alors la couleur de fond du nom champ doivent être le changement à la valeur de champ couleur. (JasperReports soutient ce comportement)

Répondre

0

Directives:

Dans cette solution, une liste de toutes les couleurs possibles est généré avant la création du rapport, La colonne coloredCell a sa propre valeur et aussi. la valeur de couleur (représentée comme un champ). toutes les couleurs sont 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; 
} 
    }