javascript – 定向网络中的衰落网络连接 – d3js
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 定向网络中的衰落网络连接 – d3js,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8500字,纯文字阅读大概需要13分钟。
内容图文
我有一个使用d3.layout.force的定向网络.适应this answer,如果连接(连接方向无关紧要),我设法获得节点和链接淡入淡出.
我遇到的问题是能够在它们所处的路径通过鼠标悬停事件改变其不透明度时更改标记的不透明度.
这是包含isConnected函数的脚本,用于确定连接的节点:
<script>
function bar() {
console.log("click");
force.stop();
force.start();
}
var links = [
{source: "A", target: "D", type: "high"},
{source: "A", target: "K", type: "high"},
{source: "B", target: "G", type: "high"},
{source: "C", target: "A", type: "low"},
{source: "D", target: "K", type: "low"},
{source: "E", target: "A", type: "low"},
{source: "F", target: "B", type: "low"},
{source: "K", target: "J", type: "low"},
{source: "F", target: "A", type: "low"},
{source: "F", target: "I", type: "low"},
{source: "G", target: "H", type: "low"},
{source: "E", target: "K", type: "high"},
{source: "E", target: "G", type: "low"},
{source: "E", target: "F", type: "high"},
{source: "D", target: "E", type: "high"}
];
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
var width = 960,
height = 700;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(105)
.charge(-775)
.on("tick", tick)
.start();
force.on("start", function () {
console.log("start");
});
force.on("end", function () {
console.log("end");
});
R=18
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// add defs-marker
svg.append('svg:defs')
.append('svg:marker')
.attr('id', 'end-arrow')
.attr('viewBox', '0 0 10 10')
.attr('refX', 2+R)
.attr('refY', 5)
.attr('markerWidth', 4)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,0 L0,10 L10,5 z');
var link = svg.selectAll(".link")
.data(force.links())
.enter()
.append("line")
.attr("class", "link")
.attr('marker-end', 'url(#end-arrow)')
;
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", R)
.on("mouseover", fade(.1))
.on("mouseout", fade(1))
;
node.append("text")
.attr("x", 0)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
function tick() {
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
var linkedByIndex = {};
links.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
function isConnected(a, b) {
return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
}
function fade(opacity) {
return function(d) {
node.style("stroke-opacity", function(o) {
thisOpacity = isConnected(d, o) ? 1 : opacity;
this.setAttribute('fill-opacity', thisOpacity);
return thisOpacity;
});
link.style("stroke-opacity", function(o) {
return o.source === d || o.target === d ? 1 : opacity;
});
marker.style("opacity", function(o) {
return o.source === d || o.target === d ? 1 : opacity;
});
};
}
</script>
一个切线相关的问题是如何缩短路径,以便当节点和链路的不透明度消失时,到达每个节点中间的线路不明显.
解决方法:
由于标记实例的呈现方式,您的方法不可行.将我自己的answers中的一个变为cannibal并引用SVG规范:
07001
[…]
The rendering effect of a marker is as if the contents of the referenced ‘marker’ element were deeply cloned into a separate non-exposed DOM tree for each instance of the marker. Because the cloned DOM tree is non-exposed, the SVG DOM does not show the cloned instance of the marker.
只有原始标记元素,即声明<标记>.元素,可以使用CSS进行样式化,而通过属性marker-start,marker-mid或marker-end引用的克隆实例不可访问,因此不能单独设置样式.
CSS2 selectors can be applied to the original (i.e., referenced) elements because they are part of the formal document structure. CSS2 selectors cannot be applied to the (conceptually) cloned DOM tree because its contents are not part of the formal document structure.
为了规避这些约束,您可以使用两个定义标记元素,第二个是第一个具有降低不透明度的克隆版本.
// add defs-markers
svg.append('svg:defs').selectAll("marker")
.data([{id:"end-arrow", opacity:1}, {id:"end-arrow-fade", opacity:0.1}])
.enter().append('marker')
.attr('id', function(d) { return d.id; })
.attr('viewBox', '0 0 10 10')
.attr('refX', 2+R)
.attr('refY', 5)
.attr('markerWidth', 4)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,0 L0,10 L10,5 z')
.style("opacity", function(d) { return d.opacity; });
在你的fade()函数中,你可以切换行的marker-end属性来引用相应的标记的id:
link.attr("marker-end", function(o) {
return opacity === 1 || o.source === d || o.target === d
? 'url(#end-arrow)' : 'url(#end-arrow-fade)';
});
请查看以下代码片段以了解正常工作:
function bar() {
console.log("click");
force.stop();
force.start();
}
var links = [
{source: "A", target: "D", type: "high"},
{source: "A", target: "K", type: "high"},
{source: "B", target: "G", type: "high"},
{source: "C", target: "A", type: "low"},
{source: "D", target: "K", type: "low"},
{source: "E", target: "A", type: "low"},
{source: "F", target: "B", type: "low"},
{source: "K", target: "J", type: "low"},
{source: "F", target: "A", type: "low"},
{source: "F", target: "I", type: "low"},
{source: "G", target: "H", type: "low"},
{source: "E", target: "K", type: "high"},
{source: "E", target: "G", type: "low"},
{source: "E", target: "F", type: "high"},
{source: "D", target: "E", type: "high"}
];
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
var width = 600,
height = 600;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(105)
.charge(-775)
.on("tick", tick)
.start();
force.on("start", function () {
console.log("start");
});
force.on("end", function () {
console.log("end");
});
R=18
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// add defs-markers
svg.append('svg:defs').selectAll("marker")
.data([{id:"end-arrow", opacity:1}, {id:"end-arrow-fade", opacity:0.1}])
.enter().append('marker')
.attr('id', function(d) { return d.id; })
.attr('viewBox', '0 0 10 10')
.attr('refX', 2+R)
.attr('refY', 5)
.attr('markerWidth', 4)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,0 L0,10 L10,5 z')
.style("opacity", function(d) { return d.opacity; });
var link = svg.selectAll(".link")
.data(force.links())
.enter()
.append("line")
.attr("class", "link")
.attr('marker-end', 'url(#end-arrow)');
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", R)
.on("mouseover", fade(.1))
.on("mouseout", fade(1))
;
node.append("text")
.attr("x", 0)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
function tick() {
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("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
var linkedByIndex = {};
links.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
function isConnected(a, b) {
return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
}
function fade(opacity) {
return function(d) {
node.style("stroke-opacity", function(o) {
thisOpacity = isConnected(d, o) ? 1 : opacity;
this.setAttribute('fill-opacity', thisOpacity);
return thisOpacity;
});
link.style("stroke-opacity", function(o) {
return o.source === d || o.target === d ? 1 : opacity;
});
link.attr("marker-end", function(o) {
return opacity === 1 || o.source === d || o.target === d
? 'url(#end-arrow)' : 'url(#end-arrow-fade)';
});
};
}
.node circle {
fill: #DDD;
stroke: #777;
stroke-width: 2px;
}
.node text {
font-family: sans-serif;
text-anchor: middle;
pointer-events: none;
user-select: none;
-webkit-user-select: none;
}
.link {
stroke: #88A;
stroke-width: 4px;
}
text {
font: 18px sans-serif;
pointer-events: none;
}
#end-arrow {
fill: #88A;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
内容总结
以上是互联网集市为您收集整理的javascript – 定向网络中的衰落网络连接 – d3js全部内容,希望文章能够帮你解决javascript – 定向网络中的衰落网络连接 – d3js所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。