|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<meta charset="utf-8"> |
| 5 | +<style> |
| 6 | + |
| 7 | +.node { |
| 8 | + cursor: pointer; |
| 9 | +} |
| 10 | + |
| 11 | +.node circle { |
| 12 | + fill: #fff; |
| 13 | + stroke: steelblue; |
| 14 | + stroke-width: 1.5px; |
| 15 | +} |
| 16 | + |
| 17 | +.node text { |
| 18 | + font: 10px sans-serif; |
| 19 | +} |
| 20 | + |
| 21 | +.link { |
| 22 | + fill: none; |
| 23 | + stroke: #ccc; |
| 24 | + stroke-width: 1.5px; |
| 25 | +} |
| 26 | + |
| 27 | +</style> |
| 28 | +<head> |
| 29 | + <title>Project Civics GovMap</title> |
| 30 | +</head> |
| 31 | + |
| 32 | +<body> |
| 33 | +<script src="https://d3js.org/d3.v3.min.js"></script> |
| 34 | +<script> |
| 35 | + |
| 36 | +var margin = {top: 20, right: 120, bottom: 20, left: 120}, |
| 37 | + width = 960 - margin.right - margin.left, |
| 38 | + height = 800 - margin.top - margin.bottom; |
| 39 | + |
| 40 | +var i = 0, |
| 41 | + duration = 750, |
| 42 | + root; |
| 43 | + |
| 44 | +var tree = d3.layout.tree() |
| 45 | + .size([height, width]); |
| 46 | + |
| 47 | +var diagonal = d3.svg.diagonal() |
| 48 | + .projection(function(d) { return [d.y, d.x]; }); |
| 49 | + |
| 50 | +var svg = d3.select("body").append("svg") |
| 51 | + .attr("width", width + margin.right + margin.left) |
| 52 | + .attr("height", height + margin.top + margin.bottom) |
| 53 | + .append("g") |
| 54 | + .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
| 55 | + |
| 56 | +d3.json("GovMap.json", function(error, GovMap) { |
| 57 | + if (error) throw error; |
| 58 | + |
| 59 | + root = GovMap; |
| 60 | + root.x0 = height / 2; |
| 61 | + root.y0 = 0; |
| 62 | + |
| 63 | + function collapse(d) { |
| 64 | + if (d.children) { |
| 65 | + d._children = d.children; |
| 66 | + d._children.forEach(collapse); |
| 67 | + d.children = null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + root.children.forEach(collapse); |
| 72 | + update(root); |
| 73 | +}); |
| 74 | + |
| 75 | +d3.select(self.frameElement).style("height", "800px"); |
| 76 | + |
| 77 | +function update(source) { |
| 78 | + |
| 79 | + // Compute the new tree layout. |
| 80 | + var nodes = tree.nodes(root).reverse(), |
| 81 | + links = tree.links(nodes); |
| 82 | + |
| 83 | + // Normalize for fixed-depth. |
| 84 | + nodes.forEach(function(d) { d.y = d.depth * 180; }); |
| 85 | + |
| 86 | + // Update the nodes… |
| 87 | + var node = svg.selectAll("g.node") |
| 88 | + .data(nodes, function(d) { return d.id || (d.id = ++i); }); |
| 89 | + |
| 90 | + // Enter any new nodes at the parent's previous position. |
| 91 | + var nodeEnter = node.enter().append("g") |
| 92 | + .attr("class", "node") |
| 93 | + .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }) |
| 94 | + .on("click", click); |
| 95 | + |
| 96 | + nodeEnter.append("circle") |
| 97 | + .attr("r", 1e-6) |
| 98 | + .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); |
| 99 | + |
| 100 | + nodeEnter.append("text") |
| 101 | + .attr("x", function(d) { return d.children || d._children ? -10 : 10; }) |
| 102 | + .attr("dy", ".35em") |
| 103 | + .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) |
| 104 | + .text(function(d) { return d.name; }) |
| 105 | + .style("fill-opacity", 1e-6); |
| 106 | + |
| 107 | + // Transition nodes to their new position. |
| 108 | + var nodeUpdate = node.transition() |
| 109 | + .duration(duration) |
| 110 | + .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); |
| 111 | + |
| 112 | + nodeUpdate.select("circle") |
| 113 | + .attr("r", 4.5) |
| 114 | + .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); |
| 115 | + |
| 116 | + nodeUpdate.select("text") |
| 117 | + .style("fill-opacity", 1); |
| 118 | + |
| 119 | + // Transition exiting nodes to the parent's new position. |
| 120 | + var nodeExit = node.exit().transition() |
| 121 | + .duration(duration) |
| 122 | + .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; }) |
| 123 | + .remove(); |
| 124 | + |
| 125 | + nodeExit.select("circle") |
| 126 | + .attr("r", 1e-6); |
| 127 | + |
| 128 | + nodeExit.select("text") |
| 129 | + .style("fill-opacity", 1e-6); |
| 130 | + |
| 131 | + // Update the links… |
| 132 | + var link = svg.selectAll("path.link") |
| 133 | + .data(links, function(d) { return d.target.id; }); |
| 134 | + |
| 135 | + // Enter any new links at the parent's previous position. |
| 136 | + link.enter().insert("path", "g") |
| 137 | + .attr("class", "link") |
| 138 | + .attr("d", function(d) { |
| 139 | + var o = {x: source.x0, y: source.y0}; |
| 140 | + return diagonal({source: o, target: o}); |
| 141 | + }); |
| 142 | + |
| 143 | + // Transition links to their new position. |
| 144 | + link.transition() |
| 145 | + .duration(duration) |
| 146 | + .attr("d", diagonal); |
| 147 | + |
| 148 | + // Transition exiting nodes to the parent's new position. |
| 149 | + link.exit().transition() |
| 150 | + .duration(duration) |
| 151 | + .attr("d", function(d) { |
| 152 | + var o = {x: source.x, y: source.y}; |
| 153 | + return diagonal({source: o, target: o}); |
| 154 | + }) |
| 155 | + .remove(); |
| 156 | + |
| 157 | + // Stash the old positions for transition. |
| 158 | + nodes.forEach(function(d) { |
| 159 | + d.x0 = d.x; |
| 160 | + d.y0 = d.y; |
| 161 | + }); |
| 162 | +} |
| 163 | + |
| 164 | +// Toggle children on click. |
| 165 | +function click(d) { |
| 166 | + if (d.children) { |
| 167 | + d._children = d.children; |
| 168 | + d.children = null; |
| 169 | + } else { |
| 170 | + d.children = d._children; |
| 171 | + d._children = null; |
| 172 | + } |
| 173 | + update(d); |
| 174 | +} |
| 175 | + |
| 176 | +</script> |
| 177 | + |
| 178 | +</body> |
| 179 | + |
| 180 | +</html> |
0 commit comments