Built with blockbuilder.org
forked from nbonneel's block: TP 6 DataViz
forked from Naram22's block: TP 6 DataViz v2
forked from Naram22's block: TP 6 DataViz v2
forked from Naram22's block: TP 6 DataViz v2
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>
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));
var Vote = {};
d3.json("scrutins_test.json", function(error, graph) {
if (error) throw error;
for (var i=0;i<graph.scrutins.scrutin.length;i++){
if(graph.scrutins.scrutin[i].dateScrutin.indexOf(2015)==0){
Vote[i] = {"tab1":[],"tab2":[]};
var scrutin=graph.scrutins.scrutin[i]
console.log(i)
var l=0;
var m=0;
for (var j=0;j<7;j++){
pour=scrutin.ventilationVotes.organe.groupes.groupe[j].vote.decompteNominatif.pours;
if (pour !== null){
if (typeof pour.votant.length == 'undefined'){
Vote[i].tab1[l]=[pour.votant.acteurRef,scrutin.ventilationVotes.organe.groupes.groupe[j].organeRef];
l=l+1;
}
for (var k=0;k<pour.votant.length;k++){
Vote[i].tab1[l+k]=[pour.votant[k].acteurRef,scrutin.ventilationVotes.organe.groupes.groupe[j].organeRef];
}
l=l+k
}
var contre=scrutin.ventilationVotes.organe.groupes.groupe[j].vote.decompteNominatif.contres;
if (contre !== null){
if (typeof contre.votant.length == 'undefined'){
Vote[i].tab2[m]=[contre.votant.acteurRef,scrutin.ventilationVotes.organe.groupes.groupe[j].organeRef];
m=m+1;
}
for (var k=0;k<contre.votant.length;k++){
Vote[i].tab2[m+k]=[contre.votant[k].acteurRef,scrutin.ventilationVotes.organe.groupes.groupe[0].organeRef];
}
m=m+k
}
}
}
}
var member={};
for (var i=0;i<213;i++){
for (var j=0;j<Vote[i].tab1.length;j++){
if ( isNaN(member[Vote[i].tab1[j][0]])){
member[Vote[i].tab1[j][0]]=Vote[i].tab1[j][1]
}
}
for (var j=0;j<Vote[i].tab2.length;j++){
if ( isNaN(member[Vote[i].tab2[j][0]])){
member[Vote[i].tab2[j][0]]=Vote[i].tab2[j][1]
}
}
}
var resultat = {};
resultat["nodes"] = [];
resultat["links"] = [];
for (keys in member){
resultat.nodes.push
({"id": keys, "group": member[keys]});
}
var link={};
//link[Vote[0].tab1[0] + Vote[0].tab1[1]] = 0;
//console.log(link)
var l=0;
for (var i=0;i<213;i++){ // 213 corrspond
for (var j=0;j<Vote[i].tab1.length;j++){
for (var k=j+1;k<Vote[i].tab1.length-1;k++){
var tab =[Vote[i].tab1[j][0],Vote[i].tab1[k][0]];
tab.sort;
if ( isNaN(link[tab[0]+" "+tab[1]])){
link[tab[0]+" "+tab[1]]=1;
}else{
link[tab[0]+" "+tab[1]]=link[tab[0]+" "+tab[1]]+1
}
}
}
}
for (var i=0;i<213;i++){ // 213 corrspond
for (var j=0;j<Vote[i].tab2.length;j++){
for (var k=j+1;k<Vote[i].tab2.length-1;k++){
var tab =[Vote[i].tab2[j][0],Vote[i].tab2[k][0]];
tab.sort;
if ( isNaN(link[tab[0]+" "+tab[1]])){
link[tab[0]+" "+tab[1]]=1;
}else{
link[tab[0]+" "+tab[1]]=link[tab[0]+" "+tab[1]]+1
}
}
}
}
for (keys in link){
if(link[keys]>=20){
resultat.links.push
({"source": keys.split(" ")[0], "target": keys.split(" ")[1], "value": link[keys]});
}
}
console.log(resultat)
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(resultat.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.pow(d.value,0.25); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(resultat.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(resultat.nodes)
.on("tick", ticked);
simulation.force("link")
.links(resultat.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