How to get coordinates of an svg element?

前端 未结 7 2039

I am using d3 to draw a line from a relative svg position and hence want to access the coordinates of the element itself. I tried something like this (where \"this\" refers

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 12:04

    i can handle it like that ;

    svg.selectAll("rect")
        .data(zones)
        .enter()
        .append("rect")
        .attr("id", function (d) { return "zone" + d.zone; })
        .attr("class", "zone")
        .attr("x", function (d, i) {
            if (parseInt(i / (wcount)) % 2 == 0) {
                this.xcor = (i % wcount) * zoneW;
            }
            else {
                this.xcor = (zoneW * (wcount - 1)) - ((i % wcount) * zoneW);
            }
    
            return this.xcor;
        })
    

    and anymore you can find x coordinate

    svg.select("#zone1").on("click",function(){alert(this.xcor});
    

提交回复
热议问题