- 论坛徽章:
- 0
|
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>;
<%@ page import="java.io.OutputStream" %>;
<%@ page import="java.awt.image.BufferedImage" %>;
<%@ page import="java.awt.*" %>;
<%@ page import="com.sun.image.codec.jpeg.*" %>;
<%@ include file = "graphBG.jsp"%>;
<%
//Data arrays
String datanames[] = {"公司", "Oranges", " eaches", "Lemons", "Grapefruit","cn"};
int datavalues[] = {100, 62, 33, 200, 50,160};
//current x position
int x_pos = 0;
//y offset to cater for header space
int headerOffset = 50;
//inner padding to make sure bars never touch the outer border
int innerOffset = 20;
//height of bar, text and total
int barWidth = 15;
int textWidth = 30;
int displayWidth = barWidth + textWidth;
//Color used for the bars
Color barColor = new Color(153,19,150);
//Set the graph's outer width
int WIDTH = (datavalues.length * displayWidth) + headerOffset + innerOffset;
//Set the graph's outer height
int HEIGHT = 700;
//Width of the graphable area
int innerHEIGHT= HEIGHT- (innerOffset * 2);
//Calculate average
int average = 0;
for(int i=0; i<datavalues.length; i++)
{
average += datavalues;
}
average = average / datavalues.length;
//Calculate maximum
int maximum = 0;
for(int i=0; i<datavalues.length; i++)
{
if(datavalues >; maximum)
{
maximum = datavalues;
}
}
///////////////////////////////////////////////////////////////////////////////////////
//Draw Graph Background and Header:
///////////////////////////////////////////////////////////////////////////////////////
response.setContentType("image/jpeg" ;
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
graphBG gr = new graphBG();
gr.draw(biContext, WIDTH, HEIGHT, "北京电信公司", "平均数: " + average);
/////////////////////////////////////////////////////////////////////////////////////
//Draw data onto the graph:
/////////////////////////////////////////////////////////////////////////////////////
//Loop through & draw the bars
for(int i=0; i<datavalues.length; i++)
{
int currentValue = datavalues;
//Set x position for bar
x_pos = i * displayWidth + headerOffset;
//Set bar width
int barHeight = (innerHEIGHT * currentValue) / maximum;
//Display the current value
String display = datanames + " (" + currentValue + " ";
biContext.setColor(Color.black);
biContext.drawString(display, 20,x_pos );
//Set dimensions of the bar
biContext.setColor(barColor);
Dimension bar = new Dimension(barWidth , barHeight);
Dimension barBorder = new Dimension(barWidth - 3 , barHeight - 3);
Rectangle barRect = new Rectangle(bar);
Rectangle barRectBorder = new Rectangle(barBorder);
//Draw bar and border:
barRect.setLocation(5 + x_pos,21 );
barRectBorder.setLocation(6 + x_pos,22 );
biContext.setColor(barColor);
biContext.fill(barRect);
biContext.setColor(Color.white);
biContext.draw(barRectBorder);
}
/////////////////////////////////////////////////////////////////////////////////////
//Display the graph
/////////////////////////////////////////////////////////////////////////////////////
//Encode:
OutputStream output = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(bi);
output.close();
%>; |
|