NetCharts® Pro: DrillDownExample.java
The complete source code:
/*
DrillDownExample
Visual Mining, Inc.
This servlet produces an HTML page that contains a drillable image of a
chart generated by NetCharts Beans.This servlet gets called twice in order to completely render a page
containing a chart image. The first pass creates the chart and
returns an HTML page that contains a reference to the chart. The
second pass provides the actual image data.Pass 1:
Creates an instance of a sample Chart Repository
Retrieves an initial chart template
Creates an instance of the NetCharts Pro chart using the initial Chart Template
Creates an instance of a DynamicChartData
Generates dynamic chart data based upon the query parameters to include in the chart template
Creates an instance of an OptionalChartData
Generate additional chart data to include within the chart template
Instruct the chart to redraw itself using all the newly defined parameters
Generated a NFServerGenerateImage
Stores the NFServerGenerateImage in the session variable
Creates an instance of the NFImageMapCreator
Generates the proper HTML formated imagemap
Returns the proper HTML that contains an IMG tag for the chart.
- The IMG SRC tag should contain a reference back to this servlet.Pass 2:
Extract the NFServerGenerateImage instance from the servlet session.
Deliver the image data to the client.
*/
package ncpro.examples.servlet.drilldown;
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import netcharts.pro.common.*;
import javax.servlet.http.HttpServlet;
import netcharts.pro.util.*;public class DrillDownExample extends HttpServlet {
protected static final String CONTENT_TYPE = "text/html";
protected static String DEFAULT_IMAGE_TYPE = "image/png";static {
NFGraph.setLicenseKey("NetChartsPro4.0 EXPIRATION=30-JUN-2003 KEY=ZEPEXAGGNNBKFKXENZNZPCANCEEAGPAJ");
NFGraph.setLicenseKey("NetCharts4.0 EXPIRATION=30-JUN-2003 BANNER=NO KEY=PPDBCXJBAFMYMPHKBDGCCCGAGZAKKDPX");
}public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
// Check to see if this is an initial startup request.
if (null==request.getQueryString()) {
request.getRequestDispatcher("/example.jsp").forward(request, response);
return;
}// Check to see if we have already delivered an HTML page and are now requesting the generated image.
if(NFServletUtil.isSecondPass(request)) {
// The request contains a session variable referencing the image.
// write the image to the output.
NFServletUtil.writeImage(request, response);
return;
}// Create a ChartRepository object to retrieve the Chart Template.
ChartRepository chartRepository = new ChartRepository();// Request the Chart Template.
NFGraph chart = null;
try {
chart = NFGraph.getGraphFromTemplate(chartRepository.getChartTemplate());
} catch (Exception ex) {
writeErrorMessage(response, ex);
return;
}// Don't update the underlying graphic until we are done.
chart.setAlwaysUpdate(false);// Create a DynamicChartData instance to set the dynamic Chart Template fragments.
DynamicChartData dChartData = new DynamicChartData(getServletName(request));// Set the dynamic Chart Template fragments.
dChartData.setDynamicChartData(request, chart);// Set any Optional Chart Template fragments.
OptionalChartData oChartData = new OptionalChartData();
oChartData.setOptionalChartData(chart);// Instruct the chart to redraw based upon the new parameters.
chart.sendUpdate();// Generate and store the image. Image data can be written to disk
// or saved in the session. Here we save the image
// to a NFServerGeneratedImage which will be stored in a session variable.
NFServerGeneratedImage sgi = null;
try {
sgi = NFImageGeneration.getServerGeneratedImage(chart, DEFAULT_IMAGE_TYPE, "", true);
} catch (Exception e) {
writeErrorMessage(response, e);
return;
}// Store the ServerGeneratedImage object in a session variable as well as the image name.
// This will allow us to retrieve the genereated image data during the return call
// specified in the <img> elements 'src' attribute.
HttpSession session = request.getSession(true);
session.setAttribute(sgi.getUniqueID(), sgi);// Create the image source tag using the unique ID of the ServerGeneratedImage as the identifier for the return call to retrieve the image and the image map name.
String imgSrc = ("<img src=\"" + getServletName(request) + "?image=" + sgi.getUniqueID() + "\" border=0 USEMAP=\"#" + sgi.getUniqueID() + "\">");// During the creation of the image map, the ImageMapCreator allows the developer to specify
// functions that will be performed during the onMouseOver and onMouseOut events of the
// <area> elements. We will use these functions, written in JavaScript and contained within
// the JavaScriptRepository, to show a "rollover" label over each <area> element.
String onMouseOver = "displayActiveLabel('" + sgi.getUniqueID() +"', '" + NFImageMapCreator.LABEL_REPLACE + "');";
String onMouseOut = "unDisplayActiveLabel();";// Create the image map tag.
String mapSrc = NFImageMapCreator.createImageMap(sgi.getMap(), sgi.getUniqueID(), onMouseOver, onMouseOut, null);// Package and send the HTML response.
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>DrillDown Example</title></head>");
out.println("<body>");
// Request the JavaScript to include within the page.
JavaScriptRepository jRepository = new JavaScriptRepository();
out.print(jRepository.getJavaScript(sgi.getUniqueID()));
out.println(imgSrc);
out.println(mapSrc);
out.println("</body></html>");// Release graph for garbage collection.
chart.stop();
chart = null;
}/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}/**Retrieve the servlet URL*/
protected String getServletName(HttpServletRequest request)
{
StringBuffer sb = new StringBuffer();
sb.append(request.getScheme());
sb.append("://");
sb.append(request.getHeader("Host"));
sb.append(request.getRequestURI());
return sb.toString();
}/**Retrieve the webapp URL*/
protected String getWebAppName(HttpServletRequest request)
{
StringBuffer sb = new StringBuffer();
sb.append(request.getScheme());
sb.append("://");
sb.append(request.getHeader("Host"));
sb.append(request.getContextPath());
return sb.toString();
}
/**Send an error message via the HttpServletResponse*/
protected void writeErrorMessage(HttpServletResponse response, String msg)
{
try {
PrintWriter out = response.getWriter();
out.println("Error: "+msg);
} catch(Exception exc) {
System.out.println(msg);
System.out.println("[writeErrorMessage] error writing error message");
exc.printStackTrace();
}
}/**Send an error message via the HttpServletResponse*/
protected void writeErrorMessage(HttpServletResponse response, Exception exception)
{
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);exception.printStackTrace(pw);
pw.close();
sw.close();writeErrorMessage(response, sw.toString());
} catch(Exception exc) {
System.out.println(exception.getMessage());
System.out.println("[writeErrorMessage] error writing error message");exc.printStackTrace();
}
}/**Clean up resources*/
public void destroy() {
}
}
See also the associated Helper Applications code:
-
NetCharts Pro: DynamicChartData.java
(A Helper Application) -
NetCharts Pro: ChartRepository.java
(A Helper Application) -
NetCharts Pro: JavaScriptRepository.java
(A Helper Application) -
NetCharts Pro: OptionalChartData.java
(A Helper Application)



