JetChart provides built-in encoders to export charts as GIF, JPEG and PNG images.
In addition to using these encoders, it is also possible to export images using
the Java Advanced Imaging(JAI) package, available for download at
http://www.javasoft.com.
The Java Advanced Imaging API (JAI) implements a JPEG and a PNG encoders. The example below demonstrates
how to configure a servlet to export an anti-aliased JPEG image of a pie chart using Java2D.
import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import com.jinsight.jetchart.*; import java.io.*; import java.awt.image.BufferedImage; import java.util.Locale; import com.sun.media.jai.codec.*; public class Example3 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { // Sets the response content type. res.setContentType("image/jpeg"); // Gets a reference to the binary output stream. ServletOutputStream out=res.getOutputStream(); // Creates the chart context. PieGraph graph=new PieGraph(); graph.set3DEnabled(true); graph.setTitle(new String[]{"The JetChart Library","Using the Java Advanced Imaging(JAI) API"}); // Sets the size of the chart context and enables offscreen graph generation. These two methods must // always be invoked when using JetChart with servlets. graph.setSize(500,400); graph.setOffScreenGraphEnabled(true); // Some JetChart classes place calls to the method Graph.getLocale(), which only returns // a valid Locale object if the Graph object has been laid out on a container, like a // Frame or JFrame object. If the Graph object has not a parent, an exception is then // raised when the Graph.paint(Graphics g) method is invoked to paint chart on the // BufferedImage, telling that Graph must have a parent in order to determine its // locale. If we set the Graph object locale as below, the exception is not raised. graph.setLocale(Locale.getDefault()); // If a BufferedImage object is used to generate offscreen images, the method below // must always be set to true. graph.setBufferedImageEnabled(true,BufferedImage.TYPE_INT_RGB); // Creates a pie series and adds it to the chart context. PieSerie ps=new PieSerie(); ps.set3DDepth(20); graph.addSerie(ps); // Creates slices and adds them to the pie series. String[] titles={"slice1","slice2","slice3","slice4","slice5","slice6","slice7"}; double[] values={220,180,250,140,100,200,210}; Color[] colors={Color.red,Color.blue,Color.yellow,Color.green,Color.magenta,Color.orange,Color.cyan}; for (int counter=0;counter<values.length;counter++) { Slice slice=new Slice(values[counter],titles[counter],colors[counter]); // Explodes slice a little bit. slice.setPosition(10); // Enables slice legend. SliceLegend sl=slice.getSliceLegend(); sl.setEnabled(true); sl.setFont(new Font("SansSerif",Font.PLAIN,12)); sl.setOpacityEnabled(true); ps.addSlice(slice); } // The BufferedImage object dimension has to be equal to the Graph object dimension. BufferedImage bi=new BufferedImage(500,400,BufferedImage.TYPE_INT_RGB); Graphics2D g=bi.createGraphics(); // Enables antialiasing. Antialiasing eliminates the jagged edges. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); // Paints chart on the offscreen image. graph.paint(g); // Creates an object that encapsulates the common functionalities of JPEG encoding and // sets the JPEG image quality to 100 %. JPEGEncodeParam jpg=new JPEGEncodeParam(); jpg.setQuality(1.0f); // Creates a JPEG encoder. ImageEncoder encoder=ImageCodec.createImageEncoder("JPEG",out,jpg); // Encodes the BufferedImage object. encoder.encode(bi); out.close(); } }