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.setEncodingAlpha(true);
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.

19 thoughts on “Transparent PNG Charts with JFreeChart”

  1. 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. 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.

  3. 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
    false,
    false
    );

    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.setBorderPaint(null);
    //chart.setBorderStroke(new BasicStroke(10.0f));
    chart.setBorderVisible(false);
    chart.setBorderStroke(null);
    chart.setBackgroundPaint(null);
    /* chart.setDomainGridlinesVisible(false);
    chart.setRangeGridlinesVisible(false);
    chart.setBackgroundAlpha(0.0f)*/
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setSectionOutlinesVisible(false);
    plot.setNoDataMessage(“No data available”);
    plot.setLabelGenerator(null);
    //plot.setSimpleLabels(true);
    plot.setBackgroundAlpha(0);
    plot.setForegroundAlpha(0.6f);
    plot.setOutlineStroke(null);
    plot.setOutlineVisible(false);
    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;
    resourceResponse.setContentType(“image/png”);
    JFreeChart chart=createChart(new Integer(onair),new Integer(inerror),new Integer(inplanning));
    OutputStream out=resourceResponse.getPortletOutputStream();
    ChartUtilities.writeChartAsPNG(out, chart, width, height,true,0);

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>