Ste*_*see 8 svg transform d3.js
随着D3.js我要改造x一个的group多次(例如,通过点击一个按钮引起对每次点击的变换),从该组的当前位置开始.问题是我无法检索组中的x,即使我找到它,我也找不到任何内置函数,可以让我x从当前开始更改组x.我错过了重要的事吗?很奇怪我无法将x一个元素添加到SVG"舞台"中.
我的代码如下(为Lars编辑):我按照你昨天建议我的方式创建节点(克隆它们).
var m=[5,10,15,20,25,30];
var count=0;
d3.select('svg').on("mouseup", function(d, i){
count+=0.5;
var n = d3.selectAll(".myGroup");
n.each(function(d,i) {
if(i!=0){
// here I need to know the current x of the current n element but I
// can't get it
// this causes error "Uncaught TypeError: undefined is not a function"
// var currentx = d3.transform(this.attr("transform")).translate[0];
this.setAttribute("transform", "translate("+((100/m[i])*count)+",10)");
}
});
});
Run Code Online (Sandbox Code Playgroud)
Lar*_*off 18
获取和更改g元素的位置相对简单.例如这样:
var g = d3.select("#myG");
// get x position
var currentx = d3.transform(g.attr("transform")).translate[0];
// set x position
g.attr("transform", "translate(" + (currentx + 100) + ",0)");
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您有一个原始DOM元素,请先使用D3选择它:
var currentx = d3.transform(d3.select(this).attr("transform")).translate[0];
Run Code Online (Sandbox Code Playgroud)