gpt4 book ai didi

javascript - 如何在D3中的节点链接图中绘制多条自边

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:10 24 4
gpt4 key购买 nike

可以按照此处所述在节点链接图中的节点上绘制单个自链接:D3 Force Layout Self-Linking Node

如果您需要在同一个节点上绘制多个链接,您会更改什么?

我尝试根据存在的自链接数量为其添加“轮换”。根据链接示例中的代码,我进行了以下更改:

function tick() {
link.attr("d", function(d) {
var x1 = d.source.x,
y1 = d.source.y,
x2 = d.target.x,
y2 = d.target.y,
dx = x2 - x1,
dy = y2 - y1,
dr = Math.sqrt(dx * dx + dy * dy),

// Defaults for normal edge.
drx = dr,
dry = dr,
xRotation = 0, // degrees
largeArc = 0, // 1 or 0
sweep = 1; // 1 or 0

// Self edge.
if ( x1 === x2 && y1 === y2 ) {
// Fiddle with this angle to get loop oriented.
var index = getIndexOfDuplicateEdge();
var degree = 360 / numberOfDuplicateEdges();
var degreeForIndex = degree * index;


xRotation = degreeForIndex; // Previously: -45;

// Needs to be 1.
largeArc = 1;

// Change sweep to change orientation of loop.
//sweep = 0; // I also tried to change it based on index % 2

// Make drx and dry different to get an ellipse
// instead of a circle.
drx = 30;
dry = 20;

// For whatever reason the arc collapses to a point if the beginning
// and ending points of the arc are the same, so kludge it.
x2 = x2 + 1;
y2 = y2 + 1;
}

return "M" + x1 + "," + y1 + "A" + drx + "," + dry + " " + xRotation + "," + largeArc + "," + sweep + " " + x2 + "," + y2;
});

这不会按预期绘制我的椭圆,我找不到处理这个问题的方法。基于SVG from Mozilla大弧度必须为 1。扫描可以为 0 或 1,并将“镜像”我的省略号。我可以在 90-180 之间使用 xRotation 和扫描 0/1,这将覆盖我的圆的 180 度。但是,我找不到在其他 180 度位置绘制省略号的方法。

自链接的数量可以变化,我总是希望在省略号之间有“最佳”分布。

理想情况下,它应该是这样的:

Self-Linking edges for a node

最佳答案

这个想法是将圆分成与你的花瓣一样多的部分。然后计算圆上每个花瓣的起点和终点,并在这些点上拟合椭圆。

您可以使用以下代码片段来实现此目的:(该函数假定您有一个 id 为“svgthing”的 svg 元素)

function radtodeg(angle) {
return angle * (180/Math.PI);
}

function flower( center_x, center_y, num_self_edges, start_angle, end_angle, radius, length ) {

var angle_sector = end_angle - start_angle;

var num_points = num_self_edges * 2;

var angle_per_point = angle_sector / num_points;
var angle_per_sector = angle_per_point * 2;

var str_builder = [];

for( var angle = start_angle; angle < end_angle; angle += angle_per_sector ) {
var start_sector_angle = angle;
var end_sector_angle = angle + angle_per_point;

var mid_sector_angle = angle + angle_per_point / 2;

var start_x = center_x + (radius * Math.cos(start_sector_angle));
var start_y = center_y + (radius * Math.sin(start_sector_angle));
var end_x = center_x + (radius * Math.cos(end_sector_angle));
var end_y = center_y + (radius * Math.sin(end_sector_angle));

var mid_x = center_x + (radius * Math.cos(mid_sector_angle));
var mid_y = center_y + (radius * Math.sin(mid_sector_angle));

str_builder.push("<path d='");
str_builder.push("M" + start_x + " " + start_y + ",");
str_builder.push("A " + length + " 1 " + radtodeg(mid_sector_angle) + " 0 1 " + end_x + " " + end_y);
str_builder.push("'/>\n");

str_builder.push("<circle cx='" + start_x + "' cy='" + start_y + "' r='5' />\n");
str_builder.push("<circle cx='" + end_x + "' cy='" + end_y + "' r='5'/>\n");
str_builder.push("<circle cx='" + mid_x + "' cy='" + mid_y + "' r='5'/>\n");
}

str_builder.push("<circle cx='" + center_x + "' cy='" + center_y + "' r='" + radius + "' />\n");

$("#svgthing").html(str_builder.join(""));
}

flower(60, 50, 8, 0, 2 * Math.PI, 50, 10);

示例调用将生成一朵有 8 个花瓣的花。

关于javascript - 如何在D3中的节点链接图中绘制多条自边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com