Transparent PNG Charts with JFreeChart

If you’re one of the 3 people in the world that need to create a transparent PNG image using JFreeChart, you’ve come to the right place:

JFreeChart chart = ChartFactory.createXYBarChart(...);
chart.setBackgroundPaint(new Color(255,255,255,0));
KeypointPNGEncoderAdapter encoder = new KeypointPNGEncoderAdapter();
encoder.encode(chart.createBufferedImage(width, height, BufferedImage.BITMASK, null));

The key is that you must use the KeyPointPNGEncoderAdapter, which is slower for big charts, but is the only one of the two PNG encoders that JFree ships with that has the ability to do alpha transparency. On systems running JDK 1.4 and above, the ImageEncoderFactory will return the SunPNGEncoderAdapter, which does not support alpha transparency.

Also, if you’re planning on using alpha transparency in a web application that needs to support IE, you’ll want to check out this JavaScript fix by Bob Osola.

This entry was posted in Interface Design, J2EE. Bookmark the permalink.

19 Responses to Transparent PNG Charts with JFreeChart

  1. Rong Ou says:

    That’s pretty cool. I must be one of those 3 people. :)

    For the second part, you can also use ChartUtilities if you just want to save the image to a file:

    ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel);

  2. Glen Smith says:

    And your lucky 3rd person comes from Australia! Thanks for this tip, mate. Just what I was after. Made my day.

  3. Lars says:

    So im the third person using it? :-)

    That did the trick! Thanks Aaron.

  4. Karbol says:

    Hi, I am fourth person… :)

  5. Jason says:

    5th person, thanks!

  6. Tim says:

    Yet another..

    But how do you get the ChartRenderingInfo object for making clickable HTML tags??

    I’m currently calling:
    ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, useImageWidth(), useImageHeight(), chartRenderingInfo);

    inside a spring View implementation.

  7. Your count is way off… I’m #6.

  8. Steven Hess says:

    Add me to the list — thanks!

  9. Corné says:

    Thanks from the Netherlands

  10. Giovanni says:

    Another one from Italy!
    Thank you for the tip!

  11. Carlos David says:

    “Steven Hess” is 7th … I’m #8, yeah we’ll change the world :P .

  12. Alex says:

    Thanks from Germany !

  13. Alfredo says:


  14. YeahHa says:

    one more to your list, thanks

  15. Thank you from number >3 :-)

  16. Jackeror says:

    Hi, I’am one of thoses 3 \o/

  17. Jorge Rucoba says:

    Awesome!. Thanks for that.

  18. Alex C Punnen says:

    I stumbled on the site; I had problems setting the border etc to nill when rendering to a webpage;

    JFreeChart chart = ChartFactory.createPieChart3D(
    “”, // chart title
    dataset, // data
    false, // include legend

    chart.setBackgroundPaint(null);//this line necessary for transparency of background
    // final ChartPanel chartPanel = new ChartPanel(chart);
    //chartPanel.setOpaque(false); //this line necessary for transparency of background
    //chartPanel.setBackground(new Color(0, 0, 0, 0)); //this l
    //chart.setBorderStroke(new BasicStroke(10.0f));
    /* chart.setDomainGridlinesVisible(false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setNoDataMessage(“No data available”);
    plot.setSectionPaint(1, java.awt.Color.GREEN);
    plot.setSectionPaint(2, new Color(255, 0, 255)); //purple
    plot.setSectionPaint(3, java.awt.Color.RED);

    —-In servlet

    int width = 155;
    int height = 155;
    JFreeChart chart=createChart(new Integer(onair),new Integer(inerror),new Integer(inplanning));
    OutputStream out=resourceResponse.getPortletOutputStream();
    ChartUtilities.writeChartAsPNG(out, chart, width, height,true,0);

  19. Alex says:

    Thanks. This solved my blurry png problem.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>