尊敬する読者に。私は合理的にjavascriptで新しく、私はこの問題に遭遇しました。私はこの力有向グラフの修正バージョンを実装しようとしています:D3強制有向グラフの設定
http://mbostock.github.com/d3/ex/force.html
JSONデータは、PHPスクリプトからオンザフライで生成されます。アイデアは、ある特定のノード(phpスクリプトで定義されている)に接続しているすべての行を1つの色で色付けし、他はすべてグレーの色合いで色付けすることです。私は、PHPスクリプトから変数にJSONファイルのソース変数に一致しているが、このような真のときに色を変更することでそれを行うにしようとしています:
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.
})
しかし、これは動作しません。私はちょうどこれを成し遂げるために把握しよう日間、この時私が見つめてきたこの
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 '#707070';
})
を行うと、私はこだわっている場合、スクリプトは細かいが、色の変化なしで動作します。どんな助けでも大歓迎です!!ここで
は私の完全なスクリプト
<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>
ありがとう!私はそれがjavascriptになると完全な新人です。この提案はスクリプトを動作させますが、接続線の色は変わりません。私は、json配列からd.sourceを取得する際に問題があると思います。助けて?? – user1378824
私はあなたが 'テスト済み 'に対してテストしているソースオブジェクトに関するいくつかのコードで私の答えを編集しました。私は問題がそこから来ると思う。 –
クイック返信をありがとう。たぶん私は何かを誤解しているかもしれませんが、あなたが指しているオブジェクトはノードです。私はノード間の接続線の色を変更しようとしています。この値は、miserables.jsonファイルの "Links"で定義されています。私は何かを完全に誤解していますか? – user1378824