xxxxxxxxxx
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
console.log("debut script");
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("scrutins.json", function(error, json) {
if (error) throw error;
paires_votes = {};
tous_les_acteurs = {};
for (var i=0; i<json.scrutins.scrutin.length; i++) {
var date = new Date(json.scrutins.scrutin[i].dateScrutin);
var year = date.getFullYear();
if (year==2015) {
console.log(date.getFullYear());
var scrutin = json.scrutins.scrutin[i];
var groupes = scrutin.ventilationVotes.organe.groupes.groupe;
var tous_les_pours = [];
var tous_les_contres = [];
for (var j=0; j<groupes.length; j++) {
var organeRef = groupes[j].organeRef;
var pours = groupes[j].vote.decompteNominatif.pours;
var contres = groupes[j].vote.decompteNominatif.contres;
if (pours!=null) {
for (var k=0; k<pours.votant.length; k++)
tous_les_acteurs[pours.votant[k].acteurRef] = organeRef;
tous_les_pours = tous_les_pours.concat(pours.votant);
}
if (contres!=null) {
for (var k=0; k<contres.votant.length; k++)
tous_les_acteurs[contres.votant[k].acteurRef] = organeRef;
tous_les_contres = tous_les_contres.concat(contres.votant);
}
}
for (var j=0; j<tous_les_pours.length; j++) {
for (var k=j+1; k<tous_les_pours.length; k++) {
paires_votes[tous_les_pours[j].acteurRef+tous_les_pours[k].acteurRef] = ++paires_votes[tous_les_pours[j].acteurRef+tous_les_pours[k].acteurRef] || 0;
}
}
for (var j=0; j<tous_les_contres.length; j++) {
for (var k=j+1; k<tous_les_contres.length; k++) {
paires_votes[tous_les_contres[j].acteurRef+tous_les_contres[k].acteurRef] = ++paires_votes[tous_les_contres[j].acteurRef+tous_les_contres[k].acteurRef] || 0;
}
}
}
}
var graph = {};
var acteurs = Object.keys(tous_les_acteurs);
graph.nodes = [];
graph.links = [];
for (var i=0; i<acteurs.length; i++) {
graph.nodes[i] = {};
graph.nodes[i].id = acteurs[i];
graph.nodes[i].group = tous_les_acteurs[acteurs[i]];
for (var j=i+1; j<acteurs.length; j++) {
var votes = paires_votes[acteurs[i]+acteurs[j]]||0+paires_votes[acteurs[j]+acteurs[i]]||0;
if (votes>20) {
var link = {};
link.source = acteurs[i];
link.target = acteurs[j];
link.value = votes;
graph.links = graph.links.concat(link);
}
}
}
console.log(graph);
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return 0.3*Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
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; });
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
https://d3js.org/d3.v4.min.js