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
Set Position of Legend
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
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.
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.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
If you create a chart with an automatically generated legend, it ends up running this code in the JFreeChart constructor:
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:
The plot you pass to the constructor is used to populate the legend with items each time it is drawn.
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);
}
Code: Select all
LegendTitle legend = new LegendTitle(chart.getPlot());
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
Re: Set Position of Legend
Is it possible to set the legend position centered to the X-Axis, so its aligned to the axis title?
regards markus
regards markus