Browse Source

added some gravity and a bounding area to the fdg graph

master
ppound 6 years ago
parent
commit
e3aea363e7
  1. 22
      js/fdg.js

22
js/fdg.js

@ -5,7 +5,7 @@ Drupal.behaviors.islandora_herbarium_object_graph = {
var svg = d3.select(".fdg"), var svg = d3.select(".fdg"),
width = +svg.attr("width"), width = +svg.attr("width"),
height = +svg.attr("height"); height = +svg.attr("height");
var radius = 15;
var color = d3.scaleOrdinal(d3.schemeCategory20); var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation() var simulation = d3.forceSimulation()
@ -13,7 +13,8 @@ Drupal.behaviors.islandora_herbarium_object_graph = {
return d.id; return d.id;
}).distance(65).strength(1)) }).distance(65).strength(1))
.force("charge", d3.forceManyBody()) .force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2)); .force("center", d3.forceCenter(width / 2, height / 2))
.force("gravity",gravity(0.2));
var dataPath = "/" + settings.path.currentPath.replace('fdg', 'fdg_json'); var dataPath = "/" + settings.path.currentPath.replace('fdg', 'fdg_json');
d3.json(dataPath, function (error, graph) { d3.json(dataPath, function (error, graph) {
if (error) throw error; if (error) throw error;
@ -89,12 +90,8 @@ Drupal.behaviors.islandora_herbarium_object_graph = {
}); });
node node
.attr("cx", function (d) { .attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
return d.x; .attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
})
.attr("cy", function (d) {
return d.y;
});
//textElement //textElement
// .attr("dx", function(d) {return d.x}) // .attr("dx", function(d) {return d.x})
@ -108,8 +105,16 @@ Drupal.behaviors.islandora_herbarium_object_graph = {
return d.y; return d.y;
}); });
} }
}); });
function gravity(alpha) {
return function(d) {
d.y += (d.cy - d.y) * alpha;
d.x += (d.cx - d.x) * alpha;
};
}
function dragstarted(d) { function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x; d.fx = d.x;
@ -126,6 +131,7 @@ Drupal.behaviors.islandora_herbarium_object_graph = {
d.fx = null; d.fx = null;
d.fy = null; d.fy = null;
} }
} }
} }
} }

Loading…
Cancel
Save