First timer, both to Observable and Javascript! so bear with me. i downloaded the sample as shown below and tried to “fetch” data for my JSON from a python call - which i do seem to get but it is never comparable to the “links” data as shown in both the code as well as the console output. Any help here to correct me on how this ‘incoming’ JSON data can be made comparable to the “links” data on the code - would be great.
<script>
var width = 960,
height = 500;
//force = d3.layout.force().nodes(d3.values(ailments)).links(rels).size([width, height]).linkDistance(60).charge(-300).on("tick", tick).start();
var nodes = {};
var ailments = {} ;
var rels = [];
//var force = d3.layout.force().size([width, height]).linkDistance(60).charge(-300).on("tick",tick);
console.log('initiated force!! be with you !!');
d3.json("/getA", function(error, dataset){
console.log('getA the function gets called now ... ');
//ailments = JSON.stringify(dataset.nodes);
ailments = dataset.nodes;
//ailments = dataset.nodes['nodes'];
//rels = dataset.links['links'] ;
//rels = JSON.stringify(dataset.links);
rels = dataset.links;
console.log(ailments);
//console.log(nodes);
console.log('relationships coming up..');
console.log(rels);
//links = [dataset.links];
var links = [
{source: "burning eyes", target: "Bel", type: "SOLVED_BY"},
{source: "digestive disorders", target: "Bel", type: "SOLVED_BY"},
{source: "Piles", target: "Bel", type: "SOLVED_BY"},
{source: "Fever", target: "Bel", type: "SOLVED_BY"},
{source: "malaria", target: "Bel", type: "SOLVED_BY"}
];
console.log(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});
console.log('creating unique nodes gets called now');
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.attr("stroke", "black")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
var force = d3.layout.force().nodes(d3.values(nodes)).links(links).size([width, height]).linkDistance(60).charge(-300).on("tick", tick).start();
console.log('graph goes up only now');
force.on("tick", function(e) {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
});
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("path")
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", 6)
.call(force.drag);
circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});
/*
// http://blog.thomsonreuters.com/index.php/mobile-patent-suits-graphic-of-the-day/
var oldlinks = [
{source: "Microsoft", target: "Amazon", type: "licensing"},
{source: "Microsoft", target: "HTC", type: "licensing"},
{source: "Samsung", target: "Apple", type: "suit"},
{source: "Motorola", target: "Apple", type: "suit"},
{source: "Nokia", target: "Apple", type: "resolved"},
{source: "Nokia", target: "Qualcomm", type: "suit"}
];
*/
function tick() {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
}
function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
</script>
The console output is also shown below … obviously something is wrong with my data construction. The data structure at the top - is what i receive from Python but am unable to use it AS IS - so i’ve pasted the original hand created data structure at the bottom for ease of comparison.