问题
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
.
回答1:
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());
来源:https://stackoverflow.com/questions/8302194/jfreechart-get-legend-graphic