力指向グラフを生成しようとしています。'circle / rect'を使用してノードを描画すると、それを実現できます。しかし、代わりに画像を使用したいと思います。私は何が間違っているのですか?
ノードを作成および変換する方法は次のとおりです(d3 v4を使用しています)。
var node = svg.append("g")
.attr("class", "nodes f32")
.selectAll("img")
.data(json.nodes)
.enter().append("img")
.attr("class","flag ar")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
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; });
node
.style("left", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.style("top", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
}
そして、これが私がこれまでに持っているもののデモです:
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
radius=5;
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d,index) { return d.id; }).distance(10))
.force("charge", d3.forceManyBody().distanceMin(10).distanceMax(120))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("https://raw.githubusercontent.com/DealPete/forceDirected/master/countries.json",function(json ) {
json.nodes.forEach(function(d,i){
d.id = i;
})
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(json.links)
.enter().append("line")
.attr("stroke-width","1");
var node = svg.append("g")
.attr("class", "nodes f32")
.selectAll("img")
.data(json.nodes)
.enter().append("img")
.attr("class","flag ar")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
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; });
node
.style("left", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.style("top", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
}
simulation
.nodes(json.nodes)
.on("tick", ticked);
simulation.force("link")
.links(json.links);
});
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;
}
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
h1{
font-family: arial;
}
body{
display:flex;
justify-content:center;
align-items:center;
flex-direction: column;
background:#d64d4d;
}
.fdd{
width:1000px;
height:500px;
background: white;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<h1>Force Directed Graph of State Contiguity</h1>
<div class="fdd">
<svg width="1000" height="500"></svg>
</div>
画像要素を含めるには、の.append("image")
代わりにを使用する必要があります.append("img")
。
また、画像自体をxlink:href
属性で指定する必要があります。たとえば、画像へのリンクを提供できます。
これらの2つのポイントを組み合わせると、次のスニペットが得られます。
.append("image")
.attr("class","flag ar")
.attr("xlink:href", "https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico")
さらに、ノードの場所がずれているようです。あなたはこのように彼らの位置を設定することができます(それらを翻訳することによって):
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
これがデモです:
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
radius=5;
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d,index) { return d.id; }).distance(10))
.force("charge", d3.forceManyBody().distanceMin(10).distanceMax(120))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("https://raw.githubusercontent.com/DealPete/forceDirected/master/countries.json",function(json ) {
json.nodes.forEach(function(d,i){
d.id = i;
})
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(json.links)
.enter().append("line")
.attr("stroke-width","1");
var node = svg.append("g")
.attr("class", "nodes f32")
.selectAll("image")
.data(json.nodes)
.enter().append("image")
.attr("class","flag ar")
.attr("xlink:href", "https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico")
.attr("height", "32") // width/height (are necessary in Firefox to make the image appear)
.attr("width", "32")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
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; });
// -16 is half the width/height of the image I used:
node
.attr("transform", function(d) { return "translate(" + (d.x - 16) + "," + (d.y - 16) + ")"; })
}
simulation
.nodes(json.nodes)
.on("tick", ticked);
simulation.force("link")
.links(json.links);
});
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;
}
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
h1{
font-family: arial;
}
body{
display:flex;
justify-content:center;
align-items:center;
flex-direction: column;
background:#d64d4d;
}
.fdd{
width:1000px;
height:500px;
background: white;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<h1>Force Directed Graph of State Contiguity</h1>
<div class="fdd">
<svg width="1000" height="500"></svg>
</div>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加