I was frustrated by the default sorting order on the HTML5 charts. To customize the sort order you must create a comparator. And even then you can only sort by a category or series, not by a measure! I wanted to be able to sort my data in the query to solve these problems. The GUI provided no solutions besides the limited ascending or descending selections.
The Soltuion:
Create a comparator which always returns a value of 1 (greater than). iReport sees that the data is already in order and doesn't change anything. This allows you to define ordering at the query level! See the attached java file for more info. I've also attached a jar if you just want the fix.
To use:
Add jar to classpath.
Instantiate the custom comparator in the Comparator Expr box for the desired series or category. Enjoy query level sorting!
Thank you for sharing the code... I had the same problem and didn't think about this simple solution!
- Log in or register to post comments
add comment
1 Answer:
Posted on March 19, 2013 at 11:10am
*EDIT 2*
As of Jasper 5.1, I believe Jaspersoft fixed this issue by adding a 'none' sort type. I have not tried this due to time constraints but it's worth a shot if you're on 5.1 or later.
*EDIT 1*
My orginal solution worked fine for data sets that only had one measure per category. Basically any data set that didn't have the category repeated more than once. However when the category value was repeated it viewed everything as larger than the value before it. The solution wasn't as obvious as it should have been but I simply added a check in the Comparator to detect if the two objects were equal. If they were then I return 0, otherwise return 1 as usual.
The Soltuion:
Create a comparator which always returns a value of 1 (greater than). iReport sees that the data is already in order and doesn't change anything. This allows you to define ordering at the query level! See the attached java file for more info. I've also attached a jar if you just want the fix.
To use:
Add jar to classpath.
Instantiate the custom comparator in the Comparator Expr box for the desired series or category. Enjoy query level sorting!
Attachments:
Attachment | Size |
---|---|
![]() | 2.86 KB |
![]() | 625 bytes |