Hello.
I'm currently developing an application that generates a chart from temperatures.
I need to generate a printing page A4 size, and want to include the chart in the middle of the page.
Can do that taking a screen capture with java.awt.Robot and putting as an image, but that is not for
production basis. Can anyone help me on this? My main goal is taking the chart and putting it in the middle
of a PDF for printing.
Thanks for your attention.

With the latest preview release you can use the snapshot feature to create an image of your chart, convert that to a BufferedImage and you can then embed that into a PDF file via iText. You'll need the BufferedImage to either be able to write your image to a file and include that in the PDF or use the iText API directly to put the BufferedImage in there.

>
But how could I do that capturing without depending on a new Stage being created for the Chart so to have a good image?
Is there a way to put that LineChart in a Image object without a screen capture?

Yes - take a look at the snapshot method of the node class. (JavaFX 2.2 preview only !!!)

My first attempt at this failed, so I filed: http://javafx-jira.kenai.com/browse/RT-22558 "PieChart snapshot renders incorrectly"

Kevin commented:

This is a known limitation in CSS and layout processing. In order for either to work correctly for Node.snapshot(), the node in question must be attached to a Scene. The scene need not be attached to a Stage, so the following is sufficient, added any time after the chartContainer is constructed and before snapshot is called: Scene snapshotScene = new Scene(chartContainer);
As part of finishing the API docs for snapshot, this limitation will be documented.

Once I updated my code to construct a Scene for the node for which the snapshot was being taken, everything worked fine.
I edited this post to include the working code.

Note that once you save the image to a png, the 2000x2000 pixel image (at least in the case of this piechart), compresses really well (down to 164kb) - which should be fine to embed into a fairly compact and portable pdf.

Hi. Thanks for the help.
I can achive what i need with your help, but my chart .png comes out without the labels from the axis.
Very strange, tried some things but no success.
The code that takes the snapshot:

I think this is caused by a bug in the JavaFX runtime.
I created jira issue: http://javafx-jira.kenai.com/browse/RT-22736 "LineChart snapshot render incorrectly" so that you can track it - you can vote for or comment on the issue if you like.

OK, just to close the topic and maybe help someone that tries to do the same thing.
I was able to sort it out with some improv.
First, if i grab take a snapshot of the LineChart from the scene, everything is ok with the picture, the two axis
come along just fine, but the chart would vanish from the scene, leaving the screen partially empty.
So i just instantiate a new chart with the same data and setContent(newChart) inside the AnchorPane.
That way i have a nice pdf with the chart int he middle. The scene suffered a quick flash, with the chart recreation.
But no problem with that, it's ok.
One of the requirements of the app is that the user must be able to edit the values of the chart and print, slightly modified, just
to round some pikes, and i was able to provide a live editing, allowing them to see the chart go up and down. The table and the chart
share the same list of Data items, and providing a proper cellValueFactory does the trick.
Thanks for the help.

jira issue: http://javafx-jira.kenai.com/browse/RT-22736 "LineChart snapshot render incorrectly"
was resolved as Not an issue.

Comment by Kevin Rushforth from the jira resolution:

This is not a bug. By default, charts will animate the tick marks and labels on the axis, and a variety of other things over a period of time (up to 700 msec). So at the time the chart is created, the text labels are not yet created and won't be until after animation has run. We should document this limitation, though (I will add a note to RT-21572).

There are two possible approaches that an application can take:

1) set the animated property on the X and Y axes and the chart to false.