一个画柱状图形的例子,在页面中显示
2008-02-23 10:08:02来源:互联网 阅读 ()
<%@ page language="Java" contentType="charset=gb2312"%>
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.awt.*,java.awt.font.*,java.awt.geom.*,javax.imageio.*,java.io.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%@ page import="acec.*,java.util.*" %>
<!--%@ include file = "graphBG.JSP"%-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<%
//数据数组
String datanames[] = {"第一季度", "第二季度", "第三季度"};
int datavalues[] = {111, 620, 33};
int WIDTH = 600;
int HEIGHT =400;
int innerHEIGHT = HEIGHT-100;
int innerWIDTH = WIDTH-20;
int maximum = 0;
for(int i=0; i<datavalues.length; i )
{
if(datavalues[i] > maximum)
{
maximum = datavalues[i];
}
}
response.setContentType("image/jpeg");
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
//插入背景图案
try{
Image image=ImageIO.read(new File("bg.jpg"));
biContext.drawImage(image,0,0,WIDTH,HEIGHT,null);
}catch(Exception ex){System.out.println(ex.toString());}
//开始绘图
int barWidth = innerWIDTH/(datavalues.length*4);
//System.out.println("barWidth=" barWidth);
for(int i=0,ii=0; i<datavalues.length; i ,ii )
{
Color barColor=null;
Color barColor1=null;
if(ii==0)//蓝色
{
barColor=new Color(48,36,189);
barColor1 = new Color(201,201,245);
}
else if(ii==1)//绿色
{
barColor=new Color(62,145,68);
barColor1 = new Color(210,236,236);
}
else if(ii==2)//红色
{
barColor=Color.red;
barColor1 = new Color(253,181,199);
ii=-1;
}
int currentValue = datavalues[i];
//Set bar width
int barHeight = (innerHEIGHT * currentValue) / maximum;
biContext.setColor(barColor);
Rectangle barRect = new Rectangle(barWidth,barHeight);
//取字的宽度
Font f=biContext.getFont();
FontRenderContext context=biContext.getFontRenderContext();
Rectangle2D bounds_top=f.getStringBounds("人气值:" currentValue,context);
Rectangle2D bounds=f.getStringBounds(datanames[i],context);
biContext.setColor(Color.black);
//柱形的定位
int locationY=HEIGHT-barHeight-20;
int locationX=innerWIDTH/datavalues.length*(i 1)-barWidth/2;
if(datavalues.length%2!=0)
locationX=innerWIDTH/(datavalues.length 1)*(i 1)-barWidth/2;
barRect.setLocation(locationX,locationY);
biContext.drawString(datanames[i],(int)((locationX barWidth/2)-bounds.getWidth()/2),HEIGHT-5);
biContext.drawString("人气值:" currentValue, (int)((locationX barWidth/2)-bounds_top.getWidth()/2),locationY-10);
biContext.setPaint(new GradientPaint(locationX,locationY,barColor1,locationX barWidth,locationY,barColor));
biContext.fill(barRect);
}
/////////////////////////////////////////////////////////////////////////////////////
//保存图片
/////////////////////////////////////////////////////////////////////////////////////
OutputStream bos = new FileOutputStream(application.getRealPath("tp.jpeg"));
JPEGImageEncoder encoder1 = JPEGCodec.createJPEGEncoder(bos);
encoder1.encode(bi);
bos.close();
%>
<body leftmargin="0" topmargin="0">
<img src="tp.jpeg" width="600" height="400">
</html>
上一篇: Maven的Bug???
下一篇: [引用] 为 SWT 应用程序配备内容助理
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:J2ME中Random类的使用
下一篇:JavaExcelApi指南
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash