Javascript 用图像替换d3.js符号

Javascript 用图像替换d3.js符号,javascript,d3.js,nvd3.js,Javascript,D3.js,Nvd3.js,关于这把小提琴: 我需要将符号替换为图像…或者首先可能是单个图像..例如,此图像: https://github.com/favicon.ico 我在代码中尝试的内容如下: vis.selectAll("path") .data(nodes) .enter().append("path") .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) .app

关于这把小提琴:

我需要将符号替换为图像…或者首先可能是单个图像..例如,此图像:

https://github.com/favicon.ico
我在代码中尝试的内容如下:

vis.selectAll("path")
     .data(nodes)
   .enter().append("path")
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
     .append("image")
   .attr("xlink:href", "https://github.com/favicon.ico")
       .attr("x", -8)
       .attr("y", -8)
       .attr("width", 16)
       .attr("height", 16)
       .style("stroke", "black")
       .style("stroke-width", "1px")
       .call(force.drag);

它将
图像
标记附加到每个
路径
标记,但不显示图像本身。有什么提示吗?

您正在将每个图像作为每个路径的子级追加。您可能希望附加一个包含路径和图像的组(我想这是您在第一条评论中提出的问题)

大概是这样的:

var groups = vis.selectAll("g")
    .data(nodes).enter()
    .append("g")
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
    .call(force.drag);

groups.append("path")
    .attr("d", d3.svg.symbol()
        .size(function(d) { return d.size; })
        .type(function(d) { return d.type; }))
    .style("fill", "steelblue")
    .style("stroke", "black")
    .style("stroke-width", "1.5px");

groups.append("image")
   .attr("xlink:href", "https://github.com/favicon.ico")
       .attr("x", -8)
       .attr("y", -8)
       .attr("width", 16)
       .attr("height", 16)
       .style("stroke", "black")
       .style("stroke-width", "1px");

请参阅:

您将每个图像作为每个路径的子对象附加。您可能希望附加一个包含路径和图像的组(我想这是您在第一条评论中提出的问题)

大概是这样的:

var groups = vis.selectAll("g")
    .data(nodes).enter()
    .append("g")
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
    .call(force.drag);

groups.append("path")
    .attr("d", d3.svg.symbol()
        .size(function(d) { return d.size; })
        .type(function(d) { return d.type; }))
    .style("fill", "steelblue")
    .style("stroke", "black")
    .style("stroke-width", "1.5px");

groups.append("image")
   .attr("xlink:href", "https://github.com/favicon.ico")
       .attr("x", -8)
       .attr("y", -8)
       .attr("width", 16)
       .attr("height", 16)
       .style("stroke", "black")
       .style("stroke-width", "1px");

请参阅:

我是否需要附加(“g”)元素而不是“image”?这是您的意思吗?是的。这是最接近的示例,实际上节点被名称替换。在我的JSFIDLE示例中,我将节点作为符号强制输入。我需要用图像替换它们。请看我在问题顶部添加的fiddle链接。我不太清楚“节点被名称替换”是什么意思。在我发布的链接中,节点是图像。你想和那个例子有什么不同?那是一个打字错误。它是“节点被图像替换”。我的例子非常相似,但是如果你在我添加的小提琴上看到,我正在尝试与你提到的例子类似的东西。但不知怎的,它不起作用。我是否需要附加(“g”)元素而不是“image”?你的意思是什么?是的。这是最接近的例子,实际上节点被名称替换。在我的JSFIDLE示例中,我将节点作为符号强制输入。我需要用图像替换它们。请看我在问题顶部添加的fiddle链接。我不太清楚“节点被名称替换”是什么意思。在我发布的链接中,节点是图像。你想和那个例子有什么不同?那是一个打字错误。它是“节点被图像替换”。我的例子非常相似,但是如果你在我添加的小提琴上看到,我正在尝试与你提到的例子类似的东西。但不知怎的,它不起作用了……谢谢你,@findango。这很有帮助。谢谢:-)谢谢你,芬丹戈。这很有帮助。谢谢:-)