Referentiels/html/graphTemplate.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-04-28 19:34:34 +02:00
drag = simulation => {
2021-04-29 20:16:12 +02:00
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
2021-04-28 19:34:34 +02:00
}
2021-04-29 20:16:12 +02:00
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
2021-04-28 19:34:34 +02:00
}
2021-04-29 20:16:12 +02:00
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
2021-04-28 19:34:34 +02:00
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
2021-04-29 20:16:12 +02:00
}
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
const graph = {{data}}
var nodes = graph["nodes"]
var links = graph["links"]
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
$("document").ready(function() {
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
$(".categorie").click(function() {
nodes = []
links = []
if($("#Ressources").prop("checked")) {graph["nodes"].map(node => {if(node.type == "Ressource"){nodes.push(node);}})}
if($("#SAEs").prop("checked")) {
if (nodes.length != 0) {graph["links"].map(link => {if(link.type == "RessourceToSAE"){links.push(link);}})}
nodes.concat(graph["nodes"].map(node => {if(node.type == "SAE"){nodes.push(node);}}));
}
if($("#ACs").prop("checked")) {
if($("#Ressources").prop("checked")) {graph["links"].map(link => {if(link.type == "RessourceToAC"){links.push(link);}})}
if($("#SAEs").prop("checked")) {graph["links"].map(link => {if(link.type == "SAEToAC"){links.push(link);}})}
nodes.concat(graph["nodes"].map(node => {if(node.type == "AC"){nodes.push(node);}}));
}
redraw()
simulation.nodes(nodes);
simulation.force("links", d3.forceLink(links).id(node => node.id));
simulation.alpha(1).restart();
});
function redraw() {
$(".nodes").children().remove();
NODE.data(nodes)
.remove()
.enter().append("rect")
.attr("width", 40)
.attr("height", 20)
.attr("x", -20)
.attr("y", -10)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", function(d) { return color(d.type); });
}
});
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
const nodetypes = Array.from(new Set(nodes.map(node => node.type)))
const linktypes = Array.from(new Set(links.map(link => link.type)))
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
const color = d3.scaleOrdinal(nodetypes.concat(linktypes),d3.schemeCategory10)
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
var height = 800, width = 800;
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
var svg = d3.select("svg")
.attr("viewBox", [0, 0, width, height])
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
var simulation = d3.forceSimulation(nodes)
.force("charge", d3.forceManyBody().strength(-400))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("link", d3.forceLink(links).id(node => node.id));
2021-04-28 19:34:34 +02:00
2021-04-29 20:16:12 +02:00
var link = svg.append("g")
.attr("stroke-width", 1.5)
.attr("class","links")
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", d => color(d.type));
var NODE = svg.append("g")
.attr("stroke-width", 1)
.attr("stroke", "black")
.attr("class","nodes")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
NODE.append("rect")
.attr("width", 40)
.attr("height", 20)
.attr("x", -20)
.attr("y", -10)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", function(d) { return color(d.type); });
NODE.append("text")
.text(d => d.id)
.attr("dx", 15)
.attr("dy", 25)
simulation.on("tick", ticked);
2021-04-28 19:34:34 +02:00
function ticked() {
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; });
2021-04-29 20:16:12 +02:00
NODE
.attr("transform", d => `translate(${d.x},${d.y})`);
}