Printing with ChartPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
irabinov
Posts: 2
Joined: Tue Apr 08, 2014 4:29 pm
antibot: No, of course not.

Printing with ChartPanel

Post by irabinov » Tue Apr 08, 2014 4:33 pm

I need to print extra text information while printing chart panel with right button click. Does anybody has a clue how to do it? I'd assume I need to override print method in ChartPanel, but how?

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

Re: Printing with ChartPanel

Post by david.gilbert » Wed Apr 09, 2014 6:44 am

All the printing happens within this method in the ChartPanel class:

Code: Select all

    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex) {

        if (pageIndex != 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2 = (Graphics2D) g;
        double x = pf.getImageableX();
        double y = pf.getImageableY();
        double w = pf.getImageableWidth();
        double h = pf.getImageableHeight();
        this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor,
                null);
        return PAGE_EXISTS;

    }
You can override this method and draw anything you want using the Graphics2D instance (that is provided by the printing API).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

irabinov
Posts: 2
Joined: Tue Apr 08, 2014 4:29 pm
antibot: No, of course not.

Re: Printing with ChartPanel

Post by irabinov » Wed Apr 09, 2014 3:18 pm

Thank you, works and very helpful.

Locked