简体   繁体   English

如何在SV中移动SVG的位置?

[英]How to move SVG's position in D3?

I created a svg using D3. 我用D3创建了一个svg。 However, it only shows up on the upper left conner of the screen, or been appended to another svg. 但是,它只显示在屏幕的左上角,或者被附加到另一个svg。 Are there anyway I can move it using JavaScript? 无论如何我可以使用JavaScript移动它吗? For example: 例如:

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

Using d3js + Jquery : 使用d3js + Jquery:

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

// svg repositioning
$("svg").css({top: 200, left: 200, position:'absolute'});

Or 要么

// svg align center
d3.select("#chart").attr("align","center"); //  thanks & +1 to chaitanya89

Live demo 现场演示

Instead of appending SVG to the body, append it to a html element like <div> and add style to it. 不是将SVG附加到正文,而是将其附加到像<div>这样的html元素,并为其添加样式。

Javascript: 使用Javascript:

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

HTML: add this to your body tag. HTML:将其添加到您的body标签中。

<div id="chart" align="center"></div>

If you want to align svg using javascript, remove align attribute in the above <div> tag and add the following in your javascript. 如果你想使用javascript对齐svg,请删除上面<div>标记中的align属性,并在你的javascript中添加以下内容。

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

Alternatively, You could also do it using D3. 或者,你也可以使用D3。

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

Before you need append any SVG object to apply the transition on canvas. 在您需要附加任何SVG对象以在画布上应用转换之前。

The tutorial step-by-step below show you, in practice, each property of method Transition from D3js. 下面的教程将逐步向您展示D3js方法Transition的每个属性。

http://blog.visual.ly/creating-animations-and-transitions-with-d3-js/ http://blog.visual.ly/creating-animations-and-transitions-with-d3-js/

Enjoy! 请享用!

Use negative value in margin. 在保证金中使用负值。

margin = { top: 20, right: -600, bottom: 20, left: 640 } margin = {top:20,right:-600,bottom:20,left:640}

The content is added on the left by default. 默认情况下,内容会添加到左侧。 To shift to right, use negative margins. 要转移到右边,请使用负边距。 following line will take it to the second right half of the screen. 以下行将把它带到屏幕的第二个右半边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM