How to move SVG's position in D3?

前端 未结 4 2539
孤城傲影
孤城傲影 2021-02-20 07:35

I created a svg using D3. However, it only shows up on the upper left conner of the screen, or been appended to another svg. Are there anyway I can move it using JavaScript? For

4条回答
  •  孤独总比滥情好
    2021-02-20 08:22

    Instead of appending SVG to the body, append it to a html element like

    and add style to it.

    Javascript:

    var svg = d3.select("#chart").append("svg")
    .attr("width", 200)
    .attr("height", 200);
    

    HTML: add this to your body tag.

    If you want to align svg using javascript, remove align attribute in the above

    tag and add the following in your javascript.

    document.getElementById("chart").align = "center";
    

    Alternatively, You could also do it using D3.

    d3.select("#chart")
    .attr("align","center");
    

提交回复
热议问题