jsp能实现图表吗?(100分)

  • 主题发起人 主题发起人 Lidong
  • 开始时间 开始时间
L

Lidong

Unregistered / Unconfirmed
GUEST, unregistred user!
我想学JSP,但我有个疑问,JSP能将数据转化为图表(如直方图)
在页面上输出吗?实现的步骤?
 
思路大致是这样的:
1. 读出数据;
2. 根据数据在服务器端生成图形流或文件;
3. 将图形流或文件返回给客户端浏览器。
网上有现成的beans,如:<A HREF="http://www.jspin.com">Jspin.com</A>等。
另外也可以将数据传送给一个Applet,由Applet在客户端浏览器中显示出来,
这样的Applet在网上也很多。
 
这个问题本质上就是Java Applet与Beans通讯的问题,
上网查查,很多例子!
 
yzhu:
“另外也可以将数据传送给一个Applet,由Applet在客户端浏览器中显示出来,
这样的Applet在网上也很多。”
给出一个站点,好吗?
 
你直接向浏览器输出一个图形流试试,我自己也会去试。
 
谁能解决用java在服务器端生成图形的问题, 另外200分赠送.
 
Image generation on Java 1.1
For my first generated image, I will create code I can deploy on the Java 1.1 platform. Although 1.2 JVMs are fairly common, not everyone can count on deploying code on the latest platform. Some commercial Web-hosting environments, for example, are somewhat slow to update the Java Runtime Environments (JREs) on their servers. This means that any servlets you deploy must run on Java 1.1. Designing the code for Java 1.1do
es not greatly complicate matters;
however, Ido
discuss a troublesome issue regarding in-memory image creation later in the article.
I will create the image of a simple pie chart. A pie chart is a useful, easy-to-read chart. This pie chart will be complete with colorization and labeling of the pie's pieces. The chart will indicate soft drinks favored by software developers. Now I'll take a look at how you can go about this.
Fordo
ing image I/O on Java 1.1, Sun offers a class library known as Jimi. Sun purchased this class library from a small software vendor. After Sun began distributing the class library, it repackaged the classes into com.sun, but otherwise left them unchanged. The following are the steps you should take to produce a PNG image using the Jimi toolkit.

Construct a Frame window to provide an AWT image.
Use the AWT image to get a Graphics instance.
Draw into the Graphics object.
Create a JimiWriter using the HTTP output stream.
Have the JimiWriter write out the AWT image previously created.
On the Java 1.1 platform, constructing an in-memory image appears to require that an AWT component exist on the desktop. Normally, you should not use Java servlet code to create AWT components. It justdo
esn't make sense for the Web server to create user interface components to respond to a client request. In this case however, you need an image to provide a Graphics instance that you can draw into. So, you will have to live with the small window that appears on the Web server's desktop. If you know of a way to create an image on Java 1.1 without an AWT component, please let me know. One possible design would use a template image on the Web server's disk. You could load the image data from the disk, and construct the AWT image using java.awt.Toolkit.createImage(). After you construct the AWT image, it can provide the Graphics instance, which you can draw into as usual. If this works, you could avoid the spurious window. I will leave this experiment as an exercise to the interested reader.
First of all, the connection between the servlet and the class to produce the pie chart is the interface discussed earlier, ImageProducer. When you construct JIMIProducer, the ImageProducer for the pie chart, it will create an AWT frame, an AWT image, and subsequently an AWT graphics context. The following code sample is from the JIMIProducer:
Frame f = new Frame();
f.setVisible(true);
image = f.createImage(ImageWidth, ImageHeight);
graphics = image.getGraphics();
f.setVisible(false);

The key method in the JIMIProducer class is drawSlice(). Given a label and the number of degrees the slice should consume, the class draws a slice of the pie chart and colorizes it. Next, I'll discuss the way this method works and revisit some high school geometry while I'm at it.
You begin
drawing the pie chart internals at the three o'clock position;
that is, you draw a line from the chart's center to the three o'clock point on the bounding oval. After you draw the initial line, you draw each additional line in a clockwise direction. The following code takes the number of degrees passed to the method and draws a line reflecting a pie slice with that angle. The calculation first converts the angle to radians;
I start with degrees because it seems more natural. At any rate, the angle is converted to Cartesian coordinates. This x and y value is then
used as an offset to the chart's center point. A line is subsequently drawn from the center to the calculated point. This process is repeated for each call to the drawSlice method;
the accumulated calls to this method should total 360 degrees, or the pie chart will not look right.
//***************************************************************
// Convert to radians
// 1 degree = pi/180 radians
//***************************************************************
do
uble theta = degrees * (3.14/180);
currentTheta += theta;
//***************************************************************
// Convert to x/y
// x = r cos @
// y = r sin @
//***************************************************************
do
uble x = Radius * Math.cos(currentTheta);
do
uble y = Radius * Math.sin(currentTheta);
Point mark2 = new Point(center);
mark2.translate((int)x,(int)y);
graphics.drawLine(center.x, center.y, mark2.x, mark2.y);

The next step is to provide a unique color for the new pie slice. The following code illustrates how you cando
this. The handy fillArc() method makes it easy to color the pie slice.
graphics.setColor(colors[colorIndex++]);
graphics.fillArc(Inset,
Inset,
PieWidth,
PieHeight,
-1 * lastAngle,
-1 * degrees);

The final task for drawSlice() is to label the new pie slice. The code calculates an appropriate place for the label based on the font metrics and then
draws the text. The complete pie chart is found in Figure 1.
不想为你做翻译了。
 
对不起, 我的目的是用jsp或servlet来作.
 
to cytown:
你是想在服务器端生成gif/jpg图形后由页面(html)引用吗?
 
给你一个最新消息:可以,如果你答应给我你这个问题的分,我将给你源代码。
 
cytown@btamail.net.cn
200分, 决不食言!
 
给我原程序,200分就是你的啦
zyccld@zpeb.sinopec.com.cn
 
你先在http://java.sun.com/products/jimi/去当一个
jimi包下来。然后,安装后。
到http://www.javaworld.com/jw-05-2000/servlets/Servlets.zip去当这个程序。
然后,将ImageServlet.java 的doget方法中的
imageProducer =
(ImageProducer) Class.forName(request.getQueryString()).newInstance();

type = imageProducer.createImage(response.getOutputStream());
改为:
StockGraphProducer producer = new StockGraphProducer();
String type =producer.createImage(response.getOutputStream());
,一切搞定。
 
要applet来画图干吗?用js直接画直方图,甚至直接用html平装直方图不就可以了》
 
// This is a simple Java servlet which can generate jpeg image.
// It can be used in html file, e.g. <img src=http://yourserver/servlet/servletjpeg?str=hi>
//
// (C)Copyright by Sun Yiyi (yysun@263.net)
// 3.21.2000

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;

public class servletjpeg extends HttpServlet{

public voiddo
Get(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

String str = req.getParameter("str");
Frame frame = new Frame();
frame.addNotify();
frame.setBackground(new Color(255,212,212));
BufferedImage image = (BufferedImage) frame.createImage(400, 200);
Graphics g= image.getGraphics();
g.drawString(str, 50, 50);
res.setContentType("image/jpeg");
ServletOutputStream out = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
}

public voiddo
Post(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}



 
多人接受答案了。
 
后退
顶部