我正在使用Cluster Dendogram。 我想根据我的聚类结果将图像插入到树形图的叶子中。 我怎么能这样做?
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<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="10" height="10" xlink:href="http://www.e-pint.com/epint.jpg"></image>
</pattern>
</defs>
</svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
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,0)");
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("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
//var circle = svg.append("circle")
// .style("fill", "red")
// .style("fill", "url(#image)");
node.append("circle")
.attr("r", 4.5);
//.style("fill", "black");e
node.append("image")
.attr("xlink:href", "http://www.e-pint.com/epint.jpg")
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);
</script>
在此代码中,我添加了样式部分和node.append("image")
.attr("xlink:href", "http://www.e-pint.com/epint.jpg")
。
问题是我看不到圆圈充满图像。什么可能是错的?
答案 0 :(得分:2)
选中此JSFiddle
这是向图片添加图片的代码:
var leafnodes = svg.selectAll('g.leaf.node')
.append("image")
.attr("xlink:href", "http://www.e-pint.com/epint.jpg")
.attr("width", 10)
.attr("height", 10);
在此之前,叶节点标有leaf
类,如下所示:
.attr("class", function(n) {
return n.children ? "inner node" : "leaf node";
})
更新:
要向节点添加不同的图像,您可以执行以下操作:
var imgs = ['http://***.jpg',
'http://***.jpg',
'http://***.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", 10)
.attr("height", 10);
检查上面的JSFiddle以获取完整示例。
答案 1 :(得分:2)
您可以使用svg clipPath
用图片填充圆圈。
示例代码:
var imgRadius = 10 - 1.5; //Where 10 is the radius of node and 1.5 is the stroke width
svg.append("defs")
.append("clipPath")
.attr('id', "circleCip")
.append("circle")
.attr("r", imgRadius);
node.append("svg:image")
.attr("xlink:href", "http://www.e-pint.com/epint.jpg")
.attr("clip-path", "url(#circleCip)")
.attr("x", function(d) {
return -imgRadius;
})
.attr("y", function(d) {
return -imgRadius;
})
.attr("width", function(d) {
return imgRadius * 2;
})
.attr("height", function(d) {
return imgRadius * 2;
});
<强>演示强>
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);
svg.append("g")
.attr("transform", "translate(40,0)");
var imgRadius = 10 - 1.5; //10 is the radius of node and 1.5 is the stroke width
svg.append("defs")
.append("clipPath")
.attr('id', "circleCip")
.append("circle")
.attr("r", imgRadius);
var flare = {
"name": "flare",
"children": [{
"name": "analytics",
"children": [{
"name": "cluster",
"children": [{
"name": "AgglomerativeCluster",
"size": 3938
}, {
"name": "CommunityStructure",
"size": 3812
}, {
"name": "HierarchicalCluster",
"size": 6714
}]
}, {
"name": "graph",
"children": [{
"name": "BetweennessCentrality",
"size": 3534
}, {
"name": "LinkDistance",
"size": 5731
}]
}]
}, {
"name": "animate",
"children": [{
"name": "Easing",
"size": 17010
}, {
"name": "FunctionSequence",
"size": 5842
}, {
"name": "Transitioner",
"size": 19975
}, {
"name": "TransitionEvent",
"size": 1116
}, {
"name": "Tween",
"size": 6006
}]
}]
};
root = flare;
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("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
})
node.append("circle")
.attr("r", 10);
node.append("svg:image")
.attr("xlink:href", "http://www.e-pint.com/epint.jpg")
.attr("clip-path", "url(#circleCip)")
.attr("x", function(d) {
return -imgRadius;
})
.attr("y", function(d) {
return -imgRadius;
})
.attr("width", function(d) {
return imgRadius * 2;
})
.attr("height", function(d) {
return imgRadius * 2;
});
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);
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>