You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Ruben Domene <rd...@svalero.es> on 2001/08/22 14:00:54 UTC

Graphics with jsp

Hi.
How can i show data with graphics like de follow or similar:

   100|   .
    80| .
    60|	 .
    40|
    20|
     0|____________
	 1  2  3  4		

I need access to database, take the data and show with one graphic.
There are any web where i will find information about this?.

Re: Graphics with jsp

Posted by Jim Cheesman <jc...@msl.es>.
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:
jdc-webmaster@sun.com

- 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: 
jchees@msl.es - (34)(91) 724 9200 x 2360
                Free advice is 
worth what you paid for it.



Re: Graphics with jsp

Posted by Nitin Goyal <ni...@webrizon.com>.
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.

Regards
Nitin Goyal
Webrizon eSolutions Pvt. Ltd., India
nitin.goyal@webrizon.com

There is no failure except in no longer trying.


----- Original Message -----
From: "Ruben Domene" <rd...@svalero.es>
To: "tomcat" <to...@jakarta.apache.org>
Sent: Wednesday, August 22, 2001 5:30 PM
Subject: Graphics with jsp


Hi.
How can i show data with graphics like de follow or similar:

   100|   .
    80| .
    60| .
    40|
    20|
     0|____________
1  2  3  4

I need access to database, take the data and show with one graphic.
There are any web where i will find information about this?.



RE: Graphics with jsp

Posted by Jim Urban <ji...@netsteps.net>.
You could use awt to draw the graph to a "Graphics" context and GifEncoder
from Acme Laboratories (www.acme.com) to convert the image to a gif which
you can simply return to the browser by setting the content type to
"image/gif".  GifEncoder is free.  Attached is a servlet which shows how to
do this.  This sample shows how to send the image back to the browser
directly.  You could also create the image as file and use an img tag to
include it in the HTML sent back to the browser.

Jim Urban
Product Manager
Netsteps Inc.
Suite 505E
1 Pierce Pl.
Itasca, IL  60143
Voice:  (630) 250-3045 x2164
Fax:  (630) 250-3046




-----Original Message-----
From: Nikola Milutinovic [mailto:Nikola.Milutinovic@ev.co.yu]
Sent: Wednesday, August 22, 2001 7:27 AM
To: tomcat-user@jakarta.apache.org
Subject: Re: Graphics with jsp


Gregor Kovah wrote:

> Hi!
>
> Hmm, this is a bit off topic, but nevertheless:
> You could use KavaChart (www.ve.com), which you can use in Servlets and
> JSPs, but unfortunately it is not free.

Why not the good old "Fly"? It is a C compiled CGI program that takes a
"command file" and draws a GIF following those commands. All the
JSP/servlet needs to do is create a command file.

Is there something equivalent in Java?

Nix.


Re: Graphics with jsp

Posted by Nikola Milutinovic <Ni...@ev.co.yu>.
Gregor Kovah wrote:

> Hi!
> 
> Hmm, this is a bit off topic, but nevertheless:
> You could use KavaChart (www.ve.com), which you can use in Servlets and 
> JSPs, but unfortunately it is not free.

Why not the good old "Fly"? It is a C compiled CGI program that takes a 
"command file" and draws a GIF following those commands. All the 
JSP/servlet needs to do is create a command file.

Is there something equivalent in Java?

Nix.


Re: Graphics with jsp

Posted by Gregor =?iso-8859-1?Q?Kova�?= <gr...@mikropis.si>.
Hi!

Hmm, this is a bit off topic, but nevertheless:
You could use KavaChart (www.ve.com), which you can use in Servlets and 
JSPs, but unfortunately it is not free.

Best regards,
         Kovi

At 14:00 22.8.01 +0200, you wrote:
>Hi.
>How can i show data with graphics like de follow or similar:
>
>    100|   .
>     80| .
>     60| .
>     40|
>     20|
>      0|____________
>         1  2  3  4
>
>I need access to database, take the data and show with one graphic.
>There are any web where i will find information about this?.