Created
September 3, 2017 19:50
-
-
Save tezzutezzu/c6e16baac713eaf55449d81162e65d70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font-family: sans-serif; | |
} | |
.links line { | |
stroke: #999; | |
stroke-opacity: 0.6; | |
} | |
.nodes circle { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
text { | |
pointer-events: none; | |
font-size: .5rem; | |
} | |
</style> | |
</head> | |
<body> | |
<svg width="960" height="600"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<!-- <form> | |
<h3> Link threshold 0 <input type="range" id="thersholdSlider" name="points" value = 0 min="0" max="10" onchange="threshold(this.value)"> 10 </h3> | |
</form> | |
--> | |
<script src="main.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const THRESHOLD = .4; | |
const THRESHOLD_1 = .45; | |
const THRESHOLD_2 = .5; | |
// DATA | |
var graph; | |
// NODES | |
var svg = d3.select("svg"); | |
var width = +svg.attr("width"); | |
var height = +svg.attr("height"); | |
// SCALES | |
var dashScale = d3.scaleQuantize().domain([THRESHOLD_1, THRESHOLD_2, 1]).range(["5,5","5,10","5,20"]); | |
var opacityScale = d3.scaleLinear().range([0.1, 1]); | |
var color = d3.scaleOrdinal(d3.schemeCategory20); | |
var radiusScale = d3.scaleLog(); | |
// PHYSICS | |
var simulation = d3.forceSimulation() | |
.force("link", d3.forceLink()) | |
.force("charge", d3.forceManyBody().strength((d)=> -d.tweets)) | |
.force("center", d3.forceCenter(width / 2, height / 2)); | |
var link, node, text; | |
// FUNCTIONS | |
function onLoaded(error, data) { | |
if (error) throw error; | |
graph = data; | |
radiusScale.domain(d3.extent(data.nodes, (d)=>d.tweets)).range([10,100]) | |
opacityScale.domain(d3.extent(data.edges, (d)=>d.weight)) | |
var linkData = graph.edges.filter((d)=> d.weight> THRESHOLD) | |
link = svg.append("g") | |
.attr("class", "links") | |
.selectAll("line") | |
.data(linkData) | |
.enter() | |
.append("line") | |
.style("stroke-opacity", (d)=> (d.weight > THRESHOLD_1 ? 2 : 1)) | |
.attr("stroke-width", (d)=> (d.weight > THRESHOLD_1 ? 2 : 1)) | |
.attr("stroke-dasharray", (d)=> (d.weight <= THRESHOLD_1 ? dashScale(d.weight) : 0 )) | |
node = svg.append("g") | |
.attr("class", "nodes") | |
.selectAll("circle") | |
.data(graph.nodes) | |
.enter() | |
.append("circle") | |
.attr("r", (d)=> (radiusScale(d.tweets))) | |
.attr("fill", "red") | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended)); | |
text = svg | |
.selectAll("text") | |
.data(graph.nodes) | |
.enter() | |
.append("text") | |
.text((d)=> d.name); | |
simulation | |
.nodes(graph.nodes) | |
.on("tick", ticked); | |
simulation.force("link") | |
.links(linkData) | |
.distance(function(d){return d.weight * 400}) | |
.strength(1); | |
} | |
function ticked() { | |
link | |
.attr("x1", (d)=> d.source.x) | |
.attr("y1", (d)=> d.source.y) | |
.attr("x2", (d)=> d.target.x) | |
.attr("y2", (d)=> d.target.y) | |
node | |
.attr("cx", (d)=> d.x) | |
.attr("cy", (d)=> d.y) | |
text | |
.attr("x", (d)=>d.x) | |
.attr("y", (d)=>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; | |
} | |
// START | |
d3.json("politicians_graph.json", onLoaded) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"edges": [ | |
{ | |
"source": 0, | |
"target": 1, | |
"weight": 0.4716256819304776 | |
}, | |
{ | |
"source": 0, | |
"target": 2, | |
"weight": 0.34253341531449183 | |
}, | |
{ | |
"source": 0, | |
"target": 3, | |
"weight": 0.5850021012364067 | |
}, | |
{ | |
"source": 0, | |
"target": 4, | |
"weight": 0.5167036278591745 | |
}, | |
{ | |
"source": 0, | |
"target": 5, | |
"weight": 0.35199505553715504 | |
}, | |
{ | |
"source": 1, | |
"target": 2, | |
"weight": 0.3730431400620412 | |
}, | |
{ | |
"source": 1, | |
"target": 3, | |
"weight": 0.5130490641708315 | |
}, | |
{ | |
"source": 1, | |
"target": 4, | |
"weight": 0.3745925800899256 | |
}, | |
{ | |
"source": 1, | |
"target": 5, | |
"weight": 0.35017147850622254 | |
}, | |
{ | |
"source": 2, | |
"target": 3, | |
"weight": 0.4123832526511902 | |
}, | |
{ | |
"source": 2, | |
"target": 4, | |
"weight": 0.31764094814496674 | |
}, | |
{ | |
"source": 2, | |
"target": 5, | |
"weight": 0.4385062602095448 | |
}, | |
{ | |
"source": 3, | |
"target": 4, | |
"weight": 0.4581154353395781 | |
}, | |
{ | |
"source": 3, | |
"target": 5, | |
"weight": 0.34931304656705653 | |
}, | |
{ | |
"source": 4, | |
"target": 5, | |
"weight": 0.3168755582230677 | |
} | |
], | |
"nodes": [ | |
{ | |
"name": "@civati", | |
"tweets": 6785 | |
}, | |
{ | |
"name": "@giorgiameloni", | |
"tweets": 678 | |
}, | |
{ | |
"name": "@giulianopisapia", | |
"tweets": 39 | |
}, | |
{ | |
"name": "@meb", | |
"tweets": 255 | |
}, | |
{ | |
"name": "@nichivendola", | |
"tweets": 153 | |
}, | |
{ | |
"name": "@pbersani", | |
"tweets": 78 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment