Graphe ressources

This commit is contained in:
Éric Li 2021-04-29 20:16:12 +02:00
parent b95a0410d1
commit f8f8af0e46
3 changed files with 123 additions and 55 deletions

View File

@ -248,13 +248,13 @@ for indexSem, sem in enumerate(ressources):
defineSearchTerm(data, url, documents) defineSearchTerm(data, url, documents)
template.stream(datas).dump(REPERTOIRE_HTML + "/" + url) template.stream(datas).dump(REPERTOIRE_HTML + "/" + url)
relations["nodes"].append({"id": data["code"]}) relations["nodes"].append({"id": data["code"], "type": "Ressource"})
for sae in data["sae"]: for sae in data["sae"]:
if not any(sae in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": sae}) if not any(sae in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": sae, "type": "SAE"})
relations["links"].append({"source": data["code"], "target": sae, "type": "RessourceToSAE"}) relations["links"].append({"source": data["code"], "target": sae, "type": "RessourceToSAE"})
for rt in data["acs"]: for rt in data["acs"]:
for ac in data["acs"][rt]: for ac in data["acs"][rt]:
if not any(ac in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": ac}) if not any(ac in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": ac, "type": "AC"})
relations["links"].append({"source": data["code"], "target": ac, "type": "RessourceToAC"}) relations["links"].append({"source": data["code"], "target": ac, "type": "RessourceToAC"})
#Créer un fichier contenant la liste des ressources du semestre #Créer un fichier contenant la liste des ressources du semestre
@ -286,10 +286,10 @@ for indexSem, sem in enumerate(ressources):
SAE_mobilise_AC[ac].append(sae.getInfo()) SAE_mobilise_AC[ac].append(sae.getInfo())
if not any(data["code"] in node["id"] for node in relations["nodes"]): if not any(data["code"] in node["id"] for node in relations["nodes"]):
relations["nodes"].append({"id": data["code"]}) relations["nodes"].append({"id": data["code"], "type": "SAE"})
for rt in data["acs"]: for rt in data["acs"]:
for ac in data["acs"][rt]: for ac in data["acs"][rt]:
if not any(ac in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": ac}) if not any(ac in node["id"] for node in relations["nodes"]): relations["nodes"].append({"id": ac, "type": "AC"})
relations["links"].append({"source": data["code"], "target": ac, "type": "SAEToAC"}) relations["links"].append({"source": data["code"], "target": ac, "type": "SAEToAC"})
for sae in exemples[sem]: for sae in exemples[sem]:

View File

@ -4,16 +4,23 @@
<script src="https://d3js.org/d3.v5.js"></script> <script src="https://d3js.org/d3.v5.js"></script>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<style> <!--
.node { <div class="field is-horizontal">
stroke: #000; <div class="control">
stroke-width: 1.5px; <label class="checkbox">
} <input id="Ressources" class="categorie" type="checkbox" checked>
.link { Ressources
stroke: #999; </label>
stroke-width: 1.5px; <label class="checkbox">
} <input id="SAEs" class="categorie" type="checkbox" checked>
</style> SAEs
<svg width="960" height="500"></svg> </label>
<label class="checkbox">
<input id="ACs" class="categorie" type="checkbox" checked>
ACs
</label>
</div>
</div>-->
<svg style="border: 1px solid black; margin: auto;"></svg>
<script src="js/graph.js"></script> <script src="js/graph.js"></script>
{% endblock %} {% endblock %}

View File

@ -1,60 +1,120 @@
drag = simulation => { drag = simulation => {
function dragstarted(event) { function dragstarted(d) {
if (!event.active) simulation.alphaTarget(0.3).restart(); if (!d3.event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x; d.fx = d.x;
event.subject.fy = event.subject.y; d.fy = d.y;
} }
function dragged(event) { function dragged(d) {
event.subject.fx = event.x; d.fx = d3.event.x;
event.subject.fy = event.y; d.fy = d3.event.y;
} }
function dragended(event) { function dragended(d) {
if (!event.active) simulation.alphaTarget(0); if (!d3.event.active) simulation.alphaTarget(0);
event.subject.fx = null; d.fx = null;
event.subject.fy = null; d.fy = null;
} }
return d3.drag() return d3.drag()
.on("start", dragstarted) .on("start", dragstarted)
.on("drag", dragged) .on("drag", dragged)
.on("end", dragended); .on("end", dragended);
} }
var svg = d3.select("svg"), const graph = {{data}}
width = +svg.attr("width"), var nodes = graph["nodes"]
height = +svg.attr("height"); var links = graph["links"]
var simulation = d3.forceSimulation() $("document").ready(function() {
.force("charge", d3.forceManyBody().strength(-200))
.force("link", d3.forceLink().id(function(d) { return d.id; }).distance(40))
.force("x", d3.forceX(width / 2))
.force("y", d3.forceY(height / 2))
.on("tick", ticked);
var link = svg.selectAll(".link"), $(".categorie").click(function() {
node = svg.selectAll(".node"); 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()
graph = {{data}} 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); });
}
});
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", [0, 0, width, height])
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));
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));
simulation.nodes(graph.nodes); var NODE = svg.append("g")
simulation.force("link").links(graph.links); .attr("stroke-width", 1)
.attr("stroke", "black")
.attr("class","nodes")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
link = link NODE.append("rect")
.data(graph.links) .attr("width", 40)
.enter().append("line") .attr("height", 20)
.attr("class", "link"); .attr("x", -20)
.attr("y", -10)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", function(d) { return color(d.type); });
node = node NODE.append("text")
.data(graph.nodes) .text(d => d.id)
.enter().append("circle") .attr("dx", 15)
.attr("class", "node") .attr("dy", 25)
.attr("r", 6)
.style("fill", function(d) { return d.id; });
simulation.on("tick", ticked);
function ticked() { function ticked() {
link.attr("x1", function(d) { return d.source.x; }) link.attr("x1", function(d) { return d.source.x; })
@ -62,6 +122,7 @@ function ticked() {
.attr("x2", function(d) { return d.target.x; }) .attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; }); .attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; }) NODE
.attr("cy", function(d) { return d.y; }); .attr("transform", d => `translate(${d.x},${d.y})`);
} }