D3 - How to convert circle-pack to ellipse-pack?

自闭症网瘾萝莉.ら 提交于 2019-12-06 11:15:25

Well the part you need to pay attention to is :

var circles = vis.append("circle")
    .attr("stroke", "black")
    .style("fill", function(d) { return !d.children ? "tan" : "beige"; })
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; })
    .attr("r", function(d) { return d.r; });

What happens here is you are creating an svg circle element with attributes of cx, cy, and r, similar to <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />

To convert this to ellipsis, you need to know what are the attributes you need to set. An svg ellipse element can be created like so: <ellipse cx="200" cy="80" rx="100" ry="50"> Note how you need to set the cx, cy, rx, and ry as opposed to cx, cy, and r for <circle>

Based on this knowledge you should be now able to convert your circle to ellipse like this :

var ellipses = vis.append("ellipse")
    .attr("stroke", "black")
    .style("fill", function(d) { return !d.children ? "tan" : "beige"; })
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; })
    .attr("rx", 10) // define your own rule for x radius
    .attr("ry", 5); // define your own rule for y radius
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!