免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2399 | 回复: 3
打印 上一主题 下一主题

请教用jsp画直方图!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-10-13 23:46 |只看该作者 |倒序浏览
<%@ 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();

%>;

论坛徽章:
0
2 [报告]
发表于 2003-10-13 23:47 |只看该作者

请教用jsp画直方图!!

<%!
public class graphBG
{

        Font font = new Font("宋体", Font.PLAIN, 11);
        Color headerColor = new Color(20,50,100);

        public Graphics2D draw(Graphics2D biContext, int WIDTH, int HEIGHT, String headerText, String averageText)
        {
                //Grey color for the drop shadow
                Color dropShadow = new Color(200,200,200);

                //Graph and header dimensions
                Dimension dim = new Dimension(WIDTH,HEIGHT);
                Dimension dimGraph = new Dimension(WIDTH-21,HEIGHT-51);
                Dimension dimHeader = new Dimension(WIDTH-21,20);

                //Define Rectangles:
                Rectangle area = new Rectangle(dim);
                Rectangle graphArea = new Rectangle(dimGraph);
                Rectangle headerArea = new Rectangle(dimHeader);

                /////////////////////////////////////////////////////////////////////////////////////
                //Set up the graph:
                /////////////////////////////////////////////////////////////////////////////////////

                //Set background color to white:
                biContext.setColor(Color.white);
                biContext.fill(area);

                //Drop shadow for the graph area:
                graphArea.setLocation(18, 42);   //This is the drop shadow's location
                biContext.setColor(dropShadow);
                biContext.fill(graphArea);

                //Fill the graph area (white):
                graphArea.setLocation(11, 36);
                biContext.setColor(Color.white);
                biContext.fill(graphArea);

                //Draw the graph border (black):
                biContext.setColor(Color.black);
                biContext.draw(graphArea);


                //Header Drop Shadow:
                headerArea.setLocation(18, 12);   //This is the drop shadow's location
                biContext.setColor(dropShadow);
                biContext.fill(headerArea);

                //Fill the header (blue):
                headerArea.setLocation(11, 5);
                biContext.setColor(headerColor);
                biContext.fill(headerArea);

                //Draw the header border (white):
                biContext.setColor(Color.white);
                biContext.draw(headerArea);

                //Insert Header Text:
                biContext.setFont(font);
                biContext.setColor(Color.white);
                biContext.drawString(headerText, 21, 1;

                //Display the average
                biContext.setColor(Color.white);
                biContext.drawString(averageText, 150, 1;


                return biContext;
        }
}
%>;

论坛徽章:
0
3 [报告]
发表于 2003-10-13 23:50 |只看该作者

请教用jsp画直方图!!

怎样使直方图倒过来???我在网上找了个例子,但是一直没有办法使它倒过来??谢谢!!!1

论坛徽章:
0
4 [报告]
发表于 2003-10-14 12:46 |只看该作者

请教用jsp画直方图!!

改改那几个数字吧
setLocation(……)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP