11.3. Saving a Chart to File versus Sending it to a Browser

You can use both servlets and JSPs to return a chart back to the browser. The chart can either be shown in an applet (i.e., it can be interactive) or be shown as a static image (i.e., a jpeg, a png etc.). Net traffic and bandwidth availability should be the key factors in deciding whether to use applets or static images.

The charts returned to the browsers can be done in two ways: they can be saved to a file or sent directly to the browser. In the first scenario, the chart is saved on the server side and the file location is sent to the client browser which then shows the chart. In the second scenario, the chart itself is sent, either as a string or as an output stream, to the client browser.

11.3.1. Saving the Chart to a File

Servlets and JSPs can save charts to files if the charts shown need to be re-used or a copy is needed. Here, charts are saved to a file on the server side (whether the chart is shown in an applet or as a static image is immaterial). However, these types of servlets and JSPs need to be constructed carefully. If multiple clients hit the page, it is possible for one client to see another client's chart. The browsers might also sometimes cache a previous chart (in the case of a static image) and they will not show the new chart unless they refreshed/reloaded. Since the files are saved on the server side, one must be careful not to override the files before the client browser has seen the chart. They must purged periodically in order to avoid taking up server space.

In both cases, a chart is exported to the desired format.

QbChart chart = new QbChart (...........);
....
....
....
int format = QbChart.CHT;    // here set to an interactive chart.  For a static chart change to QbChart.JPEG
chart.export(format, "chart", 500, 500);

An HTML file can then be returned to the browser, which contains the chart location within the HTML file so that the browser can view it.

<applet code="quadbase.chartviewer.Viewer.class" width=650 height=650 archive="http://<machineName>/EspressChart/lib/EspressViewer.jar">
<PARAM name="filename" value="chart.cht">
</applet>

Chart Viewer has been used here. Please note that in the above case, EspressManager MUST be running.

In the case of a static image, a simple <img src> tag giving the location of the static image is sufficient to display the chart.

Using this method, you can separate the pages returning the chart from your servlet/JSP and design them well in advance and outside of your servlet/JSP. You can use a database or a hashtable to keep track of the charts generated and show them again as needed, as well as have the charts re-generated again periodically.

For examples, please refer to the files in the help/examples/servlets/DatabaseJPEG directory and help/examples/jsp/Chart directories.

11.3.2. Sending the Chart Directly to a Browser

Servlets and JSPs can send charts directly to the browser so that they need not be saved as files on the server side. Thus, disk space on the server is not being filled up and maintenance of files need not be done. However, a permanent copy of the chart cannot be made (unless it is printed from the browser). Therefore, the chart has to be re-generated if it is needed again.

Sending charts to the browser differs from saving them to files, as here, charts are sent either as an outputstream (in the case of static images) or as string (in the case of an interactive chart).

For a static image, on the servlet side, an outputstream is created and the chart is exported to the outputstream.

    public class OutputStreamServlet extends HttpServlet  {
     

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            // first, set the "content type" header of the response
            response.setContentType("image/html");
            // where response is the response to the servlet
            OutputStream toClient = res.getOutputStream();
            QbChart chart = new QbChart (...........);
            ....
            ....
            ....
            chart.export(QbChart.JPEG, toClient, 500, 500);

        }

    }

On the HTML part, an <img src> tag with the servlet enclosed is sufficient to view the chart image.

<HTML><BODY>

	<img src="http://machine_name:8080/servlet/OutputStreamServlet> 

</BODY></HTML> 

You can also output the map file as an outputstream and embed it within your generated HTML file. Generating the map file as an outputstream can be done using the following method in the QbChart class:

export(int format, OutpuStream image, OutputStream mapFile, String fileName, int w, int h, boolean generateMapFile, int option);

For an interactive chart, the chart is exported as a string.

QbChart chart = new QbChart (...........);
String buffer = chart.exportChartToString();
....
....
....

On the HTML side, you can use Chart Viewer to view the interactive chart. Here, the HTML is embedded in the servlet code itself as shown :-

toClient.println("<applet code=\"quadbase.chartviewer.Viewer\" width=600 height=600 " +
                  "archive=\"http://<machineName>/EspressEnterprise/EspressChart/lib
                   /EspressViewer.jar\">");
toClient.println("<PARAM name=\"ChartData\" value=\"" + chart.exportChartToString() +
                  "\">");
toClient.println("</applet>");

For examples, please refer to the files in the help/examples/servlets/StreamingJPEG and help/examples/servlets/StreamingChart directories.

On a JSP, you have to create a Java Bean object (myStreamChart), which creates a chart and returns it as a String object. The JSP page has to be modified as shown :-

<jsp:useBean id="myStreamChart" scope="page" class="streamingChart.CreateChart" />
<jsp:setProperty name="myStreamChart" property="*"/>
<applet code="quadbase.chartviewer.Viewer" width=600 height=600 archive="http://<machineName>/EspressChart/lib/EspressViewer.jar">
<PARAM name="ChartData" value="<%= myStreamChart.export()%>">
</applet>

where myStreamChart java bean will contain the following code :-

    public string export() throws Exception {

        QbChart chart = new QbChart(....);

        return chart.exportChartToString(); 

    } 

For an example, please refer to the files in help/examples/jsp/streamingChart.

In order to send a static image directly to the browser, a combination of JSP and servlet is used.