Kao*_*Yui 1 javascript svg d3.js
我正在尝试使用 d3.js 旋转带有内容的 svg 元素,但它总是从 viewbox 中出来,我想要实现的是将 svg 旋转到位,然后我可以将 svg 旋转到0,90,180,270deg但现在我的目标是旋转它到位。
我的代码如下(我删除了不相关的代码)
var svgContainer = d3.select("body").append("svg")
.attr("width",121)
.attr("height", 108)
//below is my code to rotate the svg
attr('transform', 'rotate(180 0 0)')
Run Code Online (Sandbox Code Playgroud)
我原来的svg
放置时我的输出 attr('transform', 'rotate(180 0 0)')
我想要达到的目标
我的小提琴
您需要围绕其中心旋转 svg,而不是 0,0。
所以首先你需要像这样向 svg 添加一个组:
var svgContainer = d3.select("body").append("svg")
.attr("width", 121 + "mm")
.attr("height", 108 + "mm")
.attr('id','board')
.style('background','#f4f4f4')
.style("border", "1px solid black").append("g");//add agroup
Run Code Online (Sandbox Code Playgroud)
向该组添加您的所有数据点。
接下来为了旋转,我们需要找到我们需要旋转的中心。
svgContainer.attr('transform',function(){
var me = svgContainer.node()
var x1 = me.getBBox().x + me.getBBox().width/2;//the center x about which you want to rotate
var y1 = me.getBBox().y + me.getBBox().height/2;//the center y about which you want to rotate
return `rotate(180, ${x1}, ${y1})`;//rotate 180 degrees about x and y
});
Run Code Online (Sandbox Code Playgroud)
工作代码在这里
| 归档时间: |
|
| 查看次数: |
4851 次 |
| 最近记录: |