Application Techniques



Graphing Data Using a Servlet that Calls a JavaBean

How to create a page that loads an image from a servlet that invokes the services of a JavaBean.

About this technique

Details

Category

HTML Client Techniques> Miscellaneous

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on using utility classes, JAR files, and JavaBeans in the Programmer's Guide

This technique uses a servlet called srvletGifEncoder to load a graph into a SilverStream page. The servlet delegates responsibility for creating the chart image to a JavaBean called JCChart, which is provided by KLG. The servlet gets the data for the chart from the database by using two AgaData objects (dataOrderDetails and dataCategories). Once the image has been created, the servlet invokes the services of a class called GifEncoder, which is provided by Acme, to generate the GIF from the image.

NOTE   In this example, the JavaBean is instantiated on the server, not the client. In addition, the JavaBean is multithreaded to allow the resource to be shared. Since the server owns the reference to the object, the Java window that pops up on the server will not be closed until the server shuts down.

Loading the image from the servlet   Top of page

The pageLoaded event and the pageActionPerformed event for the Generate Graph button both set the source of the image to the URL for the servlet (GifEncoder).

The pageLoaded event sets the graph type to Variety (total by product category) and the chart type to Bar:

    rbBar.setState(true); 
    rbVariety.setState(true); 
    imgGraphs.setSrc("../../GIFEncoder?GT=V&CT=B"); 

The pageActionPerformed event for the Generate Graph button gets the graph type and chart type from the user and passes the user's settings to the servlet:

  if (rbVariety.getState()) 
      graphType="GT=V"; 
    else { 
      graphType="GT=R"; 
      rbBar.setState(true); 
    } 
     
    if (rbBar.getState()) 
      chartType="CT=B"; 
    if (rbPie.getState()) 
      chartType="CT=P"; 
    if (rbStacked.getState()) 
      chartType="CT=S"; 
     
    imgGraphs.setSrc("../../GIFEncoder?" +  
      graphType + "&" + chartType); 

Importing the Acme and KLG classes into the servlet   Top of page

The srvletGifEncoder servlet imports classes in the Acme.JPM.Encoders package, which was uploaded to the server in the Acme.jar file. This package contains the GifEncoder class. The servlet also imports classes in the jclass package, which was uploaded to the server in the jcchart361J.jar file. This package contains the JCChart JavaBean provided by KLG:

  ... 
  ... 
  import Acme.JPM.Encoders.*; 
  ... 
  //import KLG Stuff 
   
  import jclass.chart.*; 
  import jclass.util.JCEnvironment; 

Instantiating the JavaBean   Top of page

The srvletGifEncoder servlet defines a member variable called m_JCChart that contains a reference to the JCChart JavaBean:

  ... 
  ... 
  JCChart m_JCChart = new JCChart (); 
  ... 

Coding the service method on the servlet   Top of page

The service method in the srvletGifEncoder servlet checks the parameters passed into the servlet to see what graph type and chart type were requested by the page. Depending on the graph type requested, it calls one of several methods (totalByCategory(), revenueTotals(), or revenueEmployees()) to create the graph image. Then it passes the image returned to the constructor for the GifEncoder class and calls the encode() method on the GifEncoder class:

  if (TRACE_ON) 
    System.out.println(M_THIS + ".service()"); 
   
  synchronized(this) 
  { 
    AgoHttpRequestEvent request = (AgoHttpRequestEvent) req; 
    AgoHttpReplyEvent response = (AgoHttpReplyEvent) res; 
    m_sDBName =  
      ((AgoHttpRequestEvent)req).getDatabase().getName(); 
   
  // Find out what to graph 
  // GT   ==>  GraphType      CT  ==>  ChartType 
  // V    ==>  Variety        B   ==>  Bar 
  // R    ==>  Revenue        P   ==>  Pie 
  //                          S   ==>  Stacked 
    m_graphType = (String) request.getParameter("GT"); 
    m_chartType = (String) request.getParameter("CT"); 
   
    Image img = null; 
    if (m_graphType.equals("V")) 
      img = totalByCategory((AgoHttpRequestEvent)request); 
    if (m_graphType.equals("R")) 
      img = revenueTotals((AgoHttpRequestEvent) request); 
    if (m_graphType.equals("E")) 
      img = revenueEmployees(); 
   
    OutputStream out = response.getOutputStream(); 
    GifEncoder gif = new GifEncoder (img, out); 
    gif.encode(); 
   
    String mimetype = "image/gif"; 
    response.setContentType(mimetype); 
    response.setStatus(response.SC_OK); 
  } 

Creating the graph   Top of page

The totalByCategory() and revenueTotals() methods construct the graph image. Both of these methods make method calls to the JCChart object. For example, the totalByCategory() method calls methods on the JCChart object to set the size and the chart type, as well as other attributes of the chart image. Finally, the totalByCategory() method calls the snapshot() method to get back the Image object:

  ... 
  ... 
  m_JCChart.show(); 
   
  m_JCChart.setSize(new Dimension(400,300)); 
  m_panel.add (m_JCChart); 
  m_frame.add (m_panel); 
   
  try {	  
    StringBufferInputStream sbisEmpData = new 
      StringBufferInputStream (sbGraphData.toString()); 
    m_JCChart.getDataView(0).setDataSource ( new 
      InputStreamDataSource (sbisEmpData)); 
   
    // Figure out how to chart the data 
   
    if (m_chartType.equals("B")) 
      m_JCChart.getDataView(0).setChartType (JCChart.BAR); 
    if (m_chartType.equals("P")) 
      m_JCChart.getDataView(0).setChartType (JCChart.PIE); 
    if (m_chartType.equals("S")) 
      m_JCChart.getDataView(0).setChartType (JCChart.STACKING_BAR); 
    m_JCChart.getDataView(0).getYAxis().setGridIsShowing(true); 
    m_JCChart.getChartArea().setDepth(10); 
    m_JCChart.getChartArea().setRotation(10); 
    m_JCChart.getChartArea().setElevation(10); 
    m_JCChart.getHeader().getLabel().setText 
      ("[FONT=timesroman-bold-20] TOP FIVE (5) [COLOR=BLUE]  
      by Variety [DEFAULT_COLOR] [DEFAULT_FONT]", true); 
    m_JCChart.getFooter().setIsShowing(true); 
    m_JCChart.getFooter().getLabel().setText 
      ("KL Group JClass Chart JavBean", true); 
    m_JCChart.getLegend().setIsShowing(true); 
  } 
  catch (IOException _e){ 
    System.out.println (_e.toString()); 
  } 
   
  Image img = m_JCChart.snapshot(); 
  return img; 






Copyright © 2000, SilverStream Software, Inc. All rights reserved.