How do you alter the legend in a CombinedDomainCategoryPlot? I have several barcharts (the number is determined at runtime) that have the same domain items, and the legend wants to show a key for each domain item on each chart. In the end the legend shows 15 keys for 3 charts when 5 keys is all I need.
Also, is there a way to change the text that goes with each image on the legend?
Altering Legend in CombinedDomainCategoryPlot
Subclass the plot and override this method to return whatever items you need
Or just call
Code: Select all
public LegendItemCollection getLegendItems()
Code: Select all
setFixedLegendItems(LegendItemCollection items)
Thanks
I went for the easy way, and it works, thanks. Here is a copy of the code I used:
Thanks to http://java-x.blogspot.com/2007/03/char ... chart.html
(added 2 April 08)
Also, the following code will take a collection of legend items and return a collection of unique items. For example, a CombinedDomainCategoryPlot that builds a legend based on all the series for each chart; in my case I had 5 charts with up to 3 series apiece so my CombinedDomainCategoryPlot legend had 13 items in it when I only needed 3. Here is how I took care of it. Apologies if a function already exists to do this, if it does please point it out to me.
Code: Select all
private static LegendItemCollection createLegendItems() {
LegendItemCollection legenditemcollection = new LegendItemCollection();
LegendItem legenditem = new LegendItem("Trial 1", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.red);
LegendItem legenditem1 = new LegendItem("Trial 2", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.blue);
LegendItem legenditem2 = new LegendItem("Trail 3", "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, Color.green);
legenditemcollection.add(legenditem);
legenditemcollection.add(legenditem1);
legenditemcollection.add(legenditem2);
return legenditemcollection;
}
(added 2 April 08)
Also, the following code will take a collection of legend items and return a collection of unique items. For example, a CombinedDomainCategoryPlot that builds a legend based on all the series for each chart; in my case I had 5 charts with up to 3 series apiece so my CombinedDomainCategoryPlot legend had 13 items in it when I only needed 3. Here is how I took care of it. Apologies if a function already exists to do this, if it does please point it out to me.
Code: Select all
/*****************************
* createUniqueLegendItems
* parses the passed LegendItemCollection, and returns a LegendItemCollection that only
* contains unique items
* @param colExistingLegendItems
* @return newLegendItemCollection - a unique collection of items
*/private static LegendItemCollection createUniqueLegendItems(LegendItemCollection colExistingLegendItems) {
int iCounter = 0;
int iCounter2 = 0;
boolean bolItemExists=false;
LegendItemCollection newLegendItemCollection = new LegendItemCollection();
for (iCounter = 0; iCounter < colExistingLegendItems.getItemCount(); iCounter ++){
if(iCounter==0){
newLegendItemCollection.add(colExistingLegendItems.get(iCounter));
}
else
{
for(iCounter2 = 0; iCounter2 < newLegendItemCollection.getItemCount(); iCounter2++)
{
if(colExistingLegendItems.get(iCounter).getLabel().compareTo(newLegendItemCollection.get(iCounter2).getLabel())==0){
bolItemExists=true;
}
}
if(bolItemExists == false){
newLegendItemCollection.add(colExistingLegendItems.get(iCounter));
}
else{
bolItemExists=false;
}
}
}
return newLegendItemCollection;
}