At 02:54 PM 22/08/01, you wrote:
>Hi,
>
>One of the solutions (though not very elegant) is to create the graph
>dynamically by dynamically generating html in a jsp/servlet after fetching the
>data from the database.  You create the requisite no. of html tags (td and
>tr)and fill then up with colour.  Of course this wouldnt help in case of 
>complex
>graphs, and if you feel that your or the programmers time can be utilized 
>doing
>something else more productive.  The advantage is that it is useful for simple
>graphs and is free.


Which interestingly was in the JDC Tech Tips I received yesterday... the 
relevant bit is cut 'n pasted below. I've included the archive links in 
case you're interested:



DELIVERING DYNAMIC IMAGES FROM JAVASERVER PAGES (JSP) TECHNOLOGY
Have you ever wanted to deliver dynamically-generated images from
your JSP pages (or servlets)? This tip shows you how. To run the
code in this tip, you need Tomcat or another JSP 1.1-enabled web
server. You can download Tomcat from the Jakarta Project page at
http://jakarta.apache.org/tomcat.
When a web page is delivered with a MIME type of image/jpeg (or
one of the other image formats), your browser treats the response
as an image. The browser then displays the image, either as part
of a larger web page or on its own. To set up the MIME type for
your JSP pages, you need to set the contentType attribute of the
page directive in the .jsp file for the specific page:
<%@ page contentType="image/jpeg" ... %>
Then you need to create a BufferedImage to draw on for your
dynamic image:
BufferedImage image = new BufferedImage(width,
height, BufferedImage.TYPE_INT_RGB);
After you create the BufferedImage, you need to get a graphics
context to draw with, either a Graphics or Graphics2D object will
do:
Graphics g = image.getGraphics();
// or
Graphics2d g2d = image.createGraphics();
 >From here, you can draw the image content. Drawing to the
graphics context draws to the BufferedImage. Initially, the
entire image is black, so it's a good idea to fill the image with
the desired background color. Then, when you are finished drawing,
you need to dispose of the context:
g.dispose();
// or
g2d.dispose();
Once the image is completely drawn, you send the image back in
the response. You can use the JPEGImageEncoder class of the
non-standard com.sun.image.codec.jpeg package to encode the
image. Or, if you use the Java 2 SDK, Standard Edition, v 1.4
Beta, you can use the standard ImageIO class. There is one tricky
part of using the JPEGImageEncoder. You must fetch the
ServletOutputStream from the ServletResponse (response object),
and not use the implicit JSP output variable out.
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
// or
ImageIO.write(image, "JPEG", out);
Here's a complete example that picks one option from all the
choices (for example, g.dispose(); versus g2d.dispose();).
The example uses the Graphics object to draw a random
polygon. The image is drawn back through the JPEGImageEncoder.
Feel free to play with the number of points in the polygon to get
more complex shapes, in other words, shapes with more points and
edges.
To run this example, place the JSP code from "<%@" to the
last "%>" in a file named image.jsp. Place the image.jsp file in
a directory that your web server can find. In the case of Tomcat,
this is the ROOT directory, under the webapps directory, beneath
the Tomcat installation directory. To start Tomcat, you need to
run the startup script (startup.bat or startup.sh depending upon
your platform) in the bin directory under the Tomcat installation
directory. Make sure you have the JAVA_HOME environment variable
set to the root level of your Java 2 SDK installation, for
example, C:\jdk1.2.2. Once the file is in the appropriate
directory and Tomcat is running, you can load the dynamic image
generating JSP file with http://localhost:8080/image.jsp.
<%@ page contentType="image/jpeg"
import="java.awt.*,java.awt.image.*,
com.sun.image.codec.jpeg.*,java.util.*"
%>
<%
// Create image
int width=200, height=200;
BufferedImage image = new BufferedImage(width,
height, BufferedImage.TYPE_INT_RGB);
// Get drawing context
Graphics g = image.getGraphics();
// Fill background
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
// Create random polygon
Polygon poly = new Polygon();
Random random = new Random();
for (int i=0; i < 5; i++) {
poly.addPoint(random.nextInt(width),
random.nextInt(height));
}
// Fill polygon
g.setColor(Color.cyan);
g.fillPolygon(poly);
// Dispose context
g.dispose();
// Send back image
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
%>
. . . . . . . . . . . . . . . . . . . . . . .
- NOTE
Sun respects your online time and privacy. The Java Developer
Connection mailing lists are used for internal Sun Microsystems(tm)
purposes only. You have received this email because you elected
to subscribe. To unsubscribe, go to the Subscriptions page
http://developer.java.sun.com/subscription/
uncheck the appropriate checkbox, and click the Update button.

- SUBSCRIBE
To subscribe to a JDC newsletter mailing list, go to the
Subscriptions page
http://developer.java.sun.com/subscription/
choose the newsletters you want to subscribe to, and click
Update.

- FEEDBACK
Comments? Send your feedback on the JDC Tech Tips to:
[EMAIL PROTECTED]

- ARCHIVES
You'll find the JDC Tech Tips archives at:
http://java.sun.com/jdc/TechTips/index.html

- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.



--

                           *   Jim Cheesman   *
             Trabajo: 
[EMAIL PROTECTED] - (34)(91) 724 9200 x 2360
                Free advice is 
worth what you paid for it.


Reply via email to