简体   繁体   English

所有圆形元素都相互粘在一起

[英]All circle elements stuck behind each other

I'm trying to append circles that have a color background with an image attached. 我正在尝试添加带有附加图像的彩色背景的圆圈。

To achieve that am using <defs> , <rect> <clipPath> and <use> . 要实现这一点,请使用<defs><rect> <clipPath><use> I believe that my SVG hierarchy is valid, however even though all elements have a unique ID all circles are got stuck in the same point. 我相信我的SVG层次结构是有效的,但即使所有元素都有唯一的ID,所有的圆圈都会卡在同一个点上。 All <a> elements itself that contain defs in it are having different x and y , but rects inside it are having same x and y . 所有包含defs的<a>元素本身都有不同的xy ,但其中的rects具有相同的xy

How is it possible that all rects having a unique ID having same x's and y's. 如何拥有具有相同x和y的唯一ID的所有rects。

Codepen Codepen

DOM screenshot: DOM截图:

在此输入图像描述

 let personCircles = svg.selectAll("a")
            .data(data)
            .enter()
            .append("a")
            .attr("id", function(d) {
                console.log(d["Person Name"]);
                if (d && d.length !== 0) {
                    return d["Person Name"].replace(/ |,|\./g, '_');
                }
            })
            .attr('x', function(d) {
                    return markerCirclesScale(name)
                })
                .attr('y', function(d) {
                    return fullSVGHeight / 2 + 8;
                })
            .style("opacity", 1)
            .call(d3.drag()
                .on("start", dragstarted)
                .on("drag", dragged)
                .on("end", dragended));




        //Define defs 
        let defs = personCircles.append("defs");

        defs.append('rect')
            .attr('id', function(d) {
                    return "rect-" + d["Person Name"].replace(/ |,|\./g, '_');
            })
            .attr('x', function(d) {
                return markerCirclesScale(name)
            })
            .attr('y', function(d) {
                return fullSVGHeight / 2;
            })
            .attr('width', 60)
            .attr('height', 60)
            .attr('rx', 40)
            .style('fill', 'red')


        defs.append("clipPath")
           .attr('id', function(d) {
                    return "clip-" + d["Person Name"].replace(/ |,|\./g, '_');
            })
            .append("use")
            .attr('href', function(d) {
                    return "#rect-" + d["Person Name"].replace(/ |,|\./g, '_');
            })

         personCircles
                .append("use")
                .attr('href', function(d) {
                    return "#rect-" + d["Person Name"].replace(/ |,|\./g, '_');
            })
            personCircles.append('image')
                .attr('href', function(d) {
                    return 'http://pngimg.com/uploads/donald_trump/donald_trump_PNG72.png'
                })
                .attr("clip-path", function(d) {
                    return "url(#clip-" + d["Person Name"].replace(/ |,|\./g, '_');+")"
                })
                .attr('x', function(d) {
                    return markerCirclesScale(name)
                })
                .attr('y', function(d) {
                    return fullSVGHeight / 2 + 8;
                })
                .attr("width", 60)
                .attr("height", 60)

personCircles refers to the <a> ( anchor ) elements which wouldn't move an inch if you set x and y co-ordinates within a SVG. personCircles指的是<a>锚点 )元素,如果在SVG中设置xy坐标,它们不会移动一英寸。 The elements you're trying to position are the rect s and the corresponding image s and so changing the ticked function to the following ie positioning the rect s, clipPath rect s and the image : 您尝试定位的元素是rect和相应的image ,因此将ticked函数更改为以下内容,即定位rect s,clipPath rectimage

function ticked() {
    personCircles.selectAll('rect, image') 
        .attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; });
}

the result would be as seen in the following fork of your codepen: 结果将在您的codepen的以下fork中看到:

https://codepen.io/anon/pen/aPOdON?editors=1010 https://codepen.io/anon/pen/aPOdON?editors=1010

Hope this clears up. 希望这清除。 Btw I like the sample image you're using in your testing :P 顺便说一下,我喜欢您在测试中使用的样本图像:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM