【问题标题】:d3js force layout dynamically adding image or circle for each noded3js 强制布局为每个节点动态添加图像或圆圈
【发布时间】:2012-10-04 14:39:17
【问题描述】:

我是 D3JS 新手,我需要为每个节点创建包含图像或圆形的强制布局。 这意味着,动态添加图像节点或圆形节点。 这可能吗?如果有例子请回答

【问题讨论】:

    标签: javascript svg d3.js force-layout


    【解决方案1】:

    对于动态图像,您可以将图像保存在本地,并使用图像名称可以使用本地的动态图像。

    制作圆圈,你可以使用:

     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);
    

    这里,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)
    

    如果要在圆圈内添加文字,可以使用:

    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;
            });
    

    【讨论】:

      【解决方案2】:

      如果你只想在圆圈中间放一张图片,试试这个:

      /* 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);
      

      您还可以附加标题、一些文本...有关更多信息,请参阅 selection.append(name) 的 documentation。

      【讨论】:

      • 谢谢克里斯·詹姆斯。可以用文字圈和图片圈吗?
      • 是的,它只是加起来。了解如何做的最好方法是浏览 www.d3js.org 上的力图示例
      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2013-11-15
      • 2015-12-04
      • 2012-02-23
      • 1970-01-01
      相关资源
      最近更新 更多