在SVG路径的中间显示SVG图像 [英] Display an SVG image at the middle of an SVG path

查看:216
本文介绍了在SVG路径的中间显示SVG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有不同大小节点的力有向图。我想在连接两个节点的每个路径的中间显示自定义图标。从d3的例子我发现了在节点内显示图像的方式。然而,当我尝试在路径上的相同的技术,图像不显示。

I have a force directed graph with different size nodes. I want to display a custom icon in the middle of each path connecting two nodes. From the d3 examples I found the way to display images within the nodes. However, when I try the same technique on the paths, the images are not shown.

var path = svg.append("svg:g").selectAll("path").data(force.links());

var pathEnter = path.enter().append("svg:path");

pathEnter.attr("class", function(d) {
    return "link " + d.target.type;
})

pathEnter.append("svg:g").append("image")
    .attr("xlink:href","http://127.0.0.1:8000/static/styles/images/add.png")
    .attr("x",0).attr("y",0).attr("width",12).attr("height", 12)
    .attr("class", "type-icon");


推荐答案

我想我需要更多的耐心,题。我解决问题的方式是:

I guess I need a bit more patience before asking a question. The way I solved the problem is:

var icon = svg.append("svg:g").selectAll("g")
.data(force.links()).enter().append("svg:g");

icon.append("image").attr("xlink:href","imagePath")
  .attr("x", -20)
  .attr("y", -2)
  .attr("width", 12).attr("height", 12)
        .attr("class", "type-icon");

然后在tick函数中:

And then in the tick function:

        icon.attr("transform", function(d) {
            return "translate(" +((d.target.x+d.source.x)/2) + "," +
                ((d.target.y+d.source.y))/2 + ")";
        });

获得两个节点之间的中心点。

to get the center point between the two nodes.

这篇关于在SVG路径的中间显示SVG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆