【问题标题】:Zoom only zooms my axis and not the objects on my graph缩放只缩放我的轴,而不是我的图表上的对象
【发布时间】:2014-03-26 14:23:47
【问题描述】:

我有一个我刚刚学会在 D3 中使用的当前缩放功能。但是,当我使用它时,它只会移动我的并缩放图形的轴,而不是它上面的对象。

我对 D3 非常了解,希望得到一些帮助。

我的javascript源代码贴在下面:

    //Setting generic width and height values for our SVG.
    var margin = {top: 60, right: 0, bottom: 60, left: 40},
        width = 1024 - 70 - margin.left - margin.right,
        height = 668 - margin.top - margin.bottom;

     //Other variable declarations.


    //Creating scales used to scale everything to the size of the SVG.
    var xScale = d3.scale.linear()
        .domain([0, 1024])
        .range([0, width]);

    var yScale = d3.scale.linear()
        .domain([1, 768])
        .range([height, 0]);

    //Creates an xAxis variable that can be used in our SVG.
    var xAxis = d3.svg.axis()
        .scale(xScale)
        .orient("bottom");

    var yAxis = d3.svg.axis()
        .scale(yScale)
        .orient("left");

    //Zoom command ...
    var zoom = d3.behavior.zoom()
        .x(xScale)
        .y(yScale)
        .scaleExtent([1, 10])
        .on("zoom", zoomed);


    // The mark '#' indicates an ID. IF '#' isn't included argument expected is a tag such as "svg" or "p" etc..
    var SVG = d3.select("#mainSVG")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
                .append("g")
                .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
                .call(zoom);


    //Create background. The mouse must be over an object on the graph for the zoom to work. The rectangle will cover the entire graph.
    var rect = SVG.append("rect")
        .attr("width", width)
        .attr("height", height);

    //This selects 4 circles (non-existent, there requires data-binding) and appends them all below enter.
    //The amount of numbers in data is the amount of circles to be appended in the enter() section. 
    var circle = SVG
        .selectAll("circle")
        .data([40,100,400,1900])
        .enter()
            .append("circle")
            .attr("cx",function(d){return xScale(d)})
            .attr("cy",function(d){return xScale(d)})
            .attr("r",20);

    //This appends a circles to our SVG.
    var circle = SVG
        .append("circle")
        .attr("cx",function(d){ return xScale(d)})
        .attr("cy",300)
        .attr("r",20);



    //Showing the axis that we created earlier in the script for both X and Y.
    var xAxisGroup = SVG.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    var yAxisGroup = SVG.append("g")
        .attr("class", "y axis")
        .call(yAxis);

    function zoomed() {
       SVG.select(".x.axis").call(xAxis);
       SVG.select(".y.axis").call(yAxis);
    }

【问题讨论】:

    标签: javascript graph d3.js zooming axis


    【解决方案1】:

    您还需要在缩放时使用更改的轴重新绘制所有元素——D3 不会自动为您执行此操作:

    function zoomed() {
       SVG.select(".x.axis").call(xAxis);
       SVG.select(".y.axis").call(yAxis);
    
       SVG.selectAll("circle")
          .attr("cx",function(d){return xScale(d)})
          .attr("cy",function(d){return xScale(d)});
    }
    

    【讨论】:

    • 您好,感谢您的回复。虽然我这样做时仍然存在问题。当我加载页面时,图表开始看起来像这样:puu.sh/7Ke5U.png。当我尝试进行任何缩放操作时,它会改变圆圈的坐标并将它们放置如下:puu.sh/7Ke6G.png 就像它从 0 开始是左上角,当你缩放它时,它会改变它以匹配图形从左下角的 0 开始,而不是顶部。你知道为什么它会这样解释吗?
    • 那是因为您对 x 和 y 坐标都使用了 x 比例。工作版here。跳跃的圆圈是你没有绑定任何数据的圆圈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多