Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed the legend into the plot area of JFreeChart

Tags:

jfreechart

In JFreeChart, is it possible to embed the legend into the chart itself? Legend can be set on top, bottom, left, right of the chart as follows, but is it possible to imbed it into the chart?

LegendTitle legend=chart.getLegend();
legend.setPosition(RectangleEdge.TOP);
like image 722
Junba Tester Avatar asked Jul 03 '12 22:07

Junba Tester


1 Answers

There is an example of how to set the legend inside the polt are included in the JFreeChart Samples XYTitleAnnotationDemo1, this is the key part:

XYPlot plot = (XYPlot) chart.getPlot();
LegendTitle lt = new LegendTitle(plot);
lt.setItemFont(new Font("Dialog", Font.PLAIN, 9));
lt.setBackgroundPaint(new Color(200, 200, 255, 100));
lt.setFrame(new BlockBorder(Color.white));
lt.setPosition(RectangleEdge.BOTTOM);
XYTitleAnnotation ta = new XYTitleAnnotation(0.98, 0.02, lt,RectangleAnchor.BOTTOM_RIGHT);

ta.setMaxWidth(0.48);
plot.addAnnotation(ta);

enter image description here

like image 167
GrahamA Avatar answered Nov 03 '22 04:11

GrahamA