How do I adjust zoom size for a point in D3?

前端 未结 2 2040
说谎
说谎 2020-12-31 20:29

This could be a classic case of \"you\'re doing it wrong\", but all of my searching to date hasn\'t warranted any help.

Here\'s my scenario:

I\'m using an al

相关标签:
2条回答
  • 2020-12-31 21:12

    For things you don't want to scale, just make them divided by 'scale' . In my case,

    var zoom = d3.behavior.zoom()
        .on("zoom",function() {
            g.attr("transform","translate("+d3.event.translate.join(",")+")scale("+d3.event.scale+")");
    
            g.selectAll(".mapmarker")  
            .attr("r",6/d3.event.scale)
            .attr("stroke-width",1/d3.event.scale);
    
    });
    
    0 讨论(0)
  • 2020-12-31 21:14

    This is happening because you are setting a scale transform instead of scaling the positions. You can see the difference here Basically it is the difference between:

    // Thick lines because they are scaled too
    var bottom = svg.append('g').attr('transform', 'scale('+scale+','+scale+')');
    bottom.selectAll('circle')
        .data(data)
        .enter().append('circle')
        .attr('cx', function(d) { return d.x; })
        .attr('cy', function(d) { return d.y; });
    

    and

    // line thicknesses are nice and thin
    var top = svg.append('g');
    top.selectAll('circle')
        .data(data)
        .enter().append('circle')
        .attr('cx', function(d) { return d.x * scale; })
        .attr('cy', function(d) { return d.y * scale; });
    

    With mapping probably you best solution is to compute your offset and scale as you do and then add them into your projection function - you want to directly modify the post-projection x and y values. If you update your projection function properly you should not have to do anything else to apply the appropriate zoom to your map.

    0 讨论(0)
提交回复
热议问题