Charting overview

A servlet handles an HTTP request for generation of charts by basically following the steps outlined below:

  1. Gets the chart parameters from the HttpServletRequest object.
  2. Creates a chart using the servlet parameters.
  3. Encodes chart into a GIF, JPEG, PNG or SVG image.
  4. Outputs the encoded image back to the remote client over a ServletOutputStream object or
  5. Outputs the encoded image into a file, inserting a reference to the file in the servlet generated HTML code to be sent back to the remote client.

The charts generated by JetChart are painted on the graphical context of the Graph, ScatterGraph or PieGraph classes, found in the package com.jinsight.jetchart. These classes descend from java.awt.Panel, which has to be laid out and displayed on a valid container, like a java.awt.Frame object. However, servlets are Java applications typically running in a headless server, where no display or graphical environment is available, therefore requiring the generation of offscreen images using any of the following approaches:

  1. Create an invisible container, on which the chart context is laid out. The ChartEncoder class internally creates a Frame object, adding an instance of Graph, ScatterGraph or PieGraph to it. Additionally, the ChartEncoder class is a wrapper around the API of the image encoders and implements methods to trigger the encoding process. The Frame object is also used to create the offscreen image using a java.awt.Image instance, to be passed to an image encoder and outputed as a GIF, JPEG or PNG binary stream.

  2. Create a java.awt.image.BufferedImage object and draw chart on it, directly using one of the available image encoders to encode the BufferedImage object. The following classes implement image encoders:

    1. GifEncoder - Outputs an Image object as a GIF encoded binary stream.
    2. JpegEncoder - Outputs an Image object as a JPEG encoded binary stream.
    3. PngEncoder - Outputs an Image object as a PNG encoded binary stream.

    The BufferedImage class has a constructor and does not rely on a subclass of java.awt.Component to be created, as the first approach describes. However, only JDK 1.2 and newer versions implement the BufferedImage class.

  3. Create a java.awt.BufferedImage object and draw chart on it, using the Java Advanced Imaging(JAI) API to encode chart into a JPEG or PNG image.

These approaches are illustrated through this tutorial.

As to SVG encoding, JetChart implements the class com.jinsight.svg.SVGGraphics, a subclass of java.awt.Graphics, to generate the SVG code corresponding to the graphics obtained using the drawing methods available in the Java language. An instance of SVGGraphics is created by the ChartEncoder class at the moment the encoding process is triggered, and passed to the paint(Graphics g) method of a chart context, as Graph, ScatterGraph or PieGraph. After execution of the paint(Graphics g) method, a complete SVG description of the chart submitted to ChartEncoder can be outputted to a file or delivered to an output stream.

This tutorial contains a topic detailing the generation of SVG images.