2

I am trying to select nodes in my d3 tree on mousemove using a rectangle select box(using this example). It is working fine when the svg is in normal scale . If I increase or decrease the scale value it is not working as expected.

 var translateCoords = d3.transform(d3.select("#svgGroup").attr("transform"));
            translateX = translateCoords.translate[0];
            translateY = translateCoords.translate[1];
            scaleX = translateCoords.scale[0];
            scaleY = translateCoords.scale[1];
 //where svgGroup is the main svg g element, 
         radius is radius of inner nodes

            d3.selectAll( 'g.node-element >circle.inner').each( function( state_data, i) {
                var tCoords = d3.transform(d3.select( this.parentNode).attr("transform"));
                tX = tCoords.translate[0];
                tY = tCoords.translate[1];
                if( 
                    !d3.select( this).classed( "selectedNode") && 
                    tX+translateX*scaleX -radius>=d.x && tX+translateX*scaleX -radius<=parseInt(d.x)+parseInt(d.width)&& 
                    tY+translateY*scaleY-radius>=d.y && tY+translateY*scaleY+radius<=d.y+d.height
                ) {
                    d3.select( this.parentNode)
                    .classed( "selection", true)
                    .classed( "selectedNode", true);
                }
            });

Here is the jsFiddle demo

Jerry
  • 987
  • 4
  • 16
  • 46

1 Answers1

2

I would suggest you to use getScreenCTM to calculate the translated and scaled point.

So rather than doing this heavy calculation of finding the translated + scaled point (which frankly was very tough to understand!):

tX = tCoords.translate[0];
tY = tCoords.translate[1];
// console.log(tX+translateX +":"+d.x)
if (!d3.select(this).classed("selectedNode") &&
    tX + translateX * scaleX >= d.x && tX + translateX * scaleX <= parseInt(d.x) + parseInt(d.width) &&
    tY + translateY * scaleY >= d.y && tY + translateY * scaleY <= d.y + d.height
)

I would suggest you to use getScreenCTM and make your life easy:

var point = svg.node().createSVGPoint(); //create a point
var ctm = d3.select(this.parentNode).node().getScreenCTM(); //get screen ctm of the group
point.matrixTransform(ctm); //apply the transition + scale to the point
tX = point.matrixTransform(ctm).x; // the new translated+scaled point x
tY = point.matrixTransform(ctm).y; // the new translated+scaled point y
//now do your usual comparison to find selection
if (!d3.select(this).classed("selectedNode") &&
    tX >= d.x && tX <= (parseInt(d.x) + parseInt(d.width)) &&
    tY >= d.y && tY <= (parseInt(d.y) + parseInt(d.height))
) {

working code here

Community
  • 1
  • 1
Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55