I put my html file and json file under the same folder. When I run my code, it can't load the json file. Here is the html file:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
var nodes1;
d3.json("test.json", function(error, data){
nodes1 = data;
});
var edges1 = [ { source : 0 , target: 1 } , { source : 0 , target: 2 } ,
{ source : 0 , target: 3 } , { source : 1 , target: 4 } ,
{ source : 1 , target: 5 } , { source : 1 , target: 6 } ,
{ source : 0 , target: 4 } , { source : 4 , target: 0 }];
myfunction(nodes1, edges1);
function myfunction(nodes, edges){
var width = 900;
var height = 400;
var svg = d3.select("body")
.append("svg")
.attr("width",width)
.attr("height",height);
var force = d3.layout.force()
.nodes(nodes)
.links(edges)
.size([width,height])
.linkDistance(150)
.charge(-400);
force.start();
console.log(nodes);
console.log(edges);
var svg_edges = svg.selectAll("line")
.data(edges)
.enter()
.append("line")
.style("stroke","#ccc")
.style("stroke-width",1);
var color = d3.scale.category20();
var svg_nodes = svg.selectAll("circle")
.data(nodes)
.enter()
.append("circle")
.attr("r",20)
.style("fill",function(d,i){
return color(i);
})
.call(force.drag);
var svg_texts = svg.selectAll("text")
.data(nodes)
.enter()
.append("text")
.style("fill", "black")
.attr("dx", 20)
.attr("dy", 8)
.text(function(d){
return d.name;
});
force.on("tick", function(){
svg_edges.attr("x1",function(d){ return d.source.x; })
.attr("y1",function(d){ return d.source.y; })
.attr("x2",function(d){ return d.target.x; })
.attr("y2",function(d){ return d.target.y; });
svg_nodes.attr("cx",function(d){ return d.x; })
.attr("cy",function(d){ return d.y; });
svg_texts.attr("x", function(d){ return d.x; })
.attr("y", function(d){ return d.y; });
});
}
</script>
</body>
</html>
And here is my json file:
[
{ name: "www"},
{ name: "ddd"},
{ name: "sdf"},
{ name: "ccc"},
{ name: "ads"},
{ name: "adh"},
{ name: "sdf"}
]
My server is tomcat 7 and I use eclipse to test my code. I also used Chrome, IE11, Edge, firefox and changed security strategy to run my code, but nothing appear on the explorer. Is that might because the wrong file directory? Or the method I use to load json file?