Set Position of Legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
andrew
Posts: 21
Joined: Wed Aug 31, 2005 4:52 pm

Set Position of Legend

Post by andrew » Mon Oct 03, 2005 4:38 pm

Hi:

I know how to set the legend at the bottom of a pie chart but I really wanted to set the legend at a specific position in my PDF ( which I created using iText ). Here are my codes so far:

PiePlot plot = (PiePlot) chart.getPlot();
LegendTitle legendTitle = chart.getLegend();
legendTitle.setPosition(RectangleEdge.BOTTOM);

Can you please tell me how to set or draw a legend of a pie chart at a specific X & Y position?

Your help is much appreciated.

Andrew

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Oct 03, 2005 4:43 pm

It's not something that JFreeChart supports directly (yet). You can probably work around this with a little bit of your own code, making use of the draw() method in the LegendTitle.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

andrew
Posts: 21
Joined: Wed Aug 31, 2005 4:52 pm

Post by andrew » Mon Oct 03, 2005 5:06 pm

Hi Dave:

Thank you for replying.
I was experimenting with the draw() method in LegendTitle. A problem that I was having was that I can't seem to get rid of the default legend that was printed at the bottom of the pie chart. If I set the legend parameter in "JFreeChart chart = ChartFactory.createPieChart()" to "false", then I get "java.lang.NullPointerException" when I tried to use LegendTitle. If I set the legend parameter to "true", then the default legend is printed at the bottom of the pie chart ( which I don't want ) as well as the legend that I specified at a X & Y location by using the draw() method.

Here are my codes:

Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

// Draws the pie chart.
chart.draw(g2, r2D);

//Print legend at X & Y location
Rectangle2D r2DD = new Rectangle2D.Double(119, 47, 120, 180);
legendTitle.draw(g2,r2DD);


Can you please provide suggestion? Thanks in advance.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Oct 03, 2005 5:11 pm

If you create a chart with an automatically generated legend, it ends up running this code in the JFreeChart constructor:

Code: Select all

        if (createLegend) {
            LegendTitle legend = new LegendTitle(this.plot);
            legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
            legend.setBorder(new BlockBorder());
            legend.setBackgroundPaint(Color.white);
            legend.setPosition(RectangleEdge.BOTTOM);
            this.subtitles.add(legend);
        }
You want the same thing, but without the final line which adds the legend to the chart's subtitle list. Actually, all you really need is:

Code: Select all

LegendTitle legend = new LegendTitle(chart.getPlot());
The plot you pass to the constructor is used to populate the legend with items each time it is drawn.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

andrew
Posts: 21
Joined: Wed Aug 31, 2005 4:52 pm

Post by andrew » Mon Oct 03, 2005 5:31 pm

Hi Dave:

Thanks for replying so quickly.

So if I use "LegendTitle legend = new LegendTitle(chart.getPlot());" do I still need to use the automatic generated legend method: "JFreeChart chart = ChartFactory.createPieChart()"?

Thanks

Andrew

olbrich
Posts: 19
Joined: Fri Dec 27, 2013 9:39 am
antibot: No, of course not.

Re: Set Position of Legend

Post by olbrich » Thu Apr 23, 2015 10:46 am

Is it possible to set the legend position centered to the X-Axis, so its aligned to the axis title?

regards markus

Locked