Skip to content

Instantly share code, notes, and snippets.

@kgeorgiou
Last active November 23, 2021 21:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kgeorgiou/68f864364f277720252d0329408433ae to your computer and use it in GitHub Desktop.
Save kgeorgiou/68f864364f277720252d0329408433ae to your computer and use it in GitHub Desktop.
Node Pies in Force-Directed Layout
license: mit
{
"nodes": [
{
"id": "a", "group": 1,
"pieChart" : [
{ "color": 1, "percent": 40 },
{ "color": 2, "percent": 20 },
{ "color": 3, "percent": 20 },
{ "color": 4, "percent": 20 }
]
},
{
"id": "b", "group": 2,
"pieChart" : [
{ "color": 1, "percent": 33.33 },
{ "color": 2, "percent": 33.34 },
{ "color": 3, "percent": 33.33 }
]
},
{
"id": "c", "group": 3,
"pieChart" : [
{ "color": 1, "percent": 25 },
{ "color": 2, "percent": 25 },
{ "color": 3, "percent": 25 },
{ "color": 4, "percent": 25 }
]
},
{
"id": "d", "group": 4,
"pieChart" : [
{ "color": 1, "percent": 33.33 },
{ "color": 3, "percent": 33.33 },
{ "color": 4, "percent": 33.34 }
]
},
{
"id": "e", "group": 1,
"pieChart" : [
{ "color": 1, "percent": 100 }
]
},
{
"id": "f", "group": 1,
"pieChart" : [
{ "color": 1, "percent": 100 }
]
},
{
"id": "g", "group": 2,
"pieChart" : [
{ "color": 2, "percent": 50 },
{ "color": 3, "percent": 50 }
]
},
{
"id": "h", "group": 3,
"pieChart" : [
{ "color": 2, "percent": 50 },
{ "color": 3, "percent": 50 }
]
},
{
"id": "i", "group": 4,
"pieChart" : [
{ "color": 4, "percent": 100 }
]
}
],
"links": [
{"source": "a", "target": "b", "value": 5},
{"source": "a", "target": "c", "value": 5},
{"source": "a", "target": "d", "value": 5},
{"source": "a", "target": "e", "value": 5},
{"source": "a", "target": "f", "value": 5},
{"source": "b", "target": "c", "value": 5},
{"source": "b", "target": "g", "value": 5},
{"source": "c", "target": "d", "value": 5},
{"source": "c", "target": "h", "value": 5},
{"source": "d", "target": "i", "value": 5},
{"source": "e", "target": "f", "value": 5},
{"source": "h", "target": "g", "value": 5}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
text {
font-size: 1em;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
</style>
<svg width="960" height="600"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="node-pie.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory10);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function (d) {
return d.id;
}))
.force("charge", d3.forceManyBody().strength(-900))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("data.json", function (error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function (d) {
return Math.sqrt(d.value);
});
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter()
.append("g")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
/* Draw the respective pie chart for each node */
node.each(function (d) {
NodePieBuilder.drawNodePie(d3.select(this), d.pieChart, {
parentNodeColor: color(d.group),
outerStrokeWidth: 12,
showLabelText: true,
labelText: d.id,
labelColor: color(d.group)
});
});
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
link
.attr("x1", function (d) {
return d.source.x;
})
.attr("y1", function (d) {
return d.source.y;
})
.attr("x2", function (d) {
return d.target.x;
})
.attr("y2", function (d) {
return d.target.y;
});
d3.selectAll("circle").attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
d3.selectAll("text").attr("x", function (d) {
return d.x;
})
.attr("y", function (d) {
return d.y;
});
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
var DEFAULT_OPTIONS = {
radius: 20,
outerStrokeWidth: 10,
parentNodeColor: 'blue',
showPieChartBorder: true,
pieChartBorderColor: 'white',
pieChartBorderWidth: '2',
showLabelText: false,
labelText: 'text',
labelColor: 'blue'
};
function getOptionOrDefault(key, options, defaultOptions) {
defaultOptions = defaultOptions || DEFAULT_OPTIONS;
if (options && key in options) {
return options[key];
}
return defaultOptions[key];
}
function drawParentCircle(nodeElement, options) {
var outerStrokeWidth = getOptionOrDefault('outerStrokeWidth', options);
var radius = getOptionOrDefault('radius', options);
var parentNodeColor = getOptionOrDefault('parentNodeColor', options);
nodeElement.insert("circle")
.attr("id", "parent-pie")
.attr("r", radius)
.attr("fill", function (d) {
return parentNodeColor;
})
.attr("stroke", function (d) {
return parentNodeColor;
})
.attr("stroke-width", outerStrokeWidth);
}
function drawPieChartBorder(nodeElement, options) {
var radius = getOptionOrDefault('radius', options);
var pieChartBorderColor = getOptionOrDefault('pieChartBorderColor', options);
var pieChartBorderWidth = getOptionOrDefault('pieChartBorderWidth', options);
nodeElement.insert("circle")
.attr("r", radius)
.attr("fill", 'transparent')
.attr("stroke", pieChartBorderColor)
.attr("stroke-width", pieChartBorderWidth);
}
function drawPieChart(nodeElement, percentages, options) {
var radius = getOptionOrDefault('radius', options);
var halfRadius = radius / 2;
var halfCircumference = 2 * Math.PI * halfRadius;
var percentToDraw = 0;
for (var p in percentages) {
percentToDraw += percentages[p].percent;
nodeElement.insert('circle', '#parent-pie + *')
.attr("r", halfRadius)
.attr("fill", 'transparent')
.style('stroke', color(percentages[p].color))
.style('stroke-width', radius)
.style('stroke-dasharray',
halfCircumference * percentToDraw / 100
+ ' '
+ halfCircumference);
}
}
function drawTitleText(nodeElement, options) {
var radius = getOptionOrDefault('radius', options);
var text = getOptionOrDefault('labelText', options);
var color = getOptionOrDefault('labelColor', options);
nodeElement.append("text")
.text(String(text))
.attr("fill", color)
.attr("dy", radius * 2);
}
var NodePieBuilder = {
drawNodePie: function (nodeElement, percentages, options) {
drawParentCircle(nodeElement, options);
if (!percentages) return;
drawPieChart(nodeElement, percentages, options);
var showPieChartBorder = getOptionOrDefault('showPieChartBorder', options);
if (showPieChartBorder) {
drawPieChartBorder(nodeElement, options);
}
var showLabelText = getOptionOrDefault('showLabelText', options);
if (showLabelText) {
drawTitleText(nodeElement, options);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment