Circle packs as nodes of a D3 force layout

淺唱寂寞╮ 提交于 2019-12-05 22:25:45

The strategy should be more or less: Format the data structure to reflect that hierarchy:

{
    name: 'Election Results',
    children: [
        {
            region: 'BC',
            children: [
                {party: 'Conservative', seats: 12},
                ...
            ]
        }
    ]
}

And compute the Pack Layout for this data, but don't draw any circles yet. This will give you the size for each region, in the nodes where depth === 1. Now you can compute the force layout. To avoid one region overlapping another one, you should set the charge to be proportional to the area of each region (proportional to Math.pow(d.r, 2).

Now, you can create one group for each node where depth === 1 (the regions), setting the position given by the force layout.

For each group, compute a new pack layout, setting the size of each one to twice the radius of the region. You can now create the circles in a subselection of the groups selection, giving each one the radius set by the second pack layouts. Finally, I would create a class for each party, so you can style each bubble differently. Perhaps the same should be done for the group circles.

That said, I would rather use just the bubble layout, or having a pack layout for the regions with multi-foci force layout for the party circles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!