RCHART, User Guide

Copyright 2007, J4L Components (http://www.java4less.com)
Go bak to contents


JSP and Servlet

 

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.

Running as servlet

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:

  1. create a new empty directory for rchart in Tomcat: "tomcatdirectory\webapps\rchart2"
  2. copy the file "examples_servlet\web.xml" to "tomcatdirectory\webapps\rchart2\WEB-INF\web.xml"
  3. copy the areaCurves.txt file from the "data\examples" directory to "tomcatdirectory\webapps\rchart2".
  4. Copy the image files from the "data" directory to "tomcatdirectory\webapps\rchart2\WEB-INF\classes". This is only required if you want to view one of charts that uses a background image.
  5. copy bin\rchart2.jar to "tomcatdirectory\webapps\rchart2\WEB-INF\lib"
  6. start Tomcat
  7. Open the following URL in you browser:

    http://localhost:8080/rchart2/servlet/RChartServlet?DATAFILE=http://localhost:8080/rchart2/areaCurves.txt

    Note 1: if you want to use the PNG or the GIF encoder you must copy the jar file to "tomcatdirectory\webapps\rchart2\WEB-INF\lib"

If you have any problems running the servlet check the following:

 

Running in JSP pages

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:

  1. create a new empty directory for rchart in tomcat: "tomcatdirectory\webapps\rchart2"
  2. copy all files from the "data\examples" directory to "tomcatdirectory\webapps\rchart2".
  3. Copy all image files from the "data" directory to "tomcatdirectory\webapps\rchart2\WEB-INF\classes".
  4. copy bin\rchart2.jar to "tomcatdirectory\webapps\rchart2\WEB-INF\lib"
  5. start Tomcat
  6. Open the following URL in you browser:

    http://localhost:8080/rchart2/Examples.htm

    Note 1: if you want to use the PNG or the GIF encoder you must copy the jar file to "tomcatdirectory\webapps\rchart2\WEB-INF\lib"and change the following line in all JSP pages:

    String encode="jpeg";

 

Note: You can also use RChart JSP Tag library to create charts without any java code.

 

Interactive web charts

RChart can create image maps for the chart images which provides you with the following features:

  1. Tooltips are shown when the mouse is placed over a bar or point.
  2. HTML links: a new html link can be openned when the user click's on a point in the chart.
  3. Actions: for example javascript functions can be executed when the user click's on a point in the chart.

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.

 

Realtime web charts

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:


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'>

 

Unix and Linux servers

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: