D3:改变节点样式

首先在<body>添加图片对象

       
       <svg id="mySvg_ac" width="0" height="0" >
        <defs id="mdef">
            <!-- 图片1  -->
            <pattern id="img_ac" x="0" y="0" height="128" width="128">
            <image x="0" y="0" width="128" height="128" xlink:href="/static/images/ac.png"></image>
            </pattern>

            <!-- 图片2  -->
            <pattern id="img_de" x="0" y="0" height="128" width="128">
            <image x="0" y="0" width="128" height="128" xlink:href="/static/images/de.png"></image>
            </pattern>
        </defs>
       </svg>

然后在需要的节点中引用图片对象

        var nodes = svg.selectAll("circle")
            .data(nodes_data)
            .enter()
            .append("circle")
            .attr("fill", function(d){
                if(d.type == 'ac'){
                    return "url(#img_ac)";
                }else{
                    return "url(#img_de)";
                }})

 

posted @ 2022-12-15 09:26  徐钏  阅读(229)  评论(0)    收藏  举报