Jasper report Line chart Category expression same

2019-08-18 03:16发布

问题:

I am using Jasper report line chart. In that line chart Category expression same value is printed only once.

Here, In Thread name column is specified as Category expression in Line chart. Redundant values are not printed. Only the unique names are printed. I want all my names, even if it's same name. How can fix this issue ?

回答1:

You have to wrap your String value which is printed as the category label into an object which fulfills the uniqueness constraint. You have to create a java class which implements the Comparable interface, because only Objects which are not equal are printed as a separate category value. The following code shows how such a class can be implemented:

public class UniqueCategoryLabel implements Comparable<UniqueCategoryLabel> {

    private Double id;
    private String value;

    public UniqueCategoryLabel(String value, Double id) {
        this.value = value;
        this.id = id;
    }

    @Override
    public int compareTo(UniqueCategoryLabel v) {
        return this.id.compareTo(v.id);
    }

    @Override
    public boolean equals(Object v) {
        return v instanceof UniqueCategoryLabel && this.id.equals(((UniqueCategoryLabel) v).id);
    }

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }

    @Override
    public String toString() {
        return value;
    }
}

You can provide uniqueness by creating instances of UniqueCategoryLabel using different ids because the equals method checks if the ids of the compared objects are the same. The labels of the chart themselves are created by using the toString() method of the provided object so your toString() method should return the String which you want to print as a label. In your report the field you use for the chart has to be of the type UniqueCategoryLabel instead of String and that should do all the magic.



回答2:

you would need to add a unique identifier to the category expression as this is the only way to show repeated values.