Copyright 2007, J4L
Components (http://www.java4less.com)
Go bak to contents
While applets are small Java programs that run in the browser (client), servlets and JSP pages are server side programs which run in the web server and the output in GIF, PNG or JPEG format is sent to the browser. RChart can run as servlet or inside JSP pages. The product includes ready to use examples for both cases.
The servlet class is called
com.java4less.rchart.web.ChartServlet. Basically you just need to call
the servlet and send the chart's parameters either using the GET or the POST
method. However the recommended way is using the DATAFILE parameter to tell
the servlet where it can get the parameters from (either from a file or a url).
In all cases the servlet supports 3 additional parameters:
Example, Tomcat 5:
In order to run the servlet using Tomcat 5 you can follow these steps:
If you have any problems running the servlet check the following:
JSP pages are more flexible than the ChartServlet but you need to deal with a bit of Java/JSP programming. RChart however includes a small JSP application which can be used with none or little modification.
A simple JSP page to display
a chart is:
<%
String encode="jpeg";
String myurl="http://localhost:8080/rchart2/";
String dataFile="combined2Positions.txt";// set response headers
response.setContentType("image/"+encode);
response.setDateHeader ("Expires",0);// get output stream
java.io.OutputStream outb=response.getOutputStream();
// create Chart from data file
com.java4less.rchart.ChartLoader loader=new com.java4less.rchart.ChartLoader();
loader.loadFromFile(myurl+dataFile,false);
com.java4less.rchart.Chart chart=loader.build(false,false);// set size of the chart
chart.setSize(500,500);// encode image and send output to the browser
chart.saveToFile(outb,encode);
outb.close();
%>
Example, Tomcat 5:
In order to run the JSP
examples using Tomcat 5 you can follow these steps:
Note: You can also use RChart JSP Tag library to create charts without any java code.
RChart can create image maps for the chart images which provides you with the following features:
The following parameters are used for defining tips and links:
In order to create a chart and its associate image map you need 2 servlets or
2 JSP pages. One of them is in charge of creating the chart's image and the
other is in charge of creating the image map.
You can use the html links to open a subchart when the user clicks on a given point of the chart, in this way you are creating drill-down charts.
JSP pages
The following code creates a chart and the image map. Note that the page itself only creates the HTML image map and a IMG tag which calls a second JSP script that will deliver the JPEG image (see "examples_jsp" directory).
<%
String encode="jpeg";
String myurl="http://localhost:8080/rchart2/";
String dataFile="combined2Positions.txt";
response.setContentType("text/html");
response.setDateHeader ("Expires",0);// get output stream
java.io.PrintWriter outb=response.getWriter();// create Chart from data file
com.java4less.rchart.ChartLoader loader=new com.java4less.rchart.ChartLoader();
loader.loadFromFile(myurl+dataFile,false);
com.java4less.rchart.Chart chart=loader.build(false,false);// set size of the chart
chart.setSize(w,h);// we must paint the chart once before we can retrieve the image map
com.java4less.rchart.gc.ChartImage chartImage= com.java4less.rchart.gc.GraphicsProvider.createImage(w,h);
com.java4less.rchart.gc.ChartGraphics chartGraphics=chartImage.getGraphics();
chart.paint(chartGraphics);
chartGraphics.dispose();
chartImage.dispose();
// retrieve image map
String htmlMap=chart.getHTMLImageMap("Chart");// html output
outb.print("<html>");
outb.print("<body>");
// IMG tag which includes the link to the second JSP page
outb.print("<img src='getChart.jsp?chart="+dataFile+"' BORDER=0 NAME='ICHART' USEMAP='#Chart'>");
// output image map now
outb.print(htmlMap);
outb.print("</body>");
outb.print("</html>");
outb.close();
%>
Servlet
RChart only includes a servlet which can create image maps. The name of the servlet is com.java4less.rchart.web.ChartMapServlet and must be used in the same way as the ChartServlet. Note that he ChartMapServlet creates the image map only, not the image itself.
In some applications you need the chart to be continuosly be updated with new data. The following two files in the "examples_jsp" directory shows you how to do it:
Note that the browser must be Javascript enabled to support this feature. The Javascript function for refreshing the chart is very simple:
function refreshChart(){
document.ICHART.src='getDynamicChart.jsp';
setTimeout("refreshChart()",3000);
}
the function calls itself every 5 seconds and reload the image called ICHART.
The image is defined as:
<img src='getDynamicChart.jsp' BORDER=0 NAME='ICHART'>
If you run RChart on a Unix or Linux server you must take into account the following. Java graphics classes require a X11 server to be running. This is a well known java problem.
If you are not running a X11 server you will get errors like:
Exception java.lang.NoClassDefFoundError
at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:120)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment (GraphicsEnvironment.java:58)
....
or
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
.
or similar.
The most common solutions to this problem are: