Question on multiple legends

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
MP3HiFi
Posts: 12
Joined: Fri Apr 11, 2008 8:02 am

Question on multiple legends

Post by MP3HiFi » Thu Mar 22, 2012 5:54 pm

Hello,

I have a question regarding multiple legends. I have a combined chart with 2 plots at the moment, but it will be more in future.
How can I set the position of the legends right to each plot?

The Problem is, that all the plots must fit on the same X-axis.

Image

Code: Select all

CombinedDomainXYPlot cPlot = (CombinedDomainXYPlot) chart.getPlot();
				XYPlot plot1 = new XYPlot(datasetChCo, null, new NumberAxis(""), createXYLineAndShapeRenderer());
				XYPlot plot2 = new XYPlot(datasetPrice, null, new NumberAxis(""), createXYLineAndShapeRenderer());
				cPlot.add(plot1);
				cPlot.add(plot2);
				chart.removeLegend();
				LegendTitle lt1= new LegendTitle(plot1.getRenderer());
				LegendTitle lt2=new LegendTitle(plot2.getRenderer());
				lt1.setPosition(RectangleEdge.RIGHT);
				lt2.setPosition(RectangleEdge.RIGHT);		
				chart.addLegend(lt1);
				chart.addLegend(lt2);
Thanks in advance.

MP3HiFi
Posts: 12
Joined: Fri Apr 11, 2008 8:02 am

Re: Question on multiple legends

Post by MP3HiFi » Wed Apr 18, 2012 1:12 pm

No ideas? I would be happy with setting the legend manually, but thats impossible, at least with the given API.

Another solution would be setting the chart-size and the plot-size manually. I have to make sure, that the plot fits to each other when putting the charts under each other.

Thanks
MP3HiFi

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Question on multiple legends

Post by paradoxoff » Thu Apr 19, 2012 8:56 am

Hi,
when I first read your post, I was about to suggest a CompositeTitle or a normal LegendTitle that is using two LegendItemSources (one for each of the two renderers). But that would give you only a single legend being displayed near the gap between the plots. You could use a CompositeTitle, add an EmptyBlock between the two separate LegendTitles, and then try to adjust the size and position of the CompositeTitle and its "child titles", but that sounds neither easy nor "dynamic" (it won´t auto adjust to various plot sizes).
You could try to use the XYTitleAnnotation: create one XYTitleAnnotation per plot, create a LegendTitle using the renderer as LegendItemSource, and use the LegendTitle as Title for the annotation.
Normally, an annotation is intended to be placed inside the data or plot area. If you need it to be placed outside the plot area (similar to a ordinary legend), to could try to use an x value of 1 (i.e. the x-coordinate of the annotation will be the right border of the domain axis), and RectangleAnchor.LEFT as anchor for the annotation. If you do so, you should increase the left value of the plot insets to a value that matches the width of the legend and thus the annotation. Otherwise, only the leftmost part of the annotation will be visible.
This will only work if the coordinate type of the annotation is XYCoordinateType.RELATIVE. Fortunately (at least for your use case), this is the default, and you can´t change it anyways.

MP3HiFi
Posts: 12
Joined: Fri Apr 11, 2008 8:02 am

Re: Question on multiple legends

Post by MP3HiFi » Wed May 30, 2012 8:50 pm

Thanks for that detailed hints. I will try them.

Perhaps I find another solution. Perhaps the best would be setting the plot area in all chatrs tho the same size.

Locked