/* Sample Usage: var myData = { "name": "Root", "children": [ { "name": "Branch 1", "children": [ {"name": "Leaf 3"}, {"name": "Leaf 4"} ] }, {"name": "Branch 2"} ] }; var chart = MindMap() .width(900) .height(500) ; d3.select('#chart svg') .datum(myData) .call(chart) ; */ var MindMap = function(){ "use strict"; var margin = {top: 20, left: 120, bottom: 20, right: 120}, width = 960, height = 500, duration = 500, identity = '_id', handleClick = function(){}, text = function(d){ return d.name; }, idx = 0, enterNode = function(node){ node.append("svg:circle") .attr("r", 1e-6); node.append("svg:text") .attr("text-anchor", "middle") .attr("dy", 14) .text(text) .style("fill-opacity", 1); }, updateNode = function(node){ node.select("text") .text(text); node.select("circle") .attr("r", 4.5); }, exitNode = function(node){ node.select("circle") .attr("r", 1e-6); node.select("text") .style("fill-opacity", 1e-6); } ; var connector = MindMap.elbow; var chart = function(selection){ selection.each(function(root){ var w = width - margin.left - margin.right; var h = height - margin.top - margin.bottom; var container = d3.select(this); var vis = container .attr("width", width) .attr("height", height) ; var graphRoot = vis.select('g'); if(!graphRoot[0][0]){ vis = vis.append('svg:g'); }else{ vis = graphRoot; } vis = vis .attr("transform", "translate(" + (w/2+margin.left) + "," + margin.top + ")") ; root.x0 = h / 2; root.y0 = 0; var tree = d3.layout.tree() .size([h, w]); chart.update = function() { container.transition().duration(duration).call(chart); }; // Ensure we have Left and Right node lists if(!(root.left || root.right)){ var i=0, l = (root.children||[]).length; root.left = []; root.right = []; for(; i 0) { obj["@attributes"] = {}; for (var j = 0; j < xml.attributes.length; j++) { var attribute = xml.attributes.item(j); obj["@attributes"][attribute.nodeName] = attribute.nodeValue; } } } else if (xml.nodeType == 3) { // text obj = xml.nodeValue; } // do children if (xml.hasChildNodes()) { for(var i = 0; i < xml.childNodes.length; i++) { var item = xml.childNodes.item(i); var nodeName = item.nodeName; if (typeof(obj[nodeName]) == "undefined") { obj[nodeName] = xmlToJson(item); } else { if (typeof(obj[nodeName].push) == "undefined") { var old = obj[nodeName]; obj[nodeName] = []; obj[nodeName].push(old); } obj[nodeName].push(xmlToJson(item)); } } } return obj; }; var js = xmlToJson(xml); var data = js.map.node; var parseData = function(data, direction){ var key, i, l, dir = direction, node = {}, child; for(key in data['@attributes']){ node[key.toLowerCase()] = data['@attributes'][key]; } node.direction = node.direction || dir; l = (data.node || []).length; if(l){ node.children = []; for(i=0; i