在 d3 中使用力导向布局,如何使链接距离成为优先事项,同时仍保持良好的图形布局?
如果我指定动态链接距离,但保留默认费用,我的图形距离会因费用函数而变形一点,不再是准确的距离:
但是,如果我删除电荷,图形将如下所示:
感谢任何建议!
最佳答案
如果我理解正确,我相信有一个潜在的解决方案。
为了使链接距离准确,您需要将电荷和碰撞力设置为零,但正如您的图片所暗示的那样,节点的间距不会考虑其他节点,只是那些它们共享链接的节点和。由于 d3.force 初始化在叶序排列中没有 x,y 值的节点,链接和节点将以意想不到的方式聚集。但是,如果在整个模拟过程中施加排斥力,间距会有所改善,但距离会失真。
可能的解决方案是最初使用排斥力,因为您需要根据链接将节点分成可识别的集群。然后,在它们分开后,将排斥力减小到零,以便施加的唯一力与所需的链接距离有关。
这需要您随着图形的发展修改刻度函数中的力。这还要求所有链接距离彼此兼容(一个三 Angular 形的节点不能有两个 Angular 相隔 100 像素,其余 Angular 与另外两个 Angular 相距 10 像素)。
在简单的情况下,像这样的东西可能会在 tick 函数中起作用:
var alpha = this.alpha(); // starts at 1 by default, simulation ends at zero
var chargeStrength; // a multiplier for charge strength
if ( alpha > 0.2 ) {
chargeStrength = (alpha - 0.2 / 0.8); // decrease for the first portion of the simulation
}
else {
chargeStrength = 0; // leave at zero and give the link distance force time to work without competing forces
}
对于更复杂的可视化,您可以通过减少 alphaDecay 来留出更多的冷却时间,或者为更简单的可视化增加它。
我在这里做了一个简单的例子,在可视化距离的最后记录了(我在下面的代码片段中增加了 alphaDecay 以牺牲精度为代价来加速它,但它仍然相当不错)并引用了所需的距离。
var graph = {
nodes: d3.range(15).map(Object),
links: [
{source: 0, target: 1, distance: 20 },
{source: 0, target: 2, distance: 40},
{source: 0, target: 3, distance: 80},
{source: 1, target: 4, distance: 20},
{source: 1, target: 5, distance: 40},
{source: 1, target: 6, distance: 80},
{source: 2, target: 7, distance: 12},
{source: 2, target: 8, distance: 8},
{source: 2, target: 9, distance: 6},
{source: 3, target: 10, distance: 10},
{source: 3, target: 11, distance: 10},
{source: 3, target: 12, distance: 2},
{source: 3, target: 13, distance: 2},
{source: 3, target: 14, distance: 2}
]
};
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody().strength(-30 ))
.force("link", d3.forceLink().distance(function(d) { return d.distance } ).strength(2) )
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide",d3.forceCollide().strength(0).radius(0))
.alphaDecay(0.03)
.velocityDecay(0.4);
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", 1);
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", 3)
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
var alpha = this.alpha();
var chargeStrength;
if ( alpha > 0.2 ) {
chargeStrength = (alpha - 0.2 / 0.8);
}
else {
chargeStrength = 0;
}
this.force("charge", d3.forceManyBody().strength( -30 * chargeStrength ))
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
// validate:
if (alpha < 0.001) {
link.each(function(d,i) {
var a = d.source.x - d.target.x;
var b = d.source.y - d.target.y;
var c = Math.pow(a*a + b*b, 0.5);
console.log("specified length: " + graph.links[i].distance + ", realized distance: " + c );
})
}
}.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
<svg width="500" height="300"></svg>
根据图表的复杂性,您可能需要调整冷却时间、排斥力强度以及如何在 alpha 冷却时改变它、velocityDecay(可能在 tick 函数中修改它)和/或距离力本身。
关于javascript - d3 力导向布局 - 链接距离优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38253560/
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'
我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有一个未排序的链接列表,我将其保存在旁边,我想单击每个链接并确保它转到真实页面而不是404、500等。问题是我不知道该怎么做。是否有一些我可以检查的对象会给我http状态代码或任何东西?mylinks=Browser.ul(:id,'my_ul_id').linksmylinks.eachdo|link|link.click#needtocheckfora200statusorsomethinghere!how?Browser.backend 最佳答案 我的回答与铁皮人的想法类似。require'net/http'require'
我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
假设您有一个可执行文件foo.rb,其库bar.rb的布局如下:/bin/foo.rb/lib/bar.rb在foo.rb的header中放置以下要求以在bar.rb中引入功能:requireFile.dirname(__FILE__)+"../lib/bar.rb"只要对foo.rb的所有调用都是直接的,这就可以正常工作。如果你把$HOME/project和符号链接(symboliclink)foo.rb放入$HOME/usr/bin,然后__FILE__解析为$HOME/usr/bin/foo.rb,因此无法找到bar.rb关于foo.rb的目录名.我意识到像rubygems这
在我的mac上安装几个东西时遇到这个问题,我认为这个问题来自将我的豹子升级到雪豹。我认为这个问题也与macports有关。/usr/local/lib/libz.1.dylib,filewasbuiltfori386whichisnotthearchitecturebeinglinked(x86_64)有什么想法吗?更新更具体地说,这发生在安装nokogirigem时日志看起来像:xslt_stylesheet.c:127:warning:passingargument1of‘Nokogiri_wrap_xml_document’withdifferentwidthduetoproto