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.
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);
And your lucky 3rd person comes from Australia! Thanks for this tip, mate. Just what I was after. Made my day.
So im the third person using it? 🙂
That did the trick! Thanks Aaron.
Hi, I am fourth person… 🙂
5th person, thanks!
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.
Your count is way off… I’m #6.
Add me to the list — thanks!
Thanks from the Netherlands
Another one from Italy!
Thank you for the tip!
“Steven Hess” is 7th … I’m #8, yeah we’ll change the world :P.
Thanks from Germany !
Thanks!
one more to your list, thanks
Thank you from number >3 🙂
Hi, I’am one of thoses 3 \o/
Awesome!. Thanks for that.
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);
Thanks. This solved my blurry png problem.
wow, like none of these example work or even refer to the latest api