d3js leaf node images
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
<svg id="mySvg" width="80" height="80">
<defs id="mdef">
<pattern id="image" x="-100" y="-100" height="200" width="200" patternUnits="userSpaceOnUse" >
<image x="50" y="5" width="40" height="40" xlink:href="http://www.e-pint.com/epint.jpg"></image>
</pattern>
</defs>
</svg>
CSS
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
JavaScript
var width = 960,
height = 2200;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,40)");
d3.json("https://rawgit.com/bansaghi/d3.chart.layout.hierarchy/master/example/data/flare.json", function(error, root) {
var nodes = cluster.nodes(root),
links = cluster.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
//.attr("class", "node")
.attr("class", function(n) {
return n.children ? "inner node" : "leaf node";
})
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
})
node.append("circle")
.attr("r", 4.5);
//.style("fill", "black");
var imgs = ["https://i.pinimg.com/originals/0e/c9/d6/0ec9d63f901ec251291eaa68f0da4d6c.jpg"];
var leafnodes = svg.selectAll('g.leaf.node')
.append("image")
.attr("xlink:href", function(d) {
// get random image from array
return imgs[Math.floor(Math.random() * imgs.length)];
})
.attr("width", 100)
.attr("height", 400);
node.append("text")
.attr("dx", function(d) {
return d.children ? -8 : 8;
})
.attr("dy", 3)
.style("text-anchor", function(d) {
return d.children ? "end" : "start";
})
.text(function(d) {
return d.name;
});
});
d3.select(self.frameElement).style("height", height + "px");
console.log(d3);