Skip to content

Instantly share code, notes, and snippets.

@samatt
Created February 22, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samatt/0e98c6c5a48a7c0f89bcf7016f8be0bf to your computer and use it in GitHub Desktop.
Save samatt/0e98c6c5a48a7c0f89bcf7016f8be0bf to your computer and use it in GitHub Desktop.
NEW THING
{
"nodes": [
{
"value": 8,
"type": "person",
"id": "Jeff Sessions"
},
{
"value": 7,
"type": "person",
"id": "Scott Pruitt"
},
{
"value": 8,
"type": "person",
"id": "Wilbur Ross"
},
{
"value": 6,
"type": "person",
"id": "Tom Price"
},
{
"value": 8,
"type": "person",
"id": "Ryan Zinke"
},
{
"value": 8,
"type": "person",
"id": "Ben Carson"
},
{
"value": 7,
"type": "person",
"id": "Kellyanne Conway"
},
{
"value": 6,
"type": "person",
"id": "Nikki Haley"
},
{
"value": 8,
"type": "person",
"id": "Rick Perry"
},
{
"value": 12,
"type": "person",
"id": "Mick Mulvaney"
},
{
"value": 7,
"type": "person",
"id": "Mike Pompeo"
},
{
"value": 7,
"type": "person",
"id": "Betsy DeVos"
},
{
"value": 6,
"type": "person",
"id": "Rex Tillerson"
},
{
"value": 7,
"type": "conspiracy",
"id": "sharia law taking over u.s."
},
{
"value": 6,
"type": "conspiracy",
"id": "member of actual conspiracy--fun twist, but not sure how to represent graphically. jim, surya, any ideas?"
},
{
"value": 6,
"type": "conspiracy",
"id": "pyramids built to store grain"
},
{
"value": 6,
"type": "conspiracy",
"id": "obama invited immigrant minors into the u.s."
},
{
"value": 13,
"type": "conspiracy",
"id": "climate change fake"
},
{
"value": 6,
"type": "conspiracy",
"id": "unproven medical treatments"
},
{
"value": 6,
"type": "conspiracy",
"id": "boko haram kids"
},
{
"value": 6,
"type": "conspiracy",
"id": "gay marriage is a communist plot"
},
{
"value": 6,
"type": "conspiracy",
"id": "conversion therapy"
},
{
"value": 6,
"type": "conspiracy",
"id": "washington think tank conspiracy"
},
{
"value": 6,
"type": "conspiracy",
"id": "moscow conspiracy"
},
{
"value": 6,
"type": "conspiracy",
"id": "saudia arabia sheltered bin laden after 9/11"
},
{
"value": 6,
"type": "conspiracy",
"id": "u.s. funds went to joseph kony"
},
{
"value": 6,
"type": "conspiracy",
"id": "benghazi and sidney blumenthal"
},
{
"value": 8,
"type": "conspiracy",
"id": "vaccine conspiracies"
},
{
"value": 6,
"type": "conspiracy",
"id": "alternative currency"
},
{
"value": 7,
"type": "conspiracy",
"id": "voter fraud real"
},
{
"value": 7,
"type": "conspiracy",
"id": "evolution not what it appears"
},
{
"value": 7,
"type": "conspiracy",
"id": "birtherism"
},
{
"value": 6,
"type": "conspiracy",
"id": "fake planned parenthood videos"
}
],
"links": [
{
"target": "Jeff Sessions",
"value": 1,
"source": "sharia law taking over u.s."
},
{
"target": "Kellyanne Conway",
"value": 1,
"source": "sharia law taking over u.s."
},
{
"target": "Wilbur Ross",
"value": 1,
"source": "member of actual conspiracy--fun twist, but not sure how to represent graphically. jim, surya, any ideas?"
},
{
"target": "Wilbur Ross",
"value": 1,
"source": "pyramids built to store grain"
},
{
"target": "Rick Perry",
"value": 1,
"source": "obama invited immigrant minors into the u.s."
},
{
"target": "Jeff Sessions",
"value": 1,
"source": "climate change fake"
},
{
"target": "Scott Pruitt",
"value": 1,
"source": "climate change fake"
},
{
"target": "Ryan Zinke",
"value": 1,
"source": "climate change fake"
},
{
"target": "Kellyanne Conway",
"value": 1,
"source": "climate change fake"
},
{
"target": "Nikki Haley",
"value": 1,
"source": "climate change fake"
},
{
"target": "Rick Perry",
"value": 1,
"source": "climate change fake"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "climate change fake"
},
{
"target": "Rex Tillerson",
"value": 1,
"source": "climate change fake"
},
{
"target": "Betsy DeVos",
"value": 1,
"source": "unproven medical treatments"
},
{
"target": "Ryan Zinke",
"value": 1,
"source": "boko haram kids"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "gay marriage is a communist plot"
},
{
"target": "Betsy DeVos",
"value": 1,
"source": "conversion therapy"
},
{
"target": "Wilbur Ross",
"value": 1,
"source": "washington think tank conspiracy"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "moscow conspiracy"
},
{
"target": "Scott Pruitt",
"value": 1,
"source": "saudia arabia sheltered bin laden after 9/11"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "u.s. funds went to joseph kony"
},
{
"target": "Mike Pompeo",
"value": 1,
"source": "benghazi and sidney blumenthal"
},
{
"target": "Tom Price",
"value": 1,
"source": "vaccine conspiracies"
},
{
"target": "Ryan Zinke",
"value": 1,
"source": "vaccine conspiracies"
},
{
"target": "Ben Carson",
"value": 1,
"source": "vaccine conspiracies"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "alternative currency"
},
{
"target": "Jeff Sessions",
"value": 1,
"source": "voter fraud real"
},
{
"target": "Ben Carson",
"value": 1,
"source": "voter fraud real"
},
{
"target": "Rick Perry",
"value": 1,
"source": "evolution not what it appears"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "evolution not what it appears"
},
{
"target": "Ben Carson",
"value": 1,
"source": "birtherism"
},
{
"target": "Mike Pompeo",
"value": 1,
"source": "birtherism"
},
{
"target": "Mick Mulvaney",
"value": 1,
"source": "fake planned parenthood videos"
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.label {
/*z-index: -100;*/
font-size: 0.6em;
}
.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 div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
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("conspiracies.json", function(error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", function(d) {return d.value})
.attr("fill", function(d) { return d.type === 'person' ?'red':'blue' })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.name + "<br/>" )
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
// node.append("title")
// .text(function(d) { return d.id; });
var texts = svg.selectAll("text.label")
.data(graph.nodes)
.enter().append("text")
.attr("class", "label")
.attr("fill", "black")
.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 = Math.max(d.value, Math.min(width - d.value, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(d.value, Math.min(height - d.value, d.y)); });
texts.attr("transform", function(d) {
return "translate(" + Math.max(d.value, Math.min(width - d.value, d.x))+ "," + Math.max(d.value, Math.min(height - d.value, 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment