java画直方图_java 实现直方图

这篇博客展示了如何利用Java Servlet动态生成随机值,并通过AJAX更新SVG直方图。直方图的每个条形高度由Servlet计算的随机数决定,实现了直方图的实时变化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1----------------------------------------------------

web.xml

/p>

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

dynMusic

sentrandomvalues

ajaxtest.SentRandomValues

sentrandomvalues

/sentrandomvalues

2-----------------------------------------------

index.jsp

--

%>

index

width="100%"

height="100%">

3,----------------------------------------------------------------

musicRec.svg

/p>

PUBLIC "-//W3C//DTD SVG 1.0//EN"

"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"

οnlοad="timeEvent()">

function

timeEvent(){

showCustomer();

setTimeout("timeEvent()",1000);

}

function showCustomer()

{

xmlHttp=GetXmlHttpObject();

if

(xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var

url="sentrandomvalues";

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

function stateChanged()

{

if

(xmlHttp.readyState==4)

{

var

xmlDoc=xmlHttp.responseXML.documentElement;

random1=xmlDoc.getElementsByTagName("para")[0].childNodes[0].nodeValue;

random2=xmlDoc.getElementsByTagName("para")[2].childNodes[0].nodeValue;

random3=xmlDoc.getElementsByTagName("para")[1].childNodes[0].nodeValue;

random4=xmlDoc.getElementsByTagName("para")[3].childNodes[0].nodeValue;

create_a(random1,random2,random3,random4);

}

}

function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlHttp=new

ActiveXObject("Microsoft.XMLHTTP");

}

}

return

xmlHttp;

}

function create_a(random1,random2,random3,random4)

{

var

rect1=document.getElementById("rect1");

var

rect2=document.getElementById("rect2");

var

rect3=document.getElementById("rect3");

var

rect4=document.getElementById("rect4");

var

text1=document.getElementById("text1");

var

text2=document.getElementById("text2");

var

text3=document.getElementById("text3");

var

text4=document.getElementById("text4");

rect1.setAttribute("height",250-random1);

rect1.setAttribute("y",2+eval(random1));

rect2.setAttribute("height",250-random2);

rect2.setAttribute("y",2+eval(random2));//如果写成2+random2时,会出现字符串相加

rect3.setAttribute("height",250-random3);

rect3.setAttribute("y",2+eval(random3));

rect4.setAttribute("height",250-random4);

rect4.setAttribute("y",2+eval(random4));

}

height="250"

fill="none" stroke="blue" stroke-width="2" />

//

y="50">

//

y="75">

//

y="100">

//

y="125">

fill="yellow" stroke="red" stroke-width="1">

x="125" y="2" height="250" width="15"/>

y="2" height="250" width="15"/>

height="250" width="15"/>

height="250" width="15"/>

4,----------------------------------------------------------

SerntRandomValues.java

package ajaxtest;

import javax.servlet.*;

import javax.servlet.http.*;

import

java.io.*;

import java.util.*;

public class SentRandomValues extends HttpServlet {

private

static final String CONTENT_TYPE = "text/html;

charset=GBK";

//Initialize global variables

public void init() throws ServletException

{

}

//Process the HTTP Get request

public void doGet(HttpServletRequest

request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/xml");

response.setHeader("Cache-Control","no-cache");

StringBuffer sb=new

StringBuffer("");

sb.append("");

sb.append(((Math.random()*250)));

sb.append("");

sb.append("");

sb.append(((Math.random()*250)));

sb.append("");

sb.append("");

sb.append(((Math.random()*250)));

sb.append("");

sb.append("");

sb.append(((Math.random()*250)));

sb.append("");

sb.append("");

PrintWriter out =

response.getWriter();

System.out.println(sb.toString());

out.print(sb.toString());

out.flush();

}

//Process the HTTP Post request

public void doPost(HttpServletRequest

request, HttpServletResponse response) throws ServletException, IOException

{

doGet(request, response);

}

//Clean up resources

public void destroy() {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值