Referentiels/html/graphTemplate.js

141 lines
3.7 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-30 11:26:02 +02:00
const nodetypes = Array.from(new Set(nodes.map(node => node.type)))
const linktypes = Array.from(new Set(links.map(link => link.type)))
const color = d3.scaleOrdinal(nodetypes.concat(linktypes),d3.schemeCategory10)
var height = 800, width = 800;
var svg = d3.select("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
var simulation = d3.forceSimulation(nodes)
.force("charge", d3.forceManyBody().strength(-400))
.force("link", d3.forceLink(links).id(node => node.id).distance(50))
.force("x", d3.forceX())
.force("y", d3.forceY())
.on("tick", ticked);
var link = svg.append("g")
.attr("stroke-width", 1.5)
.attr("class","links")
.selectAll("line")
var node = svg.append("g")
.attr("stroke-width", 1)
.attr("stroke", "black")
.attr("class", "nodes")
.selectAll("g")
redraw();
2021-04-28 19:34:34 +02:00
2021-04-30 11:26:02 +02:00
$("document").ready(function() {
2021-04-30 12:52:14 +02:00
$("input").click(function() {
var sem = ["default"];
$(".semestre:checked").each(function() { sem.push($(this).val()) });
var RessourceChecked = $("#Ressources").prop("checked");
var SAEChecked = $("#SAEs").prop("checked");
var ACChecked = $("#ACs").prop("checked");
nodes = [];
links = [];
if(RessourceChecked) {
graph["nodes"].forEach(node => {
if(node.type == "Ressource" && sem.includes(node.sem)) { nodes.push(node); }
});
2021-04-29 20:16:12 +02:00
}
2021-04-30 12:52:14 +02:00
if(SAEChecked) {
graph["nodes"].forEach(node => {
if(node.type == "SAE") { nodes.push(node); }
});
if (RessourceChecked) {
graph["links"].forEach(link => { if(link.type == "RessourceToSAE" && sem.includes(link.sem)) {links.push(link);} })
}
}
if(ACChecked) {
graph["nodes"].forEach(node => {if(node.type == "AC"){nodes.push(node);}});
if(RessourceChecked) {graph["links"].forEach(link => {if(link.type == "RessourceToAC" && sem.includes(link.sem)){links.push(link);}})}
if(SAEChecked) {graph["links"].forEach(link => {if(link.type == "SAEToAC" && sem.includes(link.sem)){links.push(link);}})}
2021-04-29 20:16:12 +02:00
}
2021-04-30 15:35:57 +02:00
2021-04-30 11:26:02 +02:00
redraw();
2021-04-30 15:35:57 +02:00
2021-04-29 20:16:12 +02:00
});
});
2021-04-28 19:34:34 +02:00
2021-04-30 11:26:02 +02:00
function redraw() {
2021-04-28 19:34:34 +02:00
2021-04-30 11:26:02 +02:00
link = link.data(links)
2021-04-29 20:16:12 +02:00
.join("line")
2021-04-30 11:26:02 +02:00
.attr("stroke", d => color(d.type));
2021-04-29 20:16:12 +02:00
2021-04-30 11:26:02 +02:00
node.selectAll("text").remove();
node.selectAll("rect").remove();
2021-04-29 20:16:12 +02:00
2021-04-30 11:26:02 +02:00
node = node.data(nodes)
2021-04-29 20:16:12 +02:00
.join("g")
2021-04-30 11:26:02 +02:00
.attr("class","node")
.call(drag(simulation))
node.append("rect")
2021-04-30 12:52:14 +02:00
.attr("width", 20)
.attr("height", 10)
.attr("x", -10)
.attr("y", -5)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", function(d) { return color(d.type); })
2021-04-30 11:26:02 +02:00
node.append("text")
2021-04-30 12:52:14 +02:00
.attr("style", "user-select: none")
.attr("dx", 7)
.attr("dy", 12)
.append("a")
.attr("href", d => d.id.replace("É", "E") + ".html")
.text(d => d.id)
2021-04-30 11:26:02 +02:00
simulation.nodes(nodes);
simulation.force("links", d3.forceLink(links).id(node => node.id).distance(100));
simulation.alpha(0.5).restart();
}
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-30 11:26:02 +02:00
node
2021-04-29 20:16:12 +02:00
.attr("transform", d => `translate(${d.x},${d.y})`);
}