How to change the legend order
Hi, my Eclipse BIRT Designer version is 4.9.0 and I have a problem with the legend order in the bar chart. The legend order looks good to me for my series A, B, and C. However, when I stack the bars(check the option"Stacked" in "Series" in the "Format Chart" wizard), the legend order is reversed. Is there a way to reverse the order back to before or control it? Thanks a lot for any help.
Comments
-
I found this code and it looks good to me.
/**
* Called befoer the chart is rendered.
*
* @param gcs
* GeneratedChartState
* @param icsc
* IChartScriptContext
*/
function beforeRendering( gcs, icsc )
{
//Script reverses chart legend order
importPackage(Packages.org.eclipse.birt.chart.computation);
importPackage(Packages.java.util);
importPackage(Packages.java.lang);
var llh = gcs.getRunTimeContext().getLegendLayoutHints();
//current legend
var liha = llh.getLegendItemHints( );
//new legend construct
var nliha = java.lang.reflect.Array.newInstance(LegendItemHints, (liha.length));
if ( liha.length > 1 ){
for( idx=0; idx < liha.length; idx++ ){
var tli = liha[idx];
var oldpos = tli.getIndex();
var newpos = (liha.length-1)-idx;
var ntli = liha[newpos];
//valid legend entry
if( tli.getType() == LegendItemHints.Type.LG_ENTRY){
//create a new legend item using the the new items values
var lih = LegendItemHints.newEntry( ntli.getItemText(), ntli.getValueText(), ntli.getSeriesDefinition(), ntli.getSeries(), ntli.getIndex() );
lih.top(tli.getTop());
lih.left(tli.getLeft());
lih.itemHeight(tli.getItemHeight());
nliha[idx]=lih;
}else{
//separator
nliha[idx]=liha[idx];
}
}
}
//Create a new copy of the legend layout hints
var nllh = new LegendLayoutHints( llh.getLegendSize(),llh.getTitleSize(),null, llh.isMinSliceApplied(),llh.getMinSliceText(),
nliha );
//Update before rendering
gcs.getRunTimeContext().setLegendLayoutHints(nllh);
}
0 -
I simplify the code. By swapping the left and right item locations, the order will be reversed. It works fine for me.
function beforeRendering( gcs, icsc )
{
var legendItems = gcs.getRunTimeContext().getLegendLayoutHints().getLegendItemHints( );
for( i=0; i < legendItems.length / 2; i++ ){
// left item and right item
var leftItem = legendItems[i];
var rightItem = legendItems[legendItems.length-1-i];
//swap their location(top, left)
top = leftItem.getTop();
left = leftItem.getLeft();
leftItem.top(rightItem.getTop());
leftItem.left(rightItem.getLeft());
rightItem.top(top);
rightItem.left(left);
}
}
0
Categories
- All Categories
- 117 Developer Announcements
- 52 Articles
- 145 General Questions
- 132 Services
- 56 OpenText Hackathon
- 35 Developer Tools
- 20.6K Analytics
- 4.2K AppWorks
- 8.9K Extended ECM
- 912 Cloud Fax and Notifications
- 81 Digital Asset Management
- 9.3K Documentum
- 29 eDOCS
- 162 Exstream
- 39.8K TeamSite
- 1.7K Web Experience Management
- 4 XM Fax