// Generated by CoffeeScript 1.10.0 (function() { var DX, DY, SPEED, TREE_WIDTH, depth_color, height, left, render, render_tree, right, svg, toggle, tree_aree, tree_dip, tree_layout, width; TREE_WIDTH = 340; DX = 25; DY = 50; SPEED = 400; svg = d3.select('svg'); width = svg.node().getBoundingClientRect().width; height = svg.node().getBoundingClientRect().height; left = svg.append('g').attr({ transform: 'translate(16,20)' }); right = svg.append('g').attr({ transform: "translate(" + (width - TREE_WIDTH - 16) + ",20)" }); tree_layout = d3.layout.tree(); toggle = function(n, recursive) { if (n.children != null) { n._children = n.children; delete n.children; if (recursive) { return n._children.forEach(function(c) { return toggle(c, true); }); } } else if (n._children != null) { n.children = n._children; delete n._children; if (recursive) { return n.children.forEach(function(c) { return toggle(c, true); }); } } }; depth_color = d3.scale.linear().domain([0, 3]).range(['#DBE', '#EEE']).interpolate(d3.interpolateHcl); tree_dip = null; tree_aree = null; d3.json('dipartimenti.json', function(_tree_dip) { tree_dip = _tree_dip; return d3.json('aree.json', function(_tree_aree) { tree_aree = _tree_aree; tree_dip.children.forEach(function(c) { return toggle(c, true); }); tree_aree.children.forEach(function(c) { return toggle(c, true); }); return render(); }); }); render = function() { var count_aree, count_dip; count_dip = render_tree(tree_dip, right, true); count_aree = render_tree(tree_aree, left, false); return svg.transition().delay(SPEED).duration(SPEED).attr({ height: Math.max(count_dip, count_aree) * DY + DY }); }; render_tree = function(tree, container, flip) { var enter_nodes, nodes, nodes_data; nodes_data = tree_layout.nodes(tree); nodes_data.forEach(function(d, i) { d.x = (!flip ? d.depth * DX : 0); return d.y = i * DY; }); nodes = container.selectAll('.node').data(nodes_data, function(d) { return d.depth + "_" + d.name; }); nodes.exit().transition().duration(SPEED).attr({ opacity: 0 }).remove(); nodes.transition().delay(SPEED).duration(SPEED).attr({ transform: function(d) { return "translate(" + d.x + ", " + d.y + ")"; } }); enter_nodes = nodes.enter().append('g').attr({ "class": 'node', transform: function(d) { return "translate(" + d.x + ", " + d.y + ")"; }, opacity: 0 }).on('click', function(d) { toggle(d, false); return render(); }).classed('internal', function(d) { return (d.children != null) || (d._children != null); }); enter_nodes.transition().delay(SPEED * 2).duration(SPEED).attr({ opacity: 1 }); enter_nodes.append('rect').attr({ width: function(d) { return TREE_WIDTH - d.depth * DX; }, height: DY, fill: function(d) { return depth_color(d.depth); } }); enter_nodes.append('foreignObject').html(function(d) { return "