我正在使用一个D3 api,它将通过一个servlet获取数据库值,所以第一次加载一些默认值但是当我去搜索时它没有采用新形成的json而且它是用于以前的一。任何人都可以帮忙...
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>No. of Connection</title>
</head>
<style>
.link {
fill: none;
stroke: #666;
stroke-width: 1px;
}
.linnk.licensing {
fill: green;
}
.link.resolved {
stroke: red;
}
circle {
fill: #ccc;
stroke: #333;
stroke-width: 1.5px;
cursor: pointer;
}
text {
font: 10px sans-serif;
pointer-events: none;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
<body>
<div id="main" style="height: 900px; width: 1200px;">
<div id="Header" style="height:100px; width: 800px; display: inline; float: left;"><center><h1>Subscriber Relationship</h1></center></div>
<div id="left" style="width:100px; height:100px; float:right; border: 5px; border-color: chocolate;">
<form action="AServlet">
<center><select name="connections" size="3" style="background-color: bisque;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input type="submit" value="Show"/>
</center>
</form>
</div>
<div id="space" style="height:20px;"></div>
<div id="LinkBody" style="height:800px; width:1000px; display: inline">
<div id="Center" style="height:800px; width:1000px; display:inline; float:left;">
<script>
var links = [];
var nodes = {};
// Compute the distinct nodes from the links.
var width = 1400, height = 800;
var svg = d3.select("#Center").append("svg").attr("width", width).attr(
"height", height);
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker").data(
[ "licensingdense", "licensing", "resolved","resolveddense" ]).enter().append("marker")
.attr("id", function(d) {
return d;
}).attr("viewBox", "0 -5 10 10").attr("refX", 15).attr("refY",
-1.5).attr("markerWidth", 6).attr("markerHeight", 6)
.attr("orient", "auto").append("path").attr("d",
"M0,-5L10,0L0,5");
d3.json("AServlet",function(error, directed) {
links=directed.links;
links.forEach(function(link) {
link.source = nodes[link.source]
|| (nodes[link.source] = {
name : link.source
});
link.target = nodes[link.target]
|| (nodes[link.target] = {
name : ''
});
});
var force = d3.layout.force().nodes(
d3.values(nodes)).links(links).size(
[ width, height ]).linkDistance(100).charge(
-50).on("tick", tick).start();
var path = svg.append("g").selectAll("path").data(
force.links()).enter().append("path").attr(
"class", function(d) {
return "link " + d.type;
}).attr("marker-end", function(d) {
return "url(#" + d.type + ")";
});
var circle = svg.append("g").selectAll("circle")
.data(force.nodes()).enter().append(
"circle").attr("r", 6).call(
force.drag().on("dragstart",dragstart));
var text = svg.append("g").selectAll("text").data(
force.nodes()).enter().append("text").attr(
"x", 8).attr("y", ".31em").text(
function(d) {
return d.name;
});
//selection is happening
var selected = circle.filter(function(d) {
return d.name;
});
selected.each(function(d) {
// d contains the data for the node and this is the circle element
console.log(d.name);
});
var circle = svg.append("g").selectAll("circle")
.data(force.nodes()).enter().append(
"circle").attr("r", 6).on("click",
clickfn).call(force.drag);
var clickfn = function(node) {
alert(node);
}
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
}
function linkArc(d) {
var dx = d.target.x - d.source.x, dy = d.target.y
- d.source.y, dr = Math.sqrt(dx * dx
+ dy * dy);
return "M" + d.source.x + "," + d.source.y
+ "A" + dr + "," + dr + " 0 0,1 "
+ d.target.x + "," + d.target.y;
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
function dragstart(d){
d.fixed=true;
d3.select(this).classed("fixed",true);
}
});
</script>
</div>
</div>
</div>
</div>
</body>
</html>
的Servlet
public class AServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String No_of_Conn = request.getParameter("connections");
System.out.println(No_of_Conn);
String root = null;
String query = null;
Random r = new Random();
EntryListContainer entryListContainer = new EntryListContainer();
List<Entry> entryList1 = new ArrayList<Entry>();
MysqlConnect mc = new MysqlConnect();
Connection conn = mc.dbConnect();
Statement st = conn.createStatement();
if (No_of_Conn != null) {
try {
System.out.println("inside if-->" + No_of_Conn);
query = "select * from moneyball_loader order by no_of_conn limit " + No_of_Conn;
ResultSet rs = st.executeQuery(query);
Entry entry1 = null;
int val, offnet = 0;
int count = 0;
while (rs.next()) {
val = rs.getInt(2);
offnet = rs.getInt(3);
for (int c = 0; c < val; c++) {
entry1 = new Entry();
String num = rs.getString(1);
entry1.setSource(num);
// long con = r.nextInt(1000000) + 100000;
entry1.setTarget(count);
if (c <= offnet) {
if (offnet >= 50) {
entry1.setType("licensingdense");
} else {
entry1.setType("licensing");
}
} else {
entry1.setType("resolved");
}
entryList1.add(entry1);
/*
* Child nodes are created here and put into
* entryListContainer
*/
entryListContainer.setEntryList1(entryList1);
count++;
}
}
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("entryList1", "name");
Gson gson = new GsonBuilder()
.serializeNulls()
.setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping))
.create();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(gson.toJson(entryListContainer));
System.out.println(gson.toJson(entryListContainer));
response.sendRedirect("index.jsp");
} catch (Exception e) {
}
} else {
try {
query = "select * from moneyball_loader limit 10;";
ResultSet rs = st.executeQuery(query);
Entry entry1 = null;
int val, offnet = 0;
int count = 0;
while (rs.next()) {
val = rs.getInt(2);
offnet = rs.getInt(3);
for (int c = 0; c < val; c++) {
entry1 = new Entry();
String num = rs.getString(1);
entry1.setSource(num);
// long con = r.nextInt(1000000) + 100000;
entry1.setTarget(count);
if (c <= offnet) {
if (offnet >= 50) {
entry1.setType("licensingdense");
} else {
entry1.setType("licensing");
}
} else {
entry1.setType("resolved");
}
entryList1.add(entry1);
/*
* Child nodes are created here and put into
* entryListContainer
*/
entryListContainer.setEntryList1(entryList1);
count++;
}
}
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("entryList1", "name");
Gson gson = new GsonBuilder()
.serializeNulls()
.setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping))
.create();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(gson.toJson(entryListContainer));
System.out.println(gson.toJson(entryListContainer));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(LinkServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(LinkServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}