|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<script src="d3/d3.v3.min.js"></script> |
| 4 | +<style> |
| 5 | + |
| 6 | +.link { |
| 7 | + stroke: #ccc; |
| 8 | +} |
| 9 | + |
| 10 | +.node text { |
| 11 | + pointer-events: none; |
| 12 | + font: 10px sans-serif; |
| 13 | +} |
| 14 | + |
| 15 | +</style> |
| 16 | +<body> |
| 17 | +<script> |
| 18 | + |
| 19 | +var width = window.innerWidth, |
| 20 | + height = window.innerHeight; |
| 21 | + |
| 22 | +var svg = d3.select("body").append("svg") |
| 23 | + .attr("width", width) |
| 24 | + .attr("height", height); |
| 25 | + |
| 26 | +var force = d3.layout.force() |
| 27 | + .charge(-1299) |
| 28 | + .linkDistance(function(d) { return height / (2 + (2 * d.level)); }) |
| 29 | + .size([width, height]); |
| 30 | + |
| 31 | +d3.json("network.json", function(json) { |
| 32 | + force |
| 33 | + .nodes(json.nodes) |
| 34 | + .links(json.links) |
| 35 | + .start(); |
| 36 | + |
| 37 | + var link = svg.selectAll(".link") |
| 38 | + .data(json.links) |
| 39 | + .enter().append("line") |
| 40 | + .attr("class", "link"); |
| 41 | + |
| 42 | + var node = svg.selectAll(".node") |
| 43 | + .data(json.nodes) |
| 44 | + .enter().append("g") |
| 45 | + .attr("class", "node") |
| 46 | + .call(force.drag); |
| 47 | + |
| 48 | +// node.append("circle") |
| 49 | +// .attr("r", function(d) { return d.radius; }) |
| 50 | +// .style("fill", function(d) { return d.fill; }); |
| 51 | + |
| 52 | + node.append("image") |
| 53 | + .attr("xlink:href", function(d) { return "images/" + d.logo; }) |
| 54 | + .attr("x", function(d) { return 0 - (d.width / 2); }) |
| 55 | + .attr("y", function(d) { return 0 - (d.height / 2); }) |
| 56 | + .attr("width", function(d) { return d.width; }) |
| 57 | + .attr("height", function(d) { return d.height; }) |
| 58 | + .on("click", function(d) { window.location = d.url; }); |
| 59 | + |
| 60 | +// node.append("text") |
| 61 | +// .attr("dx", 12) |
| 62 | +// .attr("dy", ".35em") |
| 63 | +// .text(function(d) { return d.name }); |
| 64 | + |
| 65 | + force.on("tick", function() { |
| 66 | + link.attr("x1", function(d) { return d.source.x; }) |
| 67 | + .attr("y1", function(d) { return d.source.y; }) |
| 68 | + .attr("x2", function(d) { return d.target.x; }) |
| 69 | + .attr("y2", function(d) { return d.target.y; }); |
| 70 | + |
| 71 | + node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +</script> |
0 commit comments