Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two code blocks in d3.js

I had thought that the d3.js append function returns the object appended to a selection, but I find the following two code blocks give different results:

var svg = d3.select("body")
    .append("svg")
        .attr("width", fig_width)
        .attr("height", fig_height);

    svg.append("g")
        .attr("class", "graph")
        .attr("transform", 
              "translate(" + graph_margin.left + "," + graph_margin.top + ")");

Which does not seem to translate the graph group, offsetting it by a left and top margin and:

var svg = d3.select("body")
    .append("svg")
        .attr("width", fig_width)
        .attr("height", fig_height)
    .append("g")
        .attr("class", "graph")
        .attr("transform", 
              "translate(" + graph_margin.left + "," + graph_margin.top + ")");

which does.

What don't I understand about the way this works in SVG / d3.js?

like image 215
xnx Avatar asked Nov 01 '22 06:11

xnx


1 Answers

Both code blocks should give exactly the same result.

I'm guessing that you're using svg to append additional elements to -- keep in mind that in the first case, svg holds the SVG element and in the second the translated g element. So anything you append to svg in the first case wouldn't be translated (because the new elements are not contained in the g element).

like image 90
Lars Kotthoff Avatar answered Nov 08 '22 07:11

Lars Kotthoff