Javascript 建立D3力有向图
给尊敬的读者。我对javascript相当陌生,我遇到了这个问题。我正在尝试实现此力定向图的修改版本: json数据是从php脚本动态生成的。其思想是将连接到一个特定节点(在php脚本中定义)的所有线都涂成一种颜色,其他所有线都涂成灰色。我正试图通过将json文件中的源变量与php脚本中的变量相匹配,并在这种情况下更改颜色,如下所示:Javascript 建立D3力有向图,javascript,d3.js,force-layout,Javascript,D3.js,Force Layout,给尊敬的读者。我对javascript相当陌生,我遇到了这个问题。我正在尝试实现此力定向图的修改版本: json数据是从php脚本动态生成的。其思想是将连接到一个特定节点(在php脚本中定义)的所有线都涂成一种颜色,其他所有线都涂成灰色。我正试图通过将json文件中的源变量与php脚本中的变量相匹配,并在这种情况下更改颜色,如下所示: var link = svg.selectAll("line.link") .data(json.links) .enter().append("l
var link = svg.selectAll("line.link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value);})
.style("stroke-opacity", function(d) { return d.value/10;})
.style("stroke", function(d) {
x = (tested == d.source) ? return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
})
我已经盯着这个看了好几天了,想弄明白如何完成这件事,我被卡住了。任何帮助都将不胜感激
这是我的完整剧本
<script type="text/javascript">
var width = 1200,
height = 1200;
var color = d3.scale.category20();
var tested=<?php echo $tested_source;?>; //<-- the variable from php
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("data.json", function(json) {
var force = d3.layout.force()
.charge(-130)
.linkDistance(function(d) { return 500-(50*d.value);})
.size([width, height]);
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll("line.link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value);})
.style("stroke-opacity", function(d) { return d.value/10;})
.style("stroke", function(d) {
x = (tested == d.source) ? return '#1f77b4' : '#707070'; //<-- Attempt to change the color of the link when this is true. But is is not working... :(
})
var node = svg.selectAll("circle.node")
.data(json.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 12)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
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; });
});
});
</script>
可变宽度=1200,
高度=1200;
var color=d3.scale.category20();
var检验=// d.source
是一个对象,您不能使用=
来确定测试的是否是一个类似的对象
如果您想要测试下面描述的d.source
对象的特定值(我假设您想要),您需要指定它
以下是源对象体系结构:(我使用的数据来自miserables.json
)
现在,下面是代码中的断开部分:
x = (tested == d.source) ? return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
如果仍要将值指定给x,可以执行以下操作
x = test ? value_if_true : value_if_false;
return x;
你应该这样做:
return (tested == d.source) ? '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
另外,如果PHP中的变量是字符串,则应该执行以下操作
var tested="<?php echo $tested_source;?>"; //<-- the variable from php
谢谢说到javascript,我完全是个新手。该建议确实使脚本工作,但它仍然不会更改连接线的颜色。我认为从json数组中检索d.source可能会有问题。帮助??我已经用一些代码编辑了我的答案,这些代码与您正在测试的源对象有关。我想问题就出在那里。谢谢你的快速回复。也许我误解了什么,但您所指的对象是节点。我正在尝试更改节点之间连接线的颜色。此值在miserables.json文件的“Links”中定义。我完全误解了什么吗?.style(“stroke”,function(d){console.dir(d.source);return(tested==d.source)?“#1f77b4”:“#707070”})
这将帮助您找到颜色没有变化的原因。使用firefox+firebug或chrome以及chrome开发者工具。我想我帮不了你更多。仅供参考,我上面打印的源对象正是我之前评论中console.dir(d.source)
的结果
x = test ? value_if_true : value_if_false;
return x;
return (tested == d.source) ? '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
return (tested === d.source.name) ? '#1f77b4' : '#707070';
var tested="<?php echo $tested_source;?>"; //<-- the variable from php
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force().charge(-120).linkDistance(30).size([width, height]);
var svg = d3.select("#chart").append("svg").attr("width", width).attr("height", height);
var tested = 20;
d3.json("miserables.json", function (json) {
force.nodes(json.nodes).links(json.links).start();
var link = svg.selectAll("line.link")
.data(json.links)
.enter()
.append("line")
.attr("class", "link")
.style("stroke-width", function (d) {
return Math.sqrt(d.value);
}).style("stroke-opacity", function (d) {
return d.value / 10;
}).style("stroke", function (d) {
return (tested == d.source.index) ? '#ee3322' : '#707070'; //'#1f77b4'
});
var node = svg.selectAll("circle.node")
.data(json.nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function (d) {
return color(d.group);
}).call(force.drag);
node.append("title").text(function (d) {
return d.name;
});
force.on("tick", function () {
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;
});
});
});