com.objectplanet.chart
Class ChartServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by com.objectplanet.chart.ChartServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class ChartServlet
extends javax.servlet.GenericServlet

This servlet generates a chart as a PNG image.

Author:
Bjorn J. Kvande.
See Also:
Serialized Form

Field Summary
protected  Chart chart
          The chart component used.
protected  ChartDataLoader dataLoader
          The data loader used to load chart parameters.
protected  java.lang.String encoding
          The encoding used to read data from an URL.
protected  java.lang.String format
          The image format to use.
protected  GaugeChart gaugeChart
          The gauge chart component used.
protected  java.util.Hashtable imageList
          The list of image parameters read.
protected  int jpegQuality
          The quality of the returned jpeg image.
protected  PlotterChart plotterChart
          The plotter chart component used.
protected  PngEncoder pngEncoder
          The png encoder used to encode the charts as png images.
protected  java.lang.String type
          The chart may be of "bar", "line", "pie", "time_line", "spline", "plotter" type.
protected  java.util.Hashtable urlList
          The list of url parameters read.
protected  java.util.Hashtable urlTargetList
          The list of url target parameters read.
 
Constructor Summary
ChartServlet()
           
 
Method Summary
protected static java.lang.String convertLineBreaks(java.lang.String label)
          Converts '\n' to
.
protected  java.lang.String convertURL(java.lang.String url, javax.servlet.ServletRequest req, java.lang.String separator, java.lang.String equals)
          Converts the data URL to use the standard parameter separator (&) instead of the one the servlet tag has to use (¤).
protected  void encodeChartImage(java.awt.Image image, int width, int height, java.io.OutputStream out, javax.servlet.ServletResponse res)
          Encodes the specified java image to a jpeg/png bytestream.
protected  void generateChartImage(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res, java.util.Hashtable parameters, java.lang.String error)
          Generates a chart image based on the given parameters
 void generateImagemap(java.lang.String type, javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
          Generates and prints out an imagemap to implement drilldown functionality for the chart.
protected  java.awt.Image getImage(java.awt.Component chart, int width, int height, boolean antialias)
          Returns the current chart as an image.
 void init(javax.servlet.ServletConfig config)
          This method is called when the servlet is placed into service.
protected  void loadInputParameters(java.io.InputStream input, java.util.Hashtable parameters, java.util.Hashtable urlList, java.util.Hashtable urlTargetList, java.util.Hashtable imageList)
          Loads the parameters from the given data input stream.
protected  void loadURLParameters(java.lang.String urlString, java.util.Hashtable parameters, java.util.Hashtable urlList, java.util.Hashtable urlTargetList, java.util.Hashtable imageList)
          Loads the parameters from the specified url.
protected  void paintError(java.awt.Graphics g, java.lang.String label)
          Paints a multiline label broken with \n.
protected  void paintLabel(java.awt.Graphics g, java.lang.String label, int label_x, int label_y)
          Paints a multiline label broken with \n.
 void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
          The service.
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

chart

protected Chart chart
The chart component used.


dataLoader

protected ChartDataLoader dataLoader
The data loader used to load chart parameters.


encoding

protected java.lang.String encoding
The encoding used to read data from an URL.


format

protected java.lang.String format
The image format to use.


gaugeChart

protected GaugeChart gaugeChart
The gauge chart component used.


imageList

protected java.util.Hashtable imageList
The list of image parameters read.


jpegQuality

protected int jpegQuality
The quality of the returned jpeg image.


plotterChart

protected PlotterChart plotterChart
The plotter chart component used.


pngEncoder

protected PngEncoder pngEncoder
The png encoder used to encode the charts as png images.


type

protected java.lang.String type
The chart may be of "bar", "line", "pie", "time_line", "spline", "plotter" type.


urlList

protected java.util.Hashtable urlList
The list of url parameters read.


urlTargetList

protected java.util.Hashtable urlTargetList
The list of url target parameters read.

Constructor Detail

ChartServlet

public ChartServlet()
Method Detail

convertLineBreaks

protected static java.lang.String convertLineBreaks(java.lang.String label)
Converts '\n' to
. Converts also the Unicode char sequences and uxxxx to valid chars.

Parameters:
label - The label with the \ and n.

convertURL

protected java.lang.String convertURL(java.lang.String url,
                                      javax.servlet.ServletRequest req,
                                      java.lang.String separator,
                                      java.lang.String equals)
Converts the data URL to use the standard parameter separator (&) instead of the one the servlet tag has to use (¤).

Parameters:
url - The data url to convert.
req - The servlet request.
separator - The separator used in the servlet tag.
equals - The equals sign.
Returns:
The converted URL.

encodeChartImage

protected void encodeChartImage(java.awt.Image image,
                                int width,
                                int height,
                                java.io.OutputStream out,
                                javax.servlet.ServletResponse res)
                         throws java.io.IOException
Encodes the specified java image to a jpeg/png bytestream. Make a subclass and override this method to support other image formats. The content type has to be set before the image is written to the output stream; res.setContentType("image/jpeg").

Parameters:
image - The image to encode.
width - The width of the image.
height - The height of the image.
out - The output stream to encode it to.
res - The servlet response.
Throws:
java.io.IOException

generateChartImage

protected void generateChartImage(javax.servlet.ServletRequest req,
                                  javax.servlet.ServletResponse res,
                                  java.util.Hashtable parameters,
                                  java.lang.String error)
                           throws java.io.IOException
Generates a chart image based on the given parameters

Parameters:
req - The servlet request.
res - The servlet response.
parameters - Paramters passed to the servlet.
error - Message to display on the image if an error happens.
Throws:
java.io.IOException - If an I/O exception has occurred.

generateImagemap

public void generateImagemap(java.lang.String type,
                             javax.servlet.ServletRequest req,
                             javax.servlet.ServletResponse res)
                      throws java.io.IOException
Generates and prints out an imagemap to implement drilldown functionality for the chart.

Parameters:
type - The type of the chart.
req - The servlet request.
res - The servlet response.
Throws:
java.io.IOException

getImage

protected java.awt.Image getImage(java.awt.Component chart,
                                  int width,
                                  int height,
                                  boolean antialias)
Returns the current chart as an image.

Parameters:
chart - The chart to get the image from.
width - The width of the chart in pixels.
height - The height of the chart in pixels.
antialias - True if antialiased, falst otherwise.
Returns:
The chart as an image object.

init

public void init(javax.servlet.ServletConfig config)
This method is called when the servlet is placed into service. It creates the bar/line/pie charts to be used for generating the chart images.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet

loadInputParameters

protected void loadInputParameters(java.io.InputStream input,
                                   java.util.Hashtable parameters,
                                   java.util.Hashtable urlList,
                                   java.util.Hashtable urlTargetList,
                                   java.util.Hashtable imageList)
                            throws java.lang.Exception
Loads the parameters from the given data input stream. The input stream is usually gotten from a valid URL or is forwarded by another component in this web application.

Parameters:
input - The input stream to read the data from.
parameters - Read the parameters into this hashtable.
urlList - Read the drilldown parameters into this hashtable.
urlTargetList - Read the url target parameters into this hashtable.
imageList - Read the image parameters into this hashtable.
Throws:
java.lang.Exception

loadURLParameters

protected void loadURLParameters(java.lang.String urlString,
                                 java.util.Hashtable parameters,
                                 java.util.Hashtable urlList,
                                 java.util.Hashtable urlTargetList,
                                 java.util.Hashtable imageList)
                          throws java.lang.Exception
Loads the parameters from the specified url. The URL must be a valid absolute URL, since this is a servlet running on some kind of server and does not have a document base or code base.

Parameters:
urlString - The url to load the data from.
parameters - Read the parameters into this hashtable.
urlList - Read the drilldown parameters into this hashtable.
urlTargetList - Read the url target parameters into this hashtable.
imageList - Read the image parameters into this hashtable.
Throws:
java.lang.Exception

paintError

protected void paintError(java.awt.Graphics g,
                          java.lang.String label)
Paints a multiline label broken with \n.

Parameters:
g - The graphics context used to paint with.
label - The label to paint.

paintLabel

protected void paintLabel(java.awt.Graphics g,
                          java.lang.String label,
                          int label_x,
                          int label_y)
Paints a multiline label broken with \n.

Parameters:
g - The graphics context used to paint with.
label - The label to paint.
label_x - The x position of the label.
label_y - The y position of the label.

service

public void service(javax.servlet.ServletRequest req,
                    javax.servlet.ServletResponse res)
             throws javax.servlet.ServletException,
                    java.io.IOException
The service.

Specified by:
service in interface javax.servlet.Servlet
Specified by:
service in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException - If the call could not be processed.
java.io.IOException