<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<style>
/*
.node {
stroke: #ffffff;
stroke-width: 0.1px;
}
*/
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script src="d3.min.js"></script>
<script>
var width = 960,
height = 500;
var colors = d3.scale.category20();
var force = d3.layout.force()
.charge(-320)
.linkDistance(100)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("miserables1.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.attr("id", function(d) { return d.value; })
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
link.append('text')
/*.attr('x', 10)
.attr('y', 20)
.style('fill', 'steelblue')
.style('font-size', '24px')
.style('font-weight', 'bold')
.append('textPath')
.attr({//引用路径
'xlink:href': function(d) { return "'#"+d.value+"'"; }
})*/
.text(function(d) { return d.value; });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 10)
.style("fill", function(d) { return colors(d.group); });
node.append("title")
.text(function(d) { return d.name});
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
/*force.on("tick", function() {
link.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; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});*/
function tick() {//打点更新坐标
link
.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; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
});
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<style>
/*
.node {
stroke: #ffffff;
stroke-width: 0.1px;
}
*/
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script src="d3.min.js"></script>
<script>
var width = 960,
height = 500;
var colors = d3.scale.category20();
var force = d3.layout.force()
.charge(-320)
.linkDistance(100)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("miserables1.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.attr("id", function(d) { return d.value; })
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
link.append('text')
/*.attr('x', 10)
.attr('y', 20)
.style('fill', 'steelblue')
.style('font-size', '24px')
.style('font-weight', 'bold')
.append('textPath')
.attr({//引用路径
'xlink:href': function(d) { return "'#"+d.value+"'"; }
})*/
.text(function(d) { return d.value; });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 10)
.style("fill", function(d) { return colors(d.group); });
node.append("title")
.text(function(d) { return d.name});
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
/*force.on("tick", function() {
link.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; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});*/
function tick() {//打点更新坐标
link
.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; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
});
</script>
</body>
</html>