简体   繁体   English

d3 svg如何将多个圆圈居中

[英]d3 svg how to center multiple circles

i have a json file: 我有一个json文件:

{
    "circles": [
        {"name": "Circle", "x":13.284199,"y":-12.223403, "group":1}, 
        {"name": "Circle", "x":13.277986,"y":-30.081020, "group":0}, 
        {"name": "Circle", "x":37.270324,"y":-46.266050, "group":0}, 
        {"name": "Circle", "x":49.085207,"y":-50.323903, "group":1}, 
        {"name": "Circle", "x":17.334599,"y":-76.113029, "group":1}, 
        {"name": "Circle", "x":40.067553,"y":-76.576700, "group":0}, 
        {"name": "Circle", "x":11.616747,"y":-107.550639, "group":0}, 
        {"name": "Circle", "x":10.827147,"y":-134.892190, "group":0}, 
        {"name": "Circle", "x":18.657633,"y":-171.319351, "group":0}, 
        {"name": "Circle", "x":50.023818,"y":-182.810422, "group":0}
    ]
}

And i am drawing circles and positioning them based on those x and y coordinates. 我正在绘制圆并根据这些x和y坐标定位它们。 So far i have tried to set the svg element's transform="translate()" to the x and y coordinates of the first circle, but that wasn't successfull. 到目前为止,我已经尝试将svg元素的transform="translate()"设置为第一个圆的x和y坐标,但这并不成功。

在此处输入图片说明

If the image is what you want, youll have to move the element the circles are appended to. 如果图像是您想要的图像,则必须移动圆附加到的元素。 Like so: 像这样:

svg.attr("transform",function(){
                movementX = windowWidth/2 - svgWidth /2;
                movementY = windowHeight/2 - svgHeight /2;
                return "translate("+movementX+","+movementY +")";
                }
                );

That's if svg is of type SVG. 就是说svg是SVG类型。 If not youll have to use offset. 如果不是,则必须使用偏移量。

$(svg).offset({top : movementY, left: movementX});

This will center the svg to the center of the screen. 这会将svg居中到屏幕中心。 I cant answer it 100% as ill need to see your code and what elements you have used, but the basics are there and should be easily understood. 我无法百分百地回答它,因为您急需查看您的代码以及使用了哪些元素,但是基础知识已经存在,应该很容易理解。

EDIT 编辑

I just realized this wasn't resolved. 我只是意识到这还没有解决。

I added an ID to the <g> tag, ie the container of the circles and added the following code. 我向<g>标签添加了一个ID,即圆圈的容器,并添加了以下代码。 I have commented to explain :) 我评论解释了:)

 var circleContainer = d3.select('#circleContainer') //get circle container var circleContainerBBox = getBoundingBox('#circleContainer'); //get bounding box of circle container to get x,y,width and height var svgBBox = getBoundingBox('#svg'); //same for svg to find offset var differenceX = circleContainerBBox.x - svgBBox.x - (circleContainerBBox.width / 2); //difference between svg x and circleContainer x take away half the width of the circle container as it draws from top left var differenceY = circleContainerBBox.y - svgBBox.y - (circleContainerBBox.height / 2); //same for y direction var width = 800, //set width height = 400; //and height circleContainer.attr('transform', function(d) { return "translate(" + ((width / 2) + differenceX) + ',' + ((height / 2) + differenceY) + ')'; //here's were you transform the circle container based on the values calculated above }) function getBoundingBox(element) { //function to get bbox var thisEl = document.querySelector(element) var thisElBBox = thisEl.getBBox(); console.log(thisElBBox) return thisElBBox; } 
 svg { width: 800px; height: 400px; border: 1px solid #00667A; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <svg id='svg'> <!--Make a javascript script, that sets a transform to the g to center the circles.--> <g id='circleContainer'> <!-- Transform here <---- --> <circle cx="20" cy="10" fill="red" r="10"></circle> <circle cx="20" cy="36" fill="red" r="10"></circle> </g> </svg> 

如果您已经通过"translate(" + dx + "," + dy + ") transform这些圆,请检查是否是因为位置y值为负,并且圆在您的svg区域中不可见。

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

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