JSP calls JavaBean to dynamically generate a bar chart on the webpage
Source: Internet
Author: User
We often need to see some dynamic update images on the webpage. The most common is the stock K-line chart. This article attempts to use a simple example, it shows you how to use JSP to call JavaBean to dynamically generate a bar chart on a webpage.
Background: when I recently developed a project for a bureau of statistics, I had to dynamically generate images on the webpage. It took a day to complete the process, to help you avoid detours when you encounter the same problems in the future, we will announce the design ideas and source code to share with you. The following code is successfully tested on Windows. The Web application server uses Allaire Jrun3.0.
Step 1: Create a Java Bean to generate a jpg file
The source program is as follows:
// Java Bean for generating images
// By Cui Guanyu
// Date: 2001-08-24
Import java. io .*;
Import java. util .*;
Import com.sun.image.codec.jpeg .*;
Import java. awt. image .*;
Import java. awt .*;
Public class ChartGraphics {
BufferedImage image;
Public void createImage (String fileLocation ){
Try {
FileOutputStream fos = new FileOutputStream (fileLocation );
BufferedOutputStream bos = new BufferedOutputStream (fos );
Required imageencoder encoder = required codec. createJPEGEncoder (bos );
Encoder. encode (image );
Bos. close ();
} Catch (Exception e ){
System. out. println (e );
}
}
Public void graphicsGeneration (int h1, int h2, int h3, int h4, int h5 ){
Final int X = 10;
Int imageWidth = 300; // the image width.
Int imageHeight = 300; // The Image height
Int columnWidth = 30; // The width of the column
Int columnHeight = 200; // maximum column height
ChartGraphics chartGraphics = new ChartGraphics ();
ChartGraphics. image = new BufferedImage (imageWidth, imageHeight, BufferedImage. TYPE_INT_RGB );
Graphics graphics = chartGraphics. image. getGraphics ();
Graphics. setColor (Color. white );
Graphics. fillRect (0, 0, imageWidth, imageHeight );
Graphics. setColor (Color. red );
Graphics. drawRect (X + 1 * columnWidth, columnHeight-h1, columnWidth, h1 );
Graphics. drawRect (X + 2 * columnWidth, columnHeight-h2, columnWidth, h2 );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.