2

In my application, I am showing data in a table as well as through a Dual Axis Bar/Line JFreeChart. To save some space (as the charts are being saved as PNG and put on PDF with iText PDF), I wanted to take the Graphics from the Legend, use them in the tabular view, and remove the legend.

Is there a way to grab the icons that lie with the legend item? I have found the LegendGraphic class, which seemed like would be the method to retrieve the icon from the LegendItem, but have not found anything in the documentation for LegendItem that would indicate it does.

It would be preferable if they were returned in an object that could easily be used to create a com.itextpdf.text.Image, such as byte[] or java.awt.Image.

king14nyr
  • 715
  • 6
  • 21

1 Answers1

3

You can get a series' LegendItem using the chart renderer's getLegendItem() method. You can change a series' Shape using the methods of ShapeUtilities, as shown in this example. See also DefaultDrawingSupplier for details of how createStandardSeriesShapes() works.

Addendum: Note that the renderer's getLegendItem() method works even if you create the chart with no legend or later use chart.removeLegend(). Once you have the LegendItem, you can use it's attributes as required.

System.out.println(renderer.getLegendItem(0, 0).getShape());
System.out.println(renderer.getLegendItem(0, 0).getFillPaint());
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the examples. This will get me to a point I can use the Graphics2D to draw out some shapes and such. I was hoping that there was something in the API that would prevent me from having to rebuild the legend icons, but this will work nonetheless. Thanks again. – king14nyr Nov 29 '11 at 13:38
  • The temporary nature of `LegendItem` lets the legend stay in sync with the renderer. You can implement `RendererChangeListener` and have your table register with the renderer. – trashgod Nov 29 '11 at 16:40