[原创]Java ,jsp 简单立体报表图,不需要额外插件
想和大家交流一下。
把我的作品发到这里来。
---------------------
最近工作上需要统计图。自己翻翻网上资料,找了一下3D的Java图形资料。说需要什么插件。
我也不懂,就自己写了一个。希望大家评论一下。
======================
代码大部分是使用java.awt.Graphics类里的方法写的。自己画的3D box.
然后输出java.awt.image.BufferedImage到jsp页。就可以了。
下面是代码:
/**
* <p>Title: Java ,jsp 简单立体报表图,不需要额外插件</p>
*
* <p>Description: </p>
*
* <p>Company: </p>
*
* @author xym,msn:yuanmingxu@hotmail.com
* @version 1.0
* <p>Copyright: Copyright (c) 2006版权所有,转载请保留作者信息</p>
*/
package xym.counter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.*;
public class Counter {
private int width, height;//图片宽高
private int cooLeft,cooTop,cooWidth,cooHeight;
//坐标系的顶点和宽高
private int maxValue=40000;//box最大值
private int ply=10;//3D宽度
private String szTitle;//图片标题
BufferedImage image ;
Graphics g;
/**
* @param args
*/
//Hashtable boxes;//长方块键值对。键代表显示名称。置代表方块长度
public Counter(int width,int height){
this.width=width;
this.height=height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
}
public Counter(){
this.width=600;
this.height=200;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
}
public void drawCoordinate(){
drawCoordinate(90,60);
}
public void clear(){//清除所有
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
drawCoordinate();
}
private void drawCoordinate(int left,int top){//画坐标系统,根据最大值画20个纵坐标值
cooLeft=left;
cooTop=top;
g.setColor(Color.GRAY);
g.draw3DRect(0,0,width-1,height-1,true);//
g.draw3DRect(1,1,width-2,height-2,true);//
g.draw3DRect(2,2,width-3,height-3,true);//画外框。
if(szTitle!=null)
g.drawString(szTitle,width/2-szTitle.length()*5,top-20);
//画坐标
cooWidth=width-left-10;
cooHeight=height-top-40;//坐标系宽高。
g.drawRect(left,top,cooWidth,cooHeight);
int[] x={left,left-ply,left-ply,left};//画阴影。
int[] y={top,top+ply,top+cooHeight+ply,top+cooHeight};
g.drawPolygon(x,y,4);//画左边阴影
int[] x1={left,left-ply,left-ply+cooWidth,left+cooWidth};
int[] y1={top+cooHeight,top+cooHeight+ply,top+cooHeight+ply,top+cooHeight};
g.drawPolygon(x1,y1,4);//画下边阴影
g.setColor(new Color(200,200,200));
g.fillPolygon(x,y,4);
g.setColor(Color.BLACK);//画实线
g.drawLine(left,top,left-ply,top+ply);
g.drawLine(left-ply,top+ply,left-ply,top+ply+cooHeight);
g.drawLine(left-ply,top+ply+cooHeight,left-ply+cooWidth,top+ply+cooHeight);
g.drawLine(left-ply+cooWidth,top+ply+cooHeight,left+cooWidth,top+cooHeight);
//开始画刻度和线条
int kleft=left-ply-15;
int ktop=top+ply;
int kspace=cooHeight/20;//刻度间距
for(int i=0;i<20;i++){
g.drawLine(kleft,ktop+kspace*i,kleft+10,ktop+kspace*i);
//写刻度
g.drawString(""+(maxValue-(maxValue/20)*i),kleft-50,ktop+kspace*i);
g.drawLine(left,ktop+kspace*i,left+cooWidth,ktop+kspace*i);
}
for(int i=0;i<31;i+=2){//测试代码
this.drawCubeInCoo((i+1)+"日",i,i*1000,new Color(5*i,7*i,5*i));
}
g.dispose();
}
private void drawCube(int left,int top,int width,int height ,Color color){//画一个立方体
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int c=20;//颜色差值
int[] x={left,left-ply,left-ply+width,left+width};
int[] y={top,top+ply,top+ply,top};
g.setColor(new Color((red-c<0?0:red-c),(green-c<0?0:green-c),(blue-c<0?0:blue-c)));
g.fillPolygon(x,y,4);
int[] x1={left-ply,left-ply,left-ply+width,left-ply+width};
int[] y1={top+ply,top+ply+height,top+ply+height,top+ply};
g.setColor(color);
g.fillPolygon(x1,y1,4);
int[] x2={left+width,left+width-ply,left+width-ply,left+width};
int[] y2={top,top+ply,top+ply+height,top+height};
g.setColor(new Color((red-2*c<0?0:red-2*c),(green-2*c<0?0:green-2*c),(blue-2*c<0?0:blue-2*c)));
g.fillPolygon(x2,y2,4);
}
public void drawCubeInCoo(String Title,int pos,int value,Color color){//在坐标里面画boxes
if(pos>31) pos=31;
if(value>maxValue)value=maxValue;
//标题,位置(一共31个位置),值,颜色。
int top=cooTop+(int)(cooHeight*(1-(float)value/maxValue));
int left=cooLeft+(int)(((float)cooWidth/31)*pos);
//this.drawCube(left,top,cooWidth/24,cooHeight*(value/maxValue),ply,color);
g.drawString(""+value,left,top);
this.drawCube(left,top,cooWidth/31-ply-2,(int)(cooHeight*((float)value/maxValue)),color);
if(null!=Title)
g.drawString(Title,left,cooTop+cooHeight+ply+20);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public BufferedImage getImage() {
g.dispose();
return image;
}
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
public String getSzTitle() {
return szTitle;
}
public void setSzTitle(String szTitle) {
this.szTitle = szTitle;
}
}
////
下面是JSP代码:
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,xym.counter.*" %>
<%
//set no cache.
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//create image
Counter counter = new Counter(1000,400);
counter.setSzTitle("Test");
counter.clear();
ImageIO.write(counter.getImage(), "JPEG", response.getOutputStream())
%>
有效果图,不知道这里怎么发。现这样吧。需要看图的可以自己运行一下。或者管我要。
msn:yuanmingxu@hotmail.com
问题点数:0、回复次数:12Top
1 楼eaglezhang(Eagle)回复于 2006-03-03 16:55:23 得分 0
沙发Top
2 楼GlobalFree(随机的)回复于 2006-03-03 17:02:11 得分 0
谢谢收看。。。Top
3 楼loveyt(咆哮的独角兽^大家都在长大)回复于 2006-03-03 17:03:49 得分 0
晕!板凳都被抢了~~Top
4 楼asinezhang(含沙射影)回复于 2006-03-03 17:33:06 得分 0
坐地板上了。。。看看先~~~~~Top
5 楼rogerfhl(PRISON BREAK!)回复于 2006-03-03 17:43:12 得分 0
学习~
Top
6 楼rogerfhl(PRISON BREAK!)回复于 2006-03-03 17:54:09 得分 0
:(
运行jsp错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 2 in the jsp file: /ww/fan.jsp
Generated servlet error:
Syntax error, insert ";" to complete Statement
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Top
7 楼GlobalFree(随机的)回复于 2006-03-04 17:11:03 得分 0
楼上的,注意一下JSP里的import,和java里面的package...................Top
8 楼yeqiufeng(叶秋枫)回复于 2006-03-04 17:21:22 得分 0
收藏一下Top
9 楼wsk_228(qing_feng)回复于 2006-03-04 17:35:38 得分 0
ImageIO.write(counter.getImage(), "JPEG", response.getOutputStream());
%>
楼主是在这里掉了一个分号,不过自己调试一下不就出来了嘛。
做的挺好的,我运行过了。谢谢收藏一下Top
10 楼GlobalFree(随机的)回复于 2006-03-04 18:44:47 得分 0
谢谢各位收藏,楼上的一句话,有点成就感阿。赫赫。Top
11 楼rogerfhl(PRISON BREAK!)回复于 2006-03-06 11:34:26 得分 0
^_^,问了个低级的问题......
但我这里还是有点问题,提示:
An error occurred at line: 2 in the jsp file: /ww/fan.jsp
Generated servlet error:
Counter cannot be resolved or is not a type
怎么提示Counter 这个方法错误??
Top
12 楼Timsole(一陀智慧)回复于 2006-03-06 12:41:57 得分 0
hao 支持 收藏一下Top




