SOLUTION: Retain Query/Datasource Level Sorting HTML5 Charts

 

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!
StevenCrawford's picture
Joined: Feb 19 2013 - 8:09am
Last seen: 2 years 9 months ago
Thank you for sharing the code... I had the same problem and didn't think about this simple solution!
simonabertozzi - 10 years 4 days ago

1 Answer:

*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: 
StevenCrawford's picture
Joined: Feb 19 2013 - 8:09am
Last seen: 2 years 9 months ago

I fashioned your solution as an 'Answer' so you would get credit for it in our system.

djohnson53 - 10 years 3 days ago
Feedback