Printing JavaFX charts
Printing JavaFX charts
When ChartVIewer is printed using standard JavaFX printing tool PrinterJob.printPage(node), the resulting image is very poor (low resolution).
In contrast, printing from the Swing JFreeChart is very sharp.
The issue is probably related to printing the contents of a Canvas (see e.g., http://stackoverflow.com/questions/3198 ... -in-javafx).
I have been trying to find a workaround, but so far I was unable to solve this. Has anyone dealt with this issue?
In contrast, printing from the Swing JFreeChart is very sharp.
The issue is probably related to printing the contents of a Canvas (see e.g., http://stackoverflow.com/questions/3198 ... -in-javafx).
I have been trying to find a workaround, but so far I was unable to solve this. Has anyone dealt with this issue?
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Printing JavaFX charts
I hadn't tried the JavaFX printing before, but it does seem to be limited versus the Swing printing API. I couldn't find a way to improve the quality of the JavaFX output. I wonder if the approach of taking a scaled snapshot of the node, then somehow printing the image might work...see this Stack Overflow entry for some info:
http://stackoverflow.com/questions/2918 ... 3#29196173
http://stackoverflow.com/questions/2918 ... 3#29196173
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Re: Printing JavaFX charts
I tried that approach. The Scale transformation increases the size of the image, but not the resolution. The final image is very pixelated.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Printing JavaFX charts
Do you need the print setup dialog or are you doing silent printing? In the latter case you could just use the AWT printer job.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Re: Printing JavaFX charts
Using AWT might be the best approach, until the printing support in JavaFX gets some improvements.
I am not sure, though, how to obtain the AWT Printable from the JavaFX ChartViewer.
I am not sure, though, how to obtain the AWT Printable from the JavaFX ChartViewer.
Re: Printing JavaFX charts
OK, I found how to do it. The trick is that one JFreeChart can be added to both ChartViewer (JavaFX) and ChartPanel (Swing) at the same time.
Printing via AWT using the following code creates a nice, sharp output:
Printing via AWT using the following code creates a nice, sharp output:
Code: Select all
SwingUtilities.invokeLater(() -> {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
PageFormat pf2 = job.pageDialog(pf);
if (pf2 == pf)
return;
ChartPanel p = new ChartPanel(chartViewer.getChart());
job.setPrintable(p, pf2);
if (!job.printDialog())
return;
try {
job.print();
} catch (PrinterException e) {
e.printStackTrace();
}
});
Re: Printing JavaFX charts
I have heard, JavaFX increases the size of the image, but not the resolution in print version. I never use it. Anyone suggest me a link to JavaFX API.
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm