9.  Creating server side graphics and forms 
9.1  Foreword 
This chapter provides some insight about how to create server side graphs and forms. These examples are 
quite rough and they should not be taken  as good all around solutions. There are many third party tools 
that can be used to create better looking graphics and maybe a bit easier, too. 
9.2  Graphs 
The basic idea is to use a third party graph toolset to create your desired graph and then send the graph 
image to the client browser. There are two options in getting the result image back to client browser: Either 
(a) stream it directly using a 
HttpServletResponse 
object provided by JSP container or (b) store the 
image to a file and provide a link to the file back to the client browser. Followed are examples of both by 
creating a simple graph with Sitraka JClass Chart tools 
(
com.klg.jclass.chart.beans.SimpleChart
). At least JBuilder6 Pro is shipped with a development 
version of the tools. 
9.2.1  Example of streaming a graph image back to the client browser 
The first example shows a JSP listing of how to create the graph, encode it to a PNG image and how to 
stream it to the client browser. The listing uses a tool function shipped with e FixIT 4.2 that wraps around 
Java Advanced Imaging (JAI) 1.1 tools. The tools are freely available at 
http://java.sun.com/products/java 
media/jai/
. Many commercial graph creation tools also contain methods to converting graph  to several 
image formats.  
 
<% 
  response.reset(); 
/* Init the chart object and set it's size */ 
  com.klg.jclass.chart.beans.SimpleChart chart = new 
com.klg.jclass.chart.beans.SimpleChart(); 
  chart.setSize(new java.awt.Dimension(500,300)); 
/* Fetch the graph data using a XfijFinder component. The graph will show 
lengths of lab test results (field number  6  in file  80001 ) in a simple plot 
graph connected with lines. */ 
  xfij.core.XfijFinder finder = new xfij.core.XfijFinder(); 
  finder.setFileNumber(new xfij.editor.FileNumber("80001")); 
  finder.setWRPCBroker(laboratoriotutkimusBean.getBroker()); 
  finder.setFields(new String[] { "6" }); 
  String[] results = finder.getList(); 
/* Create a TableModel that is used as data source by the graph component*/
  javax.swing.table.TableModel model = 
xfij.util.ChartUtil.ResultsToTableModel(results, false); 
  chart.setSwingDataModel(model); 
  chart.setXAxisTitleText("Record number"); 
  chart.setYAxisTitleText("The length of result"); 
  chart.repaint(); 
/* Create the source image and stream it as PNG image ( testImage.png ) back to 
the client browser. The browser detects the stream and will ask for the user to 
redirect the PNG image*/ 
  xfij.util.ChartUtil.encode(chart.serverSnapshot(),"PNG","testImage",response); 
%> 
32 






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

servlets web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved