Last active
April 21, 2020 12:57
-
-
Save afrinc/4a3f3cf6b6ab5eefc3b17ee55dab952a to your computer and use it in GitHub Desktop.
A Composite Node with d3 v5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>A Composite Node with d3 v5</title> | |
<link | |
rel="stylesheet" | |
href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" | |
/> | |
<style> | |
rect.node { | |
width: 150px; | |
height: 80px; | |
rx: 4px; | |
fill: #dceed3; | |
stroke: #fff; | |
stroke-width: 2px; | |
} | |
text { | |
font-family: Roboto; | |
} | |
text.name { | |
font-size: 15px; | |
text-align: center; | |
stroke: #012247; | |
stroke-width: 1px; | |
} | |
text.description { | |
font-size: 12px; | |
text-align: center; | |
stroke: #012247; | |
stroke-width: 1px; | |
} | |
circle.countCircle { | |
fill: #fff; | |
stroke: black; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<script> | |
var width = 500, | |
height = 400; | |
const nodes = [ | |
{ | |
id: 0, | |
name: "ServiceGroup", | |
description: "Port : 80", | |
connection_count: 3 | |
} | |
]; | |
const svg = d3 | |
.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
let circle = svg.append("svg:g").selectAll("g"); | |
circle = circle.data(nodes, d => d.id); | |
const g = circle.enter(); | |
const rectangularNode = g | |
.append("svg:rect") | |
.attr("class", "node") | |
.attr("x", d => { | |
return 0; | |
}) | |
.attr("y", d => { | |
return 0; | |
}); | |
//get dimensions of outer box | |
var outerNodebbox = rectangularNode.node().getBBox(); | |
const images = g | |
.append("svg:image") | |
.attr( | |
"xlink:href", | |
"https://img.icons8.com/ios-glyphs/30/000000/superman.png" | |
) | |
.attr("x", function(d) { | |
let X = outerNodebbox.x; | |
return X + 10; | |
}) | |
.attr("y", function(d) { | |
let Y = outerNodebbox.y; | |
let HEIGHT = outerNodebbox.height; | |
return Y + HEIGHT / 3 ; | |
}); | |
const label = g | |
.append("svg:text") | |
.attr("class", "name") | |
.attr("dx", function(d) { | |
return outerNodebbox.width / 3; | |
}) | |
.attr("dy", function(d) { | |
return outerNodebbox.height / 3; | |
}) | |
.attr("dominant-baseline", "central") | |
.text(d => { | |
return d["name"]; | |
}); | |
const description = g | |
.append("svg:text") | |
.attr("class", "description") | |
.attr("dx", function(d) { | |
return outerNodebbox.width / 3; | |
}) | |
.attr("dy", function(d) { | |
return (2 * outerNodebbox.height) / 3; | |
}) | |
.attr("dominant-baseline", "central") | |
.text(d => { | |
return d["description"]; | |
}); | |
const count_circle = g | |
.append("svg:circle") | |
.attr("class", "countCircle") | |
.style("visibility", "unset") | |
.attr("r", 10) | |
.attr("cx", function(d) { | |
// return descriptionbbox.x+ 10; | |
let X = outerNodebbox.x; | |
let WIDTH = outerNodebbox.width; | |
return X + (2.5 * WIDTH) / 3; | |
}) | |
.attr("cy", function(d) { | |
let Y = outerNodebbox.y; | |
let HEIGHT = outerNodebbox.height; | |
return Y + (2 * HEIGHT) / 3; | |
}); | |
const count_text = g | |
.append("svg:text") | |
.attr("class", "countText") | |
.attr("r", 10) | |
.attr("dx", function(d) { | |
let X = outerNodebbox.x; | |
let WIDTH = outerNodebbox.width; | |
return X + (2.5 * WIDTH) / 3; | |
}) | |
.attr("dy", function(d) { | |
let Y = outerNodebbox.y; | |
let HEIGHT = outerNodebbox.height; | |
return Y + (2 * HEIGHT) / 3; | |
}) | |
.attr("dominant-baseline", "central") | |
.text(d => { | |
return d["connection_count"]; | |
}); | |
circle = g.merge(circle); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment