欢迎光临
我们一直在努力

JSP调用JavaBean在网页上动态生成柱状图-JSP教程,数据库相关

建站超值云服务器,限时71元/月

我们经常要在网页看到一些动态更新的图片,最常见的莫过于股票的k线图,本文试图通过一个简单的实例,向大家展示如何通过jsp 调用javabean在网页上动态生成柱状图。

  背景:本人最近在为某统计局开发项目时,涉及到在网页上动态生成图片的问题,费了一天的时间,终于搞定,为帮助大家在以后遇到同样的问题时不走弯路,现将设计思想及源代码公布出来,与大家共勉。以下代码在windows2000成功测试通过,web应用服务器采用allaire公司的jrun3.0。

  第一步:创建一个java bean用来生成jpg文件

  源程序如下:

//生成图片的 java bean

//作者:崔冠宇

//日期: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;//图片的宽度

  int imageheight = 300;//图片的高度

  int columnwidth=30;//柱的宽度

  int columnheight=200;//柱的最大高度

  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);

  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");

 }

}

  解释:createimage(string filelocation)方法用于创建jpg图片,参数filelocation为文件路径

  graphicsgeneration(int h1,int h2,int h3,int h4,int h5)方法用于绘出图片的内容,参数h1……h5为每一个长方形的高度

  第二步:创建另一个java bean从文本文件中读取数据(每一个长方形的高度),在实际应用中数据存储在oracle数据库中

  源程序如下:

//读取text文件中数据的 java bean

//作者:崔冠宇

//日期: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;

 }

}

  解释: gethightarray()用于从文本中读取数据,将文本中的string类型转换为int类型,并以数组类型返回。

  第三步:创建jsp文件

  源程序如下:

<%@ 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 src="d: empchart.jpg"></img>

</body>

</html>

   解释:jsp首先调用bean (getdata..class)读取文件中的数据,再调用bean(chartgraphics.class)生成图片,最后显示图片。

  结束语:由于文本(columnheightarray.txt)中的数据可以随时变化,因此生成的图片中的5个长方形的高度是随之变化的,从而实现了图片的动态生成.该设计思想还可以用于制作网站的投票系统

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » JSP调用JavaBean在网页上动态生成柱状图-JSP教程,数据库相关
分享到: 更多 (0)