您现在的位置:中国下载站学院中心网络编程JSP教程Jsp开发技巧 → 文章列表

JSP调用JavaBean在网页上动态生成柱状图

作者:佚名  来源:不详  发布时间:2007-4-14 11:56:37   

减小字体 增大字体

 
 
  我们经常要在网页看到一些动态更新的图片,最常见的莫过于股票的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为文件路径


在百度中搜索更多JSP调用JavaBean在网页上动态生成柱状图相关网页 转贴于:中国下载站

  • 上一篇文章:JSP技巧:发送动态图像
  • 下一篇文章:JSP生成jpeg图片用于投票
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!