,可以通过以下步骤实现:
<svg id="chart"></svg>
var data = [
{
name: "Parent 1",
children: [
{ name: "Child 1.1" },
{ name: "Child 1.2" },
{ name: "Child 1.3" }
]
},
{
name: "Parent 2",
children: [
{ name: "Child 2.1" },
{ name: "Child 2.2" }
]
},
// ...
];
d3.pack()
布局。var pack = d3.pack()
.size([width, height])
.padding(10);
其中,width
和height
是SVG容器的宽度和高度。
d3.hierarchy()
方法将数据转换为层次结构对象。var root = d3.hierarchy({ children: data })
.sum(function(d) { return 1; });
pack()
布局将层次结构对象转换为绘图所需的坐标和半径。pack(root);
var nodes = d3.select("#chart")
.selectAll("circle")
.data(root.descendants())
.enter()
.append("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return d.r; });
在上述代码中,使用selectAll("circle")
选择所有的圆形元素,并绑定数据。然后,使用enter()
方法创建新的圆形元素,并设置其位置和半径。
nodes.attr("fill", "steelblue")
.attr("opacity", 0.7)
.on("mouseover", function(d) {
d3.select(this).attr("fill", "orange");
})
.on("mouseout", function(d) {
d3.select(this).attr("fill", "steelblue");
});
在上述代码中,使用attr()
方法设置填充颜色和透明度。使用on()
方法添加鼠标悬停和移出时的事件处理函数。
这样,你就可以使用d3.js绘制两级嵌套数据的图形符号了。请注意,上述代码仅为示例,你可以根据实际需求进行修改和扩展。
腾讯云相关产品和产品介绍链接地址:
没有搜到相关的文章