प्रतिष्ठित पाठकों को सेट अप करना। मैं जावास्क्रिप्ट में काफी नया हूं और मैं इस समस्या में आया हूं। मैं इस बल निर्देशित ग्राफ का एक संशोधित संस्करण को लागू करने की कोशिश कर रहा हूँ:डी 3 बल निर्देशित ग्राफ
http://mbostock.github.com/d3/ex/force.html
json डेटा एक php स्क्रिप्ट से जेनरेट कर दिए है। विचार एक रंग में एक विशिष्ट नोड (एक php स्क्रिप्ट में परिभाषित) से जोड़ने वाली सभी लाइनों को रंगना है और अन्य सभी ग्रे के रंगों में रंगना है। मैं 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>
धन्यवाद! जब जावास्क्रिप्ट की बात आती है तो मैं पूरी तरह से रूकी हूं। सुझाव स्क्रिप्ट काम करता है हालांकि यह अभी भी कनेक्टिंग लाइनों का रंग नहीं बदलता है। मुझे लगता है कि json सरणी से d.source को पुनर्प्राप्त करने में कोई समस्या हो सकती है। मदद?? – user1378824
मैंने अपने उत्तर को उस स्रोत ऑब्जेक्ट से संबंधित कुछ कोड के साथ संपादित किया है जिसका आप परीक्षण 'परीक्षण' के खिलाफ परीक्षण कर रहे हैं। मुझे लगता है कि समस्या वहां से आती है। –
त्वरित उत्तर के लिए धन्यवाद। हो सकता है कि मैं कुछ गलत समझ रहा हूं लेकिन जिस वस्तु का आप जिक्र कर रहे हैं वह नोड है। मैं नोड्स के बीच कनेक्टिंग लाइनों का रंग बदलने की कोशिश कर रहा हूं। यह मान miserables.json फ़ाइल में "लिंक" में परिभाषित किया गया है। क्या मैं पूरी तरह से कुछ गलत समझ रहा हूँ? – user1378824