简体   繁体   English

d3js强制布局为每个节点动态添加图像或圆

[英]d3js force layout dynamically adding image or circle for each node

I am new to D3JS, I need to create force layout with both image or circle for each node. 我是D3JS的新手,我需要为每个节点创建包含图像或圆形的力布局。 that means, A Image node or Circle node be added dynamically. 这意味着,动态添加A Image节点或Circle节点。 Is this possible?, if any examples please answer 这可能吗?,如果有任何例子请回答

Well if you just want to have an image in the middle of the circle try this: 好吧,如果你只是想在圆圈中间有一个图像,试试这个:

/* Create nodes */
var node = vis.selectAll("g.node")
    .data(json.nodes) // get the data how you want
    .enter().append("svg:g")
    .call(node_drag);

/* append circle to node */
node.append("svg:circle")
    .attr("cursor","pointer")
    .style("fill","#c6dbef")
    .attr("r", "10px"})

/* append image to node */
node.append("image")
    .attr("xlink:href", "https://github.com/favicon.ico")
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 16)
    .attr("height", 16);

You can also append a title, some text... See the documentation of selection.append(name) for more info. 您还可以附加标题,一些文本...有关详细信息,请参阅selection.append(name)文档

For Dynamic Image, you can keep the image in local and using image name you can use the dynamic image from the local. 对于动态图像,您可以将图像保存在本地,使用图像名称可以使用本地的动态图像。

For making circle, you can use : 对于制作圈子,您可以使用:

 var groups = node.enter().append("g")
        .attr("class", "node")
        .attr("id", function (d) {
            return d.entityType;
        })
        .on('click', click)

    groups.append("circle")
       .attr("cursor", "pointer")
       .style("fill", function(d) { return color(d.entityType); })
       .style("fill", "#fff")
       .style("stroke-width", "0")
       .style("stroke", "#ddd")
     .attr("r", 20);

Here, d.entityType will be the name of the image example : favicon.png 这里, d.entityType将是图像示例的名称: favicon.png

groups.append("image")
       .attr("xlink:href",function (d) {
            return "../assets/images/"+d.entityType+".png";
            })
        .attr("x", -14)
        .attr("y", -15)
        .attr("width", 30)
        .attr("height", 30)

If you want to add text inside the circle, you can use : 如果要在圆圈内添加文本,可以使用:

groups.append("text")
        .attr("dy", 18)
        .style("font-size", "2.5px")
        .style("text-anchor", "middle")
        .style('fill','#000')
        .attr("refX", 15)
        .attr("refY", -1.5)
        .text(function (d) {
            return d.entityName;
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM