score:1
Accepted answer
Quick answer
node.filter(d=>d.depth==0)
.attr('transform',function(d){
var width = this.getBoundingClientRect().width;
return `translate(${d.y+width/2},${d.x})`
})
Check out snippet
var data = {
"name": "root",
"children": [{
"name": "Person Name 1",
"children": [{
"name": "Branch 4.1"
}, {
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
}
]
}, {
"name": "Person name 2",
"children": [{
"name": "Branch 4.1"
}, {
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
}
]
}, {
"name": "Person Name 3",
"children": [{
"name": "Branch 4.1"
}, {
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
}
]
}, {
"name": "Person Name 4",
"children": [{
"name": "Branch 4.1"
}, {
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
},{
"name": "Branch 4.2"
},
{
"name": "Branch 4.2"
}
]
}
]
};
const LAST_CHILDREN_WIDTH = 13;
let flagForChildren = false;
var groups = [];
data.children.forEach(d => {
let a = [];
if (d.children.length > 0) {
flagForChildren = true;
}
for (var i = 0; i < d.children.length; i += 2) {
let b = d.children.slice(i, i + 2);
if (b[0] && b[1]) {
a.push(Object.assign(b[0], { children: [b[1]] }));
} else {
let child = b[0];
if (i === 7) {
child = Object.assign(child, { children: [{ name: "..." }] });
}
a.push(child);
}
}
d.children = a;
groups.push(d);
});
data.children = groups;
let split_index = Math.round(data.children.length / 2);
let rectangleHeight = 50;
// Left data
let leftData = {
name: data.name,
children: JSON.parse(JSON.stringify(data.children.slice(0, split_index)))
};
// Right data
let rightData = {
name: data.name,
children: JSON.parse(JSON.stringify(data.children.slice(split_index)))
};
// Create d3 hierarchies
let left = d3.hierarchy(leftData);
// Render both trees
drawTree(left, "left");
drawTree(left, "right");
// draw single tree
function drawTree(root, pos) {
let SWITCH_CONST = 1;
if (pos === "left") {
SWITCH_CONST = -1;
}
var margin = { top: 20, right: 90, bottom: 30, left: 90 },
width = window.innerWidth - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
let svg = d3
.select("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.right + margin.left)
.attr('view-box', '0 0 ' + (width + margin.right) + ' ' + (height + margin.top + margin.bottom))
.attr('preserveAspectRatio', "xMidYMid meet")
.style("margin-left", "-30px")
.style("margin-top", "80px");
// Shift the entire tree by half it's width
let g = svg.append("g").attr("transform", "translate(" + width / 2 + ",0)");
let deductWidthValue = flagForChildren ? 0 : width * 0.33;
// Create new default tree layout
let tree = d3
.tree()
.size([height - 50, SWITCH_CONST * (width - deductWidthValue) / 2])
.separation(function(a, b) {
return a.parent === b.parent ? 4 : 4.25;
});
tree(root);
let nodes = root.descendants();
let links = root.links();
// Set both root nodes to be dead center vertically
nodes[0].x = height / 2;
// Create links
let link = g
.selectAll(".link")
.data(links)
.enter();
link
.append("line")
.attr("class", "link")
.attr("x1", function(d) {
if (
d.target.parent &&
d.target.parent.parent &&
d.target.parent.parent.parent
) {
return 0;
}
return d.source.y + 100 / 2;
})
.attr("x2", function(d) {
if (
d.target.parent &&
d.target.parent.parent &&
d.target.parent.parent.parent
) {
return 0;
} else if (d.target.parent && d.target.parent.parent) {
return d.target.y;
}
return d.target.y + 100 / 2;
})
.attr("y1", function(d) {
if (
d.target.parent &&
d.target.parent.parent &&
d.target.parent.parent.parent
) {
return 0;
}
return d.source.x + 50 / 2;
})
.attr("y2", function(d) {
if (
d.target.parent &&
d.target.parent.parent &&
d.target.parent.parent.parent
) {
return 0;
} else if (d.target.parent && d.target.parent.parent) {
return d.target.x + LAST_CHILDREN_WIDTH / 2;
}
return d.target.x + 50 / 2;
});
//Rectangle width
let node = g
.selectAll(".node")
.data(nodes)
.enter()
.append("g")
.attr("class", function(d) {
return "node" + (d.children ? " node--internal" : " node--leaf");
})
.attr("transform", function(d) {
if (d.parent && d.parent.parent) {
if (d.parent.parent.parent) {
return (
"translate(" +
d.parent.y +
"," +
(d.x + LAST_CHILDREN_WIDTH + 3) +
")"
);
}
return "translate(" + d.y + "," + d.x + ")";
}
return "translate(" + d.y + "," + d.x + ")";
});
node
.append("rect")
.attr("height", function(d, i) {
return d.parent && d.parent.parent ? 20 : rectangleHeight;
})
.attr("width", function(d, i) {
return d.parent && d.parent.parent ? 15 : rectangleWidth(d);
})
.attr("rx", function(d, i) {
return d.parent && d.parent.parent ? 5 : 5;
})
.attr("ry", function(d, i) {
return d.parent && d.parent.parent ? 5 : 5;
});
node
.append("text")
.attr("dy", function(d, i) {
return d.parent && d.parent.parent ? 18 : 28;
})
.attr("dx", function(d, i) {
if (!(d.parent && d.parent.parent)) {
return 10;
} else {
return 20;
}
})
.style("fill", function(d, i) {
return d.parent && d.parent.parent ? "Black" : "White";
})
.text(function(d) {
let name = d.data.topic_name || d.data.name;
return name;
})
node.filter(d=>d.depth==0)
.attr('transform',function(d){
var width = this.getBoundingClientRect().width;
return `translate(${d.y+width/2},${d.x})`
})
}
function rectangleWidth(d) {
let dynamicLength = 6;
if (d.data.topic_name) {
dynamicLength = d.data.topic_name.length;
} else if (d.data.name) {
dynamicLength = d.data.name.length;
}
dynamicLength = dynamicLength < 5 ? 5 : dynamicLength;
dynamicLength = dynamicLength > 30 ? 30 : dynamicLength;
return 10 + dynamicLength * 8;
}
rect {
fill: #149c10;
}
text {
font: 14px sans-serif;
}
text:hover {
cursor: pointer;
}
.breadcrumb-chip {
margin-top: 10px;
background: #5dca5d !important;
margin-left: -30px;
}
.breadcrumb-chip:hover {
cursor: pointer;
}
line {
fill: #149c10;
stroke-width: 1px;
stroke: #149c10;
}
.breadcrumb-path {
display: block;
width: 1px;
border: 0.5px solid #149c10;
position: absolute;
top: 90px;
margin-left: 0px;
bottom: 0;
height: 10px;
}
.search-topic {
margin: auto;
}
.btn-topic-search {
min-height: 40px !important;
}
.breadcrumb-chip-container {
width: 0px !important;
margin: 0 auto !important;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
</head>
<body>
<svg className='spider-graph-svg'>
</svg>
</body>
</html>
Source: stackoverflow.com
Related Query
- centralize root node in svg
- clicking a node in d3 from a button outside the svg
- D3 force layout fix root node at the center
- Space out nodes evenly around root node in D3 force layout
- D3 update on node removal always remove the last entry in SVG DOM
- Append <div> to node in SVG with D3
- Center force directed layout after tick using root node (return to center)
- Does the d3 treemap layout get cached when a root node is passed to it?
- Svg image representing a node, varying node size in a force-directed graph
- Accurate pan and zoom to svg node
- How do I save an svg node to a file using nodejs?
- Hiding Root node in d3 tree
- Hide root node and edges in D3 v4 Tree Diagram
- How to center root node circle in a d3.layout.pack graph?
- D3: Sunburst chart with root node on the outside ring
- Adding class to root node in d3js tree layout
- How to highlight path from root to selected node in d3 js?
- Radial Reingold Tilford Tree remove root node
- How do I move an svg node from one group to another?
- Radial Tree - Root node issue
- How to render svg in backend and save to local using in node js using d3?
- How can I get the dom svg element from d3 node data?
- SVG nodes in D3 force layout moves on node scale
- Root node size to take sum of children nodes size when collapsed
- Distribute nodes evenly around central node using a relative SVG width and height
- Selecting from SVG with node as argument throws DOM Exception 12
- Place svg image created by d3.js before a HTML node
- Choosing a root node based on key value
- On click re-position root node and adjust window
- d3.js tree diagram - expand only next level nodes when clicking on root node
More Query from same tag
- d3.js click and apply zoom and pan to distribute points located inside a targeted division to the triggered subdivisions
- how to properly type zoom on d3 svg with typescript?
- Drag multiple items inside another item in d3
- How to have an entity trigger a different entities mouseover event (D3)
- Requesting embedded JSON data for D3
- creating D3 Word Cloud by using an array of json objects instead of reading from json file
- D3.js ~ Drop down lists with dependency based on dataset
- How to set a specific duration to interpolate along a path one point at time?
- D3: How to detect the end of the .each()
- Rotate Hierarchical Bar chart
- Data not formatted to date properly in d3
- d3 make a force-directed-tree layout into a circle shape
- axis scale in cubism context in d3.js
- Error processing data for d3 bar chart
- d3 animate line graph horizontally by changing parameterized function
- Cannot select SVG foreignObject element in d3
- Access svg attributes from index.html in d3.js
- How perform mouseover event on pair of items in d3?
- D3 force directed graph drag and zoom functions not working
- pan d3.js graph
- Changing Edge color with D3
- How can i localizate circles on a specific region in d3.js
- c3.js - How to manually trigger the hover of a donut arc?
- Parsing specific columns/data from multiple CSV file
- How to map an array of objects correctly in Javascript
- Is it possible to debounce d3.zoom callback?
- Error: <path> attribute d: Expected number, "MNaN" - D3.js
- Unable to add transition in d3
- Merge array of objects based on id for D3 bezier links
- d3.json, d3.xhr and cross-domain problems