Skip to content

Instantly share code, notes, and snippets.

@stimms
Forked from d3noob/.block
Last active February 6, 2022 23:30
Show Gist options
  • Save stimms/4aa633d07e40ff2346cfbe503ca19045 to your computer and use it in GitHub Desktop.
Save stimms/4aa633d07e40ff2346cfbe503ca19045 to your computer and use it in GitHub Desktop.
Sample of layout for question flow
license: mit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
.link-text{
font: 10px sans-serif;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script>
var data = [
{ id: 1, "name": "Are you happy today?", "parent": null },
{ id: 2, "name": "Are you currently performing to your potential?", "parent": 1, "parentAnswer": "yes" },
{ id: 3, "name": "Are you healthy?", "parent": 1, "parentAnswer": "no" },
{ id: 4, "name": "Do you think you're getting out enough?", "parent": 2, "parentAnswer": "yes" },
{ id: 5, "name": "Are you getting enough exercise?", "parent": 3, "parentAnswer": "yes" },
{ id: 6, "name": "Have you considered swimming?", "parent": 5, "parentAnswer": "yes" },
{ id: 7, "name": "Would new socks help?", "parent": 2, "parentAnswer": "no" },
];
// *********** Convert flat data into a nice tree ***************
// create a name: node map
var dataMap = data.reduce(function (map, node) {
map[node.id] = node;
return map;
}, {});
// create the tree array
var treeData = [];
data.forEach(function (node) {
// add to parent
var parent = dataMap[node.parent];
if (parent) {
// create child array if it doesn't exist
(parent.children || (parent.children = []))
// add node to child array
.push(node);
} else {
// parent is null or missing
treeData.push(node);
}
});
// ************** Generate the tree diagram *****************
var margin = { top: 20, right: 120, bottom: 20, left: 120 },
width = 1050 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function (d) {
return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function (d) { d.y = d.depth * 180; });
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function (d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("x", function (d) {
return d.children || d._children ? 0 : 13;
})
.attr("dy", "-1.35em")
.attr("text-anchor", function (d) {
return d.children || d._children ? "end" : "start";
})
.text(function (d) { return d.name; })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function (d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
link.enter().append("text")
.attr("class", "link-text")
.attr("y", function (d) {
return (d.target.x + d.source.x)/2;
})
.attr("x", function (d) {
return (d.target.y + d.source.y)/2;
})
.text((d) => {
return d.target.parentAnswer});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment