The ChartEncoder class is a wrapper around the API of the image encoders available, and also
provides the details associated with the generation of an offscreen Image object on which a chart
is painted by subclasses of GenericGraph.
The constructor of the ChartEncoder class receives an instance of a GenericGraph subclass, like
a Graph, ScatterGraph or PieGraph object. The encoding process is then started by invoking one of the encoding
methods implemented by the ChartEncoder class.
Before start encoding, the ChartEncoder object creates an invisible Frame, which is disposed at the end
of the encoding process. The GenericGraph instance passed to the ChartEncoder constructor is laid out on the
Frame object and an offscreen image is created with the method java.awt.Frame.createImage(int width,int height),
inherited from the java.awt.Component class, passing the GenericGraph object dimensions. Then, a reference to the
java.awt.Graphics object of the offscreen image is passed to the paint(Graphics g)
method of the GenericGraph object and chart is painted on it. At the end of the process, the offscreen image is passed to an
image encoder and is finally outputed as a GIF, JPEG or PNG binary stream.
The methods below must always be invoked on the GenericGraph instance when generating offscreen images:
import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import com.jinsight.jetchart.*; import java.io.*; public class Example1 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { // Sets the response content type. res.setContentType("image/jpeg"); // If a GIF or PNG format is desired, disable the line above and enable one of the lines below. // res.setContentType("image/gif"); //res.setContentType("image/png"); // Gets a reference to the binary output stream. ServletOutputStream out=res.getOutputStream(); // Creates the chart context. String[] labels={"label1","label2","label3","label4","label5","label6","label7"}; Graph graph=new Graph(labels); graph.setTitle(new String[]{"The JetChart Library","The ChartEncoder class"}); // 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,350); graph.setOffScreenGraphEnabled(true); // Creates a bar series. double[] values={220,180,250,140,100,200,210}; BarSerie bs=new BarSerie(values,"Bar series"); bs.setColor(Color.green); bs.setWidth(15); graph.addSerie(bs); // Creates the ChartEncoder object and encodes chart into a JPEG binary stream, setting the image // quality to 85%. ChartEncoder ce=new ChartEncoder(graph); ce.jpegEncode(out,85); // If GIF or PNG encoding is desired, disable the line above and enable one of the lines below. //ce.gifEncode(out); //ce.pngEncode(out,9); out.close(); } }