javascript - Changing Element Position on Page JSD3 - Stack Overflow

This is a simple question that I just can't seem to get figured out -- All I want to do is change

This is a simple question that I just can't seem to get figured out -- All I want to do is change the position of an element on a page. I've tried the following:

// Text
body = d3.select('body')
sometext = body.append('text').text('testing') // text appears

// Different Ways i've tried to move it
.attr('cx', 40)
.attr('x', 40)
.attr("transform", "translate("40, 100")

However, none of this moves the text -- What am I missing (/)? I'm having the same issue moving svg elements around a page, i just thought this was easier to see on the jsfiddle. Thanks for help with a basic question,

This is a simple question that I just can't seem to get figured out -- All I want to do is change the position of an element on a page. I've tried the following:

// Text
body = d3.select('body')
sometext = body.append('text').text('testing') // text appears

// Different Ways i've tried to move it
.attr('cx', 40)
.attr('x', 40)
.attr("transform", "translate("40, 100")

However, none of this moves the text -- What am I missing (http://jsfiddle/Hn5JX/)? I'm having the same issue moving svg elements around a page, i just thought this was easier to see on the jsfiddle. Thanks for help with a basic question,

Share Improve this question asked Jun 14, 2012 at 5:45 mikemike 23.8k32 gold badges81 silver badges100 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Here's an example of moved text using d3js. http://jsfiddle/JnNtZ/

Make sure to include:

"http://mbostock.github./d3/d3.js"

Then include the following javascript

// Create an svg "canvas" to place text on
var w = 400, h = 400;    
var vis = d3.select("body")
  .append("svg")
    .attr("width", w)
    .attr("height", h);

// Add text and set attributes
var text1 = vis.append('text').text('text1');
var text2 = vis.append('text').text('text2');
text1.attr("y", "300").style("fill", "red");
text2.attr("x", "200").attr("y", "403").style("fill", "blue");  // Notice how this text is on the edge of the canvas

​ I think the problem you were running into is that upon selecting text, you're getting an HTML element instead of an SVGText element (It seems you need an SVG element to allow manipulations by d3js). You also need to make sure that the (x,y) coordinates of the text fall inside the given canvas.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744294143a4567177.html

相关推荐

  • javascript - Changing Element Position on Page JSD3 - Stack Overflow

    This is a simple question that I just can't seem to get figured out -- All I want to do is change

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信