- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在 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/
我正在使用“laravel/lumen-framework”:“5.7.*” 我有两个中间件,第一个 AuthTokenAuthenticate 应该应用于所有路由,因此它在 bootstrap/ap
当同时播放两个音频时...声音会相互抵消。如何解决这个奇怪的现象? 我有一些代码,其中单击按钮时有音频,并且每隔十秒就有音频(在后台服务中)。我有以下代码来在十秒间隔播放时停止按钮音频,并且工作正常:
我有一个功能可以在我的网站上搜索用户, 我的网站上还有一个面向 friend 的功能。 我有一个查询要在我的网站上搜索正确的用户,并且 我有一个查询可以确定用户的 friend ,他们都按应有的方式工
是否可以对记录使用 GROUP BY? 例如,我有一大堆联系人数据,可能包含也可能不包含所有信息 - 在 CSV 意义上,如果可能看起来像这样: Test User, Address1, Addres
如何在客户端 JavaScript 中创建一个环境,其中与用户界面和 View 相关的任何代码优先于其他代码? 我知道你可以使用 setTimeout([function],0); 将事情推到下一个刻
Jasmine 有没有办法定义测试失败的概率? 例如,现在 500'ing 的服务比不显示在页面上的简单内容更糟糕。 谢谢! 最佳答案 这不是单元或集成测试的工作方式。以太测试是否失败。并且您的套件中
我正在为我参与的一个项目开发一个 API。该 API 将由 Android 应用、iOS 应用和桌面网站使用。几乎所有 API 都只有注册用户才能访问。该 API 允许通过 WSSE 进行身份验证,这
我正在开发一些库并创建了这个有缺陷的代码: //------------------- Gmaps = {}; Gmaps.map = new Gmaps4RailsGoogle(); //there
我有一个使用[NSLocale ISOCountryCodes]获得的国家/地区的NSArray。如何排序此NSArray,以便可以将某些常用国家(地区)放在列表的顶部,同时将其余国家/地区按字母顺序
我正在为注册表编写代码,因为我正在从另一个文件中为电话号码列导入代码,但是当我将该代码放入其中时,您可以看到@include('layouts.phone');它显示为 当我放置@include('l
我刚刚遇到了 javascript 代码 file_upload_started = progress < 100; 我不知道如何阅读它,谷歌也没有真正出现太多。我什至不知道该怎么调用它,所以很难进行
目前,我正在 cppinstitute.org 学习 C 语言认证类(class)。在其中一个测验中,有一个如下的问题来识别输出。 int i = 1,j= 1; int w1,w2; w1 = (i
我想将无符号短值从 MSB 优先转换为 LSB 优先。做了下面的代码,但它不工作。有人可以指出我所做的错误吗 #include using namespace std; int main() {
考虑以下场景:我的应用程序有一些依赖于我自己的 POM 优先 Artifact (使用纯 Maven 构建)和一些依赖于我自己的 list 优先 Artifact (使用 Tycho 构建)。对于 P
拥有它应该是很自然的事情,我想知道是否有来自 TPL DataFlow 库的优先级缓冲区块的现成实现? 最佳答案 似乎实现这一目标的最佳方法是使用专门的 任务调度器 ,而不是实现您自己的 Buffer
我有一个 date 字段,它显示为从今天开始的天数。因此 2055-01-01 和 1950-01-01 将分别显示为正数和负数。现在我希望对这些进行排序,以便非负数按升序排在第一位,然后负数按降序排
我遇到一个问题,我看到我的事件类和悬停类正在 Firebug 中应用,但它没有优先于现有样式。 因此,如果我的元素设置了背景颜色,则事件和悬停背景颜色不会更改元素。 我该如何解决这个问题? 最佳答案
我正在考虑为 Salesforce Outbound Messaging 实现监听器应用程序。 walk through 使用已弃用的 ASMX Web 服务实现它。代码是使用带有/serverInt
对于每个表,EF 都会生成一个部分类,其中所有字段都可以公开访问,例如 public int ID { get; set; } 是否可以将 set 设为私有(private)?然后,我将只允许调用我的
我正在为水电站编写一个数据评估应用程序。我需要从服务器下载数据,该数据就在那里 - 作为 MySQL 表,格式化为 JSON 数组。现在,经过无数个小时的工作,我已经完成了连接到服务器、下载数据并将其
我是一名优秀的程序员,十分优秀!