Search code examples
javacharjfreechartlegend

How to sort the legend in JFreeChart?


Is there a way to sort the items in a JFreeChart legend by their backing object?

I've noticed that the TimeSeries/Series sets the title via a Comparable object.

Is there a way or a setting that forces the legend to be sorted by the comparable objects that the items are representing? Currently the legend is sorted by FIFO order.


Solution

  • You could use the add() method of LegendItemCollection to add the items in the desired order. Most Plots have a setFixedLegendItems() method to override the default.