Altering Legend in CombinedDomainCategoryPlot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Altering Legend in CombinedDomainCategoryPlot

Post by Reggie3 » Tue Mar 25, 2008 8:02 pm

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?

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Tue Mar 25, 2008 9:34 pm

Subclass the plot and override this method to return whatever items you need

Code: Select all

public LegendItemCollection getLegendItems()
Or just call

Code: Select all

setFixedLegendItems(LegendItemCollection items)

Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Thanks

Post by Reggie3 » Wed Mar 26, 2008 2:35 am

I went for the easy way, and it works, thanks. Here is a copy of the code I used:

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;
		 }
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

	/*****************************
	 * 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;
	}

Locked