Display the same order of data sets in the legend as in the Stacked chart area

Article ID: 2343
Last updated: 29 May, 2018
Article ID: 2343
Last updated: 29 May, 2018
Revision: 3
Views: 640
Posted: 11 Mar, 2011
by Dean J.
Updated: 29 May, 2018
by Gargani A.

Question

How to have the data sets in the legend in the same order as in the Stacked chart area?

Answer

By default, the order of data sets used in the chart area is bottom-up, and in the legend area - if vertical - it is top-down.
If you are using a bar chart renderer in stacked mode, you will see by looking at the color used for each data set that the order in the legend and in the chart area is not the same.
To keep the same data set order in both components:

In JViews Charts 8.5 or higher: Use IlvLegend.setVerticalOrientation(SwingConstants.TOP).

In JViews Charts up to 8.1: You will need to override the IlvBarChartRenderer.createLegendItems() method to reverse the order of legend items.

Here is an example of such a customization:

IlvBarChartRenderer disp = new IlvBarChartRenderer(IlvBarChartRenderer.STACKED){
  public IlvLegendItem[] createLegendItems() {
    IlvLegendItem[] myItems = super.createLegendItems();
    IlvLegendItem[] modItems = new IlvLegendItem[myItems.length];
    for(int i=0;i<=myItems.length-1;i++){
      modItems[i] = myItems[(myItems.length-i)-1];
    }
    return modItems;
  }
};

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2343
Last updated: 29 May, 2018
Revision: 3
Views: 640
Posted: 11 Mar, 2011 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Also listed in


Others in this category