您现在的位置: 捷凌网安 >> 服务器 >> WEB开 发 >> JSP >> 正文
JSP调用JavaBean在网页上动态生成柱状图

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-2-20 0:30:49

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

  • 上一篇文章:

  • 下一篇文章:
  •  
    最进更新
    普通文章foxpro 更新源表05-05
    普通文章foxpro 让视图与数据源相连05-05
    普通文章foxpro 机动查询和数据输入05-05
    普通文章foxpro 多个本地数据05-05
    普通文章foxpro 维护源表05-05
    普通文章Oracle 10g Release2新功能之05-05
    普通文章将Oracle 10g内置的安全特性05-05
    普通文章ACCESS 2003 建立数据库视频05-05
    普通文章三种SQL分页法效率分析05-05
    普通文章优化MySQL数据库查询的三种方05-05
     
    推荐文章
    推荐文章教你怎样在MySQL中提高全文搜05-05
    推荐文章SQL Server中数据导入导出三05-05
    推荐文章缓冲技术提高JSP程序的性能和04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    推荐文章何时使用DataGrid、DataList04-17
    推荐文章MySQL存储过程示例04-14
    推荐文章华硕搭建Exchange2007企业邮03-14
    推荐文章升级Win 2003到Windows 200803-14
    推荐文章windows Server 2003 搭建域03-14
    推荐文章服务器成为IT中心的6个理由03-11
     
    热点文章 
    普通文章SQL Server 2008分析服务概览05-01
    普通文章Dlink路由器VPN设置04-29
    推荐文章缓冲技术提高JSP程序的性能和04-17
    普通文章教你优化你的ASP程序04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    普通文章ASP添加验证码的解决方法04-17
    推荐文章何时使用DataGrid、DataList04-17
    普通文章Asp.net中禁止用户多次登录04-17
    普通文章MySQL之表结构修改04-14
    推荐文章MySQL存储过程示例04-14

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号