4

i'm drawing several charts with XY-Plots in my application. I have no problem displaying a legend under (or on top, left or right of) the plot.

But to save space i want to draw a legend-box into the XY-Plot.

I'm able to draw image-,text- and line-annotations into the plot.

So i tried to make a TitleAnnotation from my LegendTitle and add this annotation to the plot.

    LegendTitle legend = new LegendTitle(plot.getRenderer());
    XYTitleAnnotation anno = new XYTitleAnnotation(20.0, 40.0, legend);
    plot.addAnnotation(anno);

The coordinates of the annotation(20.0, 40.0) are in the visible area of the plot, but nothing is drawn.

If i let the Chart-Object draw the legend under the plot this is done fine (but not what i need).

    chart.addLegend(legend);

So i think the LegendTitle object is correct. But why is it not drawn as Annotation?

I hope someone can figure this out or help me with a different way to draw a legend into the plotarea.

Une
  • 41
  • 1
  • 2

2 Answers2

1

If you want it to be inside an XYPlot, I think you need an XYTextAnnotation.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • I never noticed the [variety](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/annotations/XYAnnotation.html) before! – trashgod Sep 12 '11 at 16:55
  • Thanks, i'm able to do text Annotations but i want a complete legend with markers and Text. – Une Sep 13 '11 at 07:42
  • In worst case i'll try to build my own legend with shape and text annotations. Or is there a way to overlay a other Swing panel over a chart panel? – Une Sep 13 '11 at 07:44
  • I don't know. What [layer](http://stackoverflow.com/questions/6797012/jfreechart-series-tool-tip-above-shape-annotation) are you using for your `XYTitleAnnotation`? – Catalina Island Sep 13 '11 at 12:44
0

https://github.com/apetresc/JFreeChart

http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/XYPlot.html

http://kickjava.com/src/org/jfree/chart/DefaultOldLegend.java.htm

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Thanks, but none of your links can help me to solve my problem. – Une Sep 12 '11 at 12:16
  • +1 [`addAnnotation()`](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/XYPlot.html#addAnnotation%28org.jfree.chart.annotations.XYAnnotation,%20boolean%29) looks promising. – trashgod Sep 12 '11 at 16:54