【问题标题】:changing svg circles to image将 svg 圆圈更改为图像
【发布时间】:2023-03-18 01:36:02
【问题描述】:

我是 d3js 的新手,我目前正在处理一个我坚持的项目。这是我的语法

    var node = svg.selectAll(".node")
                  .data(data.nodes)
                  .enter()
                //.append("circle")
                //.attr("r",5)
                  .append("img")
                  .attr("class", function(d) { return "flag flag-" + d.code; })




    node.style('left', d => d.x + "px")
            .style('top', d => d.y + "px");

   /*
   node.attr("cx", function(d) { return d.x; })
       .attr("cy", function(d) { return d.y; });
 */

基本上,我要做的是将圆圈(我已经将其注释掉)更改为图像。你能帮我弄清楚我错过了什么吗?

【问题讨论】:

    标签: javascript css d3.js svg


    【解决方案1】:

    您可以使用以下代码附加图像并相应地调整“x”和“y”坐标:

    var node = svg.selectAll(".node")
               .data(data.nodes)
               .enter()
               .append('image')
               .attr('xlink:href', 'http://www.charbase.com/images/glyph/9899')
               .attr('width', '50px')
               .attr('height', '50px')
               .attr("class", function(d) { return "flag flag-" + d.code; });
    
    force.on("tick", function() {
    .
    .
    // node.style('left', d => d.x + "px")
    //      .style('top', d => d.y + "px");
    
    node.attr("x", function(d) { return d.x - 25; })
        .attr("y", function(d) { return d.y - 25; });
    .
    .
    });
    

    我已经在https://jsfiddle.net/o81reo7a/更新了你的代码

    【讨论】:

    • 这样我只能附加一张图片。我的目标是添加与节点数一样多的图像。
    • @AvaLucas 我已经更新了我的回复。您必须包括我在回复中提到的 force.on ("tick") 函数的更改。
    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多