Printing JavaFX charts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
plusik
Posts: 28
Joined: Tue May 16, 2006 3:36 am
Location: Boston, MA, USA

Printing JavaFX charts

Post by plusik » Wed Feb 24, 2016 9:29 pm

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?

david.gilbert
JFreeChart Project Leader
Posts: 11683
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Printing JavaFX charts

Post by david.gilbert » Thu Feb 25, 2016 10:29 pm

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
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited

plusik
Posts: 28
Joined: Tue May 16, 2006 3:36 am
Location: Boston, MA, USA

Re: Printing JavaFX charts

Post by plusik » Thu Feb 25, 2016 10:49 pm

I tried that approach. The Scale transformation increases the size of the image, but not the resolution. The final image is very pixelated.

david.gilbert
JFreeChart Project Leader
Posts: 11683
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Printing JavaFX charts

Post by david.gilbert » Thu Feb 25, 2016 10:59 pm

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

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited

plusik
Posts: 28
Joined: Tue May 16, 2006 3:36 am
Location: Boston, MA, USA

Re: Printing JavaFX charts

Post by plusik » Thu Feb 25, 2016 11:12 pm

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.

plusik
Posts: 28
Joined: Tue May 16, 2006 3:36 am
Location: Boston, MA, USA

Re: Printing JavaFX charts

Post by plusik » Fri Feb 26, 2016 5:59 am

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:

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();
            }
        });

nirmal
Posts: 1
Joined: Thu Oct 11, 2018 10:24 am
antibot: No, of course not.
Contact:

Re: Printing JavaFX charts

Post by nirmal » Thu Oct 11, 2018 10:41 am

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.

John Matthews
Posts: 444
Joined: Wed Sep 12, 2007 3:18 pm

Re: Printing JavaFX charts

Post by John Matthews » Thu Oct 11, 2018 5:22 pm


Post Reply