ChartPanel--> Print function dark background

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
develop
Posts: 296
Joined: Wed Mar 23, 2005 10:01 pm

ChartPanel--> Print function dark background

Post by develop » Fri Apr 03, 2009 2:33 pm

with this new version (1.0.11) this new dark look and feel for chart looks really cool.
but is there anyway when i click on "Print" button on rightClick menu option in chartPanel, change the look and feel of chart to normal (with light Color background)
before passing to printer ?

I am asking this, because if someone tries to print dark chart then it requires lot more ink :( and users might not like this.

Thanks

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

Re: ChartPanel--> Print function dark background

Post by david.gilbert » Mon Apr 06, 2009 8:45 am

That's a problem I hadn't considered. You would need to modify the ChartPanel code - the methods createChartPrintJob() and print() in particular (both are public methods so you could subclass and override them). You'd probably have to clone the JFreeChart instance, apply a different theme, and use the clone for the printing.
David Gilbert
JFreeChart Project Leader

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

develop
Posts: 296
Joined: Wed Mar 23, 2005 10:01 pm

Re: ChartPanel--> Print function dark background

Post by develop » Wed Apr 08, 2009 2:01 pm

Thanks for reply. i tried to override those method and modified the way you suggested.
below is code for print() fuction

but result is pretty distorted, it does create light background, but other colors are not working properly.
can you tell me what is wrong ??

Code: Select all

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        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();
        try{
        JFreeChart cloneChart = (JFreeChart)chart.clone();
        ChartFactory.setChartTheme(StandardChartTheme.createJFreeTheme());
        ChartUtilities.applyCurrentTheme(cloneChart);
        cloneChart.draw(g2, new Rectangle2D.Double(x, y, w, h), chartPanel.getAnchor(),
                null);
        }catch(Exception e){
            e.printStackTrace();
        }
        return PAGE_EXISTS;
    }

Locked