{"id":810,"date":"2006-06-09T19:51:05","date_gmt":"2006-06-09T23:51:05","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=810"},"modified":"2006-06-09T19:51:05","modified_gmt":"2006-06-09T23:51:05","slug":"transparent-png-charts-with-jfreechart","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2006\/06\/09\/transparent-png-charts-with-jfreechart\/","title":{"rendered":"Transparent PNG Charts with JFreeChart"},"content":{"rendered":"<p>If you&#8217;re one of the 3 people in the world that need to create a transparent PNG image using <a href=\"http:\/\/www.jfree.org\/jfreechart\/\">JFreeChart<\/a>, you&#8217;ve come to the right place:<br \/>\n<code><br \/>\nJFreeChart chart = ChartFactory.createXYBarChart(...);<br \/>\nchart.setBackgroundPaint(new Color(255,255,255,0));<br \/>\n...<br \/>\nKeypointPNGEncoderAdapter encoder = new KeypointPNGEncoderAdapter();<br \/>\nencoder.setEncodingAlpha(true);<br \/>\nencoder.encode(chart.createBufferedImage(width, height, BufferedImage.BITMASK, null));<br \/>\n<\/code><br \/>\nThe key is that you must use the <a href=\"http:\/\/www.jfree.org\/jfreechart\/api\/gjdoc\/org\/jfree\/chart\/encoders\/KeypointPNGEncoderAdapter.html\">KeyPointPNGEncoderAdapter<\/a>, which is <a href=\"http:\/\/www.jfree.org\/phpBB2\/viewtopic.php?t=7700&amp;highlight=keypoint\">slower for big charts<\/a>, 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 <a href=\"http:\/\/www.jfree.org\/jfreechart\/api\/gjdoc\/org\/jfree\/chart\/encoders\/ImageEncoderFactory.html\">ImageEncoderFactory<\/a> will return the <a href=\"http:\/\/www.jfree.org\/jfreechart\/api\/gjdoc\/org\/jfree\/chart\/encoders\/SunPNGEncoderAdapter.html\">SunPNGEncoderAdapter<\/a>, which does not support alpha transparency.<\/p>\n<p>Also, if you&#8217;re planning on using alpha transparency in a web application that needs to support IE, you&#8217;ll want to check out <a href=\"http:\/\/homepage.ntlworld.com\/bobosola\/index.htm\">this JavaScript fix<\/a> by Bob Osola.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re one of the 3 people in the world that need to create a transparent PNG image using JFreeChart, you&#8217;ve come to the right place: JFreeChart chart = ChartFactory.createXYBarChart(&#8230;); chart.setBackgroundPaint(new Color(255,255,255,0)); &#8230; 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 &hellip; <a href=\"https:\/\/cephas.net\/blog\/2006\/06\/09\/transparent-png-charts-with-jfreechart\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Transparent PNG Charts with JFreeChart<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/810"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=810"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/810\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}