Visual Mining contact us | site map | search

Products Solutions Resource Library Services Customers Partners Developers Company
Developers

NetCharts® Pro and a Simple Java Servlet

This servlet produces an HTML page that contains an image of a chart generated by NetCharts Pro. The HTML implements drill down functionality. This servlet gets called twice in order to completely render a page containing a chart image. The first pass fetches the chart and returns an HTML page. The second pass provides the image data. The source code is below.

NetCharts Pro Simple Servlet Example

Source Code

/*
SimpleServlet
Visual Mining, Inc.

This servlet produces an HTML page that contains an image of a
chart generated by NetCharts Pro. The HTML implements drill
down functionality.

This servlet uses the NFServletUtil class to simplify
the coding needed to produce an HTML page containing an interactive
(drill down and popup) chart image. See ServletPageExample for an
example which explicitly creates interactive chart images.

This servlet gets called twice in order to completely render a page
containing a chart image. The first pass fetches the chart and
returns an HTML page. The second pass provides the image data.
*/

package ncpro.examples.servlet;

import java.io.*;
import java.awt.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import netcharts.pro.common.*;
import netcharts.pro.util.*;

public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// If the browser is asking for the image data after loading
// the HMTL we sent it, then send the image data
if (NFServletUtil.isSecondPass(request)) {
NFServletUtil.writeImage(request, response);
} else {
response.setContentType("text/html");
NFGraph chart = null;
try {
ServletOutputStream os = response.getOutputStream();
os.println("<html>");
os.println("<body>");
// create a chart object from the template
chart = NFGraph.getGraphFromTemplate(getDefaultChart());
// generate an image and image map.
String page = NFServletUtil.getDrillDownPage(chart, request);
// output the generated page.
os.println(page);
os.println("</body>");
os.println("</html>");
// release graph for garbage collection.
chart.stop();
chart = null;
} catch (Exception ex) {
writeOutputError(response.getOutputStream(), "Chart creation failed", ex);
return;
}
}
}

private String getDefaultChart() {
// Get the servlets default chart template.
// This could be a file loaded from disk.
// For this example, we'll hardcode it into a string.
// The chart is defined in Visual Mining's Chart Definition
// Language (CDL), a simple ASCII scripting language.
// These templates can be created manually, or with
// ChartWorks Designer. A description of CDL is
// available in the Chart Definition Language Reference Manual.
return "ChartType = BARCHART;" +
"Background = (white, NONE, 0); "+
"Grid = (NULL,white, dimgray, mull, SIZE); "+
"Header = (\"Weekday Network Load\", black, \"Helvetica\", 18); "+
"BottomTics = (ON, black, \"Helvetica\", 11);"+
"LeftTics = (ON, black, \"Helvetica\", 11);"+
"LeftScale = (0, 100);"+
"BottomScale = (-0.5, 4.5);"+
"LeftFormat = (INTEGER);"+
"LeftTitle = (\"MBytes\nPer Sec\", black, \"Helvetica\", 11);"+
"RightTitle = (\" \", black, \"Helvetica\", 11);"+
"DwellLabel = (\"\", black, \"Courier\", 12);"+
"BarLabels = \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\";"+
"Bar3DDepth = 3;"+
"DataSets = (\"Server #1\", x4E60A8);"+
"DataSet1 = 55, 40,25,70,20";
}

private void writeOutputError(ServletOutputStream os, String message, Exception ex) throws ServletException
{
Throwable t = ex;
System.out.println("ex is "+ex);
if (ex instanceof java.lang.reflect.InvocationTargetException)
t = ((java.lang.reflect.InvocationTargetException)ex).getTargetException();
try {
os.println("<html>");
os.println("<h2>Chart Request failed</h2>");
os.println("<hr>"+message+"<hr>");
os.println("<pre>"+t.getMessage()+"</pre>");
os.println("</html>");
t.printStackTrace();
} catch (Exception d){
throw new ServletException ("Can't write error message "+message);
}
}
}



© 2008 Visual Mining, Inc. All rights reserved.
1-800-308-0731 | info@visualmining.com | privacy statement | legal
15825 Shady Grove Rd., Suite 20, Rockville, MD 20850 USA

Quote: Newview Technologies Inc.