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 = 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 );
Graphics. drawRect (X + 3 * columnWidth, columnHeight-h3, columnWidth, h3 );
Graphics. drawRect (X + 4 * columnWidth, columnHeight-h4, columnWidth, h4 );
Graphics. drawRect (X + 5 * columnWidth, columnHeight-h5, columnWidth, h5 );
ChartGraphics. createImage ("D: // temp // chart.jpg ");
}
}
 

Explanation: The createImage (String fileLocation) method is used to create a jpg image. The fileLocation parameter is the file path.

GraphicsGeneration (int h1, int h2, int h3, int h4, int h5) is used to plot the image content. The parameter h1 ...... H5 is the height of each rectangle

Step 2: create another Java Bean to read data from a text file (the height of each rectangle) and store the data in the Oracle database in practical applications.

The source program is as follows:

// Java Bean for reading data in the Text file
// By Cui guanyu
// Date: 2001-08-24
Import java. io .*;
Public class GetData {
Int heightArray [] = new int [5];
Public int [] getHightArray (){
Try {
RandomAccessFile randomAccessFile = new RandomAccessFile ("d: // temp // ColumnHeightArray.txt", "r ");
For (int I = 0; I <5; I ++)
{
HeightArray [I] = Integer. parseInt (randomAccessFile. readLine ());
}
}
Catch (Exception e ){
System. out. println (e );
}
Return heightArray;
}
}

Explanation: getHightArray () is used to read data from the text. It converts the String type in the text to the int type and returns the data of the array type.

Step 3: Create a JSP file

The source program is as follows:

<% @ Page import = "ChartGraphics" %>
<% @ Page import = "GetData" %>
<Jsp: useBean id = "cg" class = "ChartGraphics"/>
<Jsp: useBean id = "gd" class = "GetData"/>
<%!
Int height [] = new int [5];
%>
<%
Height = gd. getHightArray ();
Cg. graphicsGeneration (height [0], height [1], height [2], height [3], height [4]);
%>
<Html>
<Body>
</img>
</Body>
</Html>

Explanation: JSP first calls Bean (GetData .. class) to read the data in the file, then calls Bean (ChartGraphics. class) to generate an image, and finally displays the image.

Conclusion: The data in partition (columnheightarray.txt) can be changed at any time, so the height of the five rectangles in the generated image changes accordingly, so that the image can be dynamically generated. this design idea can also be used to create a website's voting system.

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.