js| News | page | Histogram we often see some dynamically updated pictures on the Web page, the most common one is the chart of stock, this article tries to show you how to use JSP to invoke Java beans to dynamically generate a histogram on a Web page by using a simple example.
Background: I recently in the development of a project for a Bureau of Statistics, involving the dynamic generation of pictures on the Web page problem, a day, and finally finished, in order to help everyone in the future encounter the same problems without detours, is the design idea and source code published, To share with you. The following code successfully tested in Windows2000, Web application server using Allaire company Jrun3.0, if in doubt, please contact Author: cuigy2000@263.net
First step: Create a Java bean to generate a JPG file
The source program is as follows:
Java beans that generate pictures
Author: Tri Kuanyu
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);
JPEGImageEncoder encoder = Jpegcodec.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;//width of picture
int imageheight = height of 300;//picture
width of int columnwidth=30;//column
The maximum height of an int columnheight=200;//column
Explanation: The CreateImage (String filelocation) method is used to create a JPG picture, with the argument filelocation as a file path
The graphicsgeneration (int h1,int h2,int h3,int h4,int h5) method is used to draw the contents of the picture, with the parameter h1......h5 to the height of each rectangle
Step two: Create another Java bean to read data from a text file (the height of each rectangle), in the actual application data stored in the Oracle database
The source program is as follows:
Java Bean that reads data from the text file
Author: Tri Kuanyu
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 text, convert string types in text to int types, and return them as array types.
Step three: 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]);
%>
<body>
</img>
</body>
Explanation: The JSP first invokes the bean (GetData. Class) to read the data in the file, then call the Bean (Chartgraphics.class) to generate the picture, and finally display the picture
Conclusion: Because the data in the text (ColumnHeightArray.txt) can change at any time, so the height of the 5 rectangles in the resulting picture is changed, which enables the dynamic generation of the picture. The design idea can also be used to create a polling system for websites.
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.