JSP call JavaBean dynamically generates histogram on the web page

zhaozj2021-02-12  195

JSP call JavaBean dynamically generates histogram on the web page

We often want to see some dynamic updated pictures in the web. The most common Mo stock's K-line map, this article tries to show you how to call JavaBean through JSP to dynamically generate a histogram on the web page through a simple instance. Background: I have recently developed a project for a statistical bureau, involving the problem of dynamically generating pictures on the web, and finally getting a day, finally getting it, in order to help everyone get along the same problem in the future, now Design ideas and source code are announced, and you will share with you. The following code is successfully tested in Windows2000, and the web application server uses Allaire's JRUN3.0. Step 1: Create a Java Bean to generate a JPG file source program as follows:

// generated picture of Java Bean //: // Cuiguan Yu 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.CREATEJPEGENCODEC.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; // Picture Width INT ImageHeight = 300; // Picture High Int ColumnWidth = 30; // Column Width INT ColumnHeight = 200 ; maximum height of the column chartGraphics chartGraphics // = new chartGraphics (); chartGraphics.image = new BufferedImage (imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = chartGraphics.image.getGraphics (); graphics.setColor (Color.wh ite); 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:

CreateImage (String Filelocation) Method for creating JPG images, parameter filelocation for file path GRAPHICSGENERATION (INT H1, INT H2, INT H3, INT H4, INT H5) method for painting the contents of the picture, parameter H1 ... H5 is per A rectangular height second step: Create another Java Bean read data from a text file (the height of each rectangle), stored in the actual application in the Oracle database Source program as follows: // Read data in the text file the Java Bean // author: Cuiguan Yu // 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;}}}}}}} explained: gethightArray () is used to read data from text, convert String type in text to int type And return in an array type. Step 3: Create a JSP file source program as follows:

<% @ Page import = "chartgraphics"%> <% @ page import = "getData"%> <%! INT Height [] = new int rt [5];%> ​​<% height = gd.gethightArray (); cg.graphicsGeneration (Height [0], Height [1], Height [2], Height [1], Height [2], HEIGHT [3], Height [4]);%> explanation: JSP first call Bean reads the data in the file, then call the bean (chartgraphics.class) to generate a picture, and finally display the image.

转载请注明原文地址:https://www.9cbs.com/read-6444.html

New Post(0)