Advanced Bitstreaming

One disadvantage of the getHtmlData method is the lack of support for Image Maps. Additionally, in some cases, chart images are required to be stored in a certain way at specific locations, i.e. hourly generated charts. The getHtmlDataEx provides full control over the generation of the Chart Image, Image Tag and Image Maps.

Below is the usage for the getHtmlDateEx method:


public void getHtmlDataEx(java.lang.Object width,
java.lang.Object height,
java.lang.String tag,
java.io.OutputStream data,
java.io.OutputStream imgmap,
java.io.OutputStream tagText)

When compared to the getHtmlData method the main differences include:

1) getHtmlDataEx does not write any data in the response OutputStream. The getHtmlData method bitstreams the entire chart to the browser.

2) getHtmlDataEx includes three additional parameters which return the Chart Image, Image Maps and the Image Tag:

data: OutputStream that contains the image data.
imgmap: OutputStream that contains the image map information
tagText: OutputStream that contains the image tag information.

All three parameters must be initialized before calling the getHtmlDataEX method. They may be initialized as a ByteArrayOutputStream or FileOutputStream objects; a sample may be found later in this section.

Before calling getHtmlDataEx, is it necessary to set the Chart ID by calling the setID method; this ID will be used to identify the HTML maps and also the SCR field in the Image Tag. If not configured, the map name will be left blank For example:


chart1.setID("MyChart");

Will produce the following Image Map:


<IMG SRC="MyChart" usemap="#MyChart" border="0">

The following sample shows how to use the getHtmlDataEx in a servlet:


protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Create chart and load data
ChartServer chart1 = new ChartServer(this.getServletContext(),
request,response);
chart1.openData(COD.VALUES, 1, 3);
chart1.setValue(0, 0, 10);
chart1.setValue(0, 1, 20);
chart1.setValue(0, 2, 30);
chart1.closeData(COD.VALUES);
// Configure image map
chart1.setImgMap(ImgMap.TITLE_TIP);
// set Chart ID
chart1.setID("MyChart");
// Initialize Output fields
String myChartName = "c:\\\\chart.png";
java.io.ByteArrayOutputStream mapStream =
new java.io.ByteArrayOutputStream();
java.io.ByteArrayOutputStream tagStream =
new java.io.ByteArrayOutputStream();
java.io.FileOutputStream imgStream =
new java.io.FileOutputStream(myChartName);
// Call getHtmlData
chart1.getHtmlDataEx("400","400","PNG", imgStream,mapStream,tagStream);
// Convert output streams to String vars
String imgMap = mapStream.toString();
String imgTag = tagStream.toString();
// replace SRC field in Image Tag
String imgTagNew = imgTag.replaceAll("\"MyChart\"",
"\"" + myChartName +"\"");
// Output resuts to browser
out.println(imgMap);
out.println(imgTagNew);
out.close();
}

This servlet will generate a PNG image at C:\chart.png, and will output the following HTML:


<MAP name="MyChart">
<AREA SHAPE=RECT COORDS="276,32,355,369" TITLE="30.00">
<AREA SHAPE=RECT COORDS="171,144,250,369" TITLE="20.00">
<AREA SHAPE=RECT COORDS="65,257,144,369" TITLE="10.00">
. . .
</MAP>

<IMG SRC="c:\chart.png" usemap="#MyChart" border="0">