Skip to content

Instantly share code, notes, and snippets.

@tangert tangert/data.json
Last active Nov 13, 2018

Embed
What would you like to do?
MMLA Transcript Analysis
{
"name": "Transcript data",
"children": [
{
"name": "Cluster 1",
"children": [
{"name": "tilted", "size": 0.767 },
{"name": "towards", "size": 0.199 },
{"name": "away", "size": 0.186 },
{"name": "north", "size": 0.098 },
{"name": "part", "size": 0.084 },
{"name": "guess", "size": 0.077 },
{"name": "closer", "size": 0.044 },
{"name": "warmer", "size": 0.042 },
{"name": "sun", "size": 0.03 },
{"name": "father", "size": 0.017 }
]
},
{
"name": "Cluster 2",
"children": [
{"name": "earth", "size": 0.4},
{"name": "spinning", "size": 0.366},
{"name": "china", "size": 0.295},
{"name": "united", "size": 0.202},
{"name": "spins", "size": 0.2},
{"name": "time", "size": 0.198},
{"name": "doesn", "size": 0.193},
{"name": "long", "size": 0.154},
{"name": "takes", "size": 0.138},
{"name": "axis", "size": 0.121}
]
},
{
"name": "Cluster 3",
"children": [
{"name": "hemisphere", "size": 0.603},
{"name": "northern", "size": 0.522},
{"name": "sunlight", "size": 0.278},
{"name": "southern", "size": 0.229},
{"name": "direct", "size": 0.154},
{"name": "equator", "size": 0.146},
{"name": "colder", "size": 0.119},
{"name": "shorter", "size": 0.111},
{"name": "turning", "size": 0.106},
{"name": "facing", "size": 0.106}
]
},
{
"name": "Cluster 4",
"children": [
{"name": "side", "size": 0.722},
{"name": "fall", "size": 0.273},
{"name": "spring", "size": 0.271},
{"name": "draw", "size": 0.107},
{"name": "sort", "size": 0.101},
{"name": "half", "size": 0.091},
{"name": "facing", "size": 0.091},
{"name": "earth", "size": 0.085},
{"name": "part", "size": 0.068},
{"name": "united", "size": 0.052}
]
},
{
"name": "Cluster 5",
"children": [
{"name": "directly", "size": 0.382},
{"name": "rays", "size": 0.293},
{"name": "hitting", "size": 0.236},
{"name": "south", "size": 0.219},
{"name": "hit", "size": 0.212},
{"name": "north", "size": 0.197},
{"name": "angle", "size": 0.194},
{"name": "light", "size": 0.188},
{"name": "america", "size": 0.173},
{"name": "chicago", "size": 0.163}
]
},
{
"name": "Cluster 6",
"children": [
{"name": "day", "size": 0.415},
{"name": "moon", "size": 0.398},
{"name": "night", "size": 0.377},
{"name": "rotates", "size": 0.178},
{"name": "make", "size": 0.089},
{"name": "turn", "size": 0.088},
{"name": "mind", "size": 0.077},
{"name": "rotating", "size": 0.068},
{"name": "planet", "size": 0.067},
{"name": "daytime", "size": 0.063}
]
},
{
"name": "Cluster 7",
"children": [
{"name": "farther", "size": 0.413},
{"name": "closer", "size": 0.403},
{"name": "away", "size": 0.379},
{"name": "point", "size": 0.222},
{"name": "colder", "size": 0.216},
{"name": "cold", "size": 0.118},
{"name": "snow", "size": 0.107},
{"name": "sun", "size": 0.103},
{"name": "far", "size": 0.094},
{"name": "space", "size": 0.089}
]
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
cursor: pointer;
}
.node:hover {
stroke: #000;
stroke-width: 1.5px;
}
.node--leaf {
fill: white;
}
.label {
font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
text-anchor: middle;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
}
.label,
.node--root,
.node--leaf {
pointer-events: none;
}
</style>
<svg width="750" height="750"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = 20,
diameter = +svg.attr("width"),
g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
var color = d3.scaleLinear()
.domain([-1, 5])
.range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
.interpolate(d3.interpolateHcl);
var pack = d3.pack()
.size([diameter - margin, diameter - margin])
.padding(2);
d3.json("data.json", function(error, root) {
if (error) throw error;
root = d3.hierarchy(root)
.sum(function(d) { return d.size; })
.sort(function(a, b) { return b.value - a.value; });
var focus = root,
nodes = pack(root).descendants(),
view;
var circle = g.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
.style("fill", function(d) { return d.children ? color(d.depth) : null; })
.on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
var text = g.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("class", "label")
.style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
.style("display", function(d) { return d.parent === root ? "inline" : "none"; })
.text(function(d) { return d.data.name; });
var node = g.selectAll("circle,text");
svg
.style("background", color(-1))
.on("click", function() { zoom(root); });
zoomTo([root.x, root.y, root.r * 2 + margin]);
function zoom(d) {
var focus0 = focus; focus = d;
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", function(d) {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
return function(t) { zoomTo(i(t)); };
});
transition.selectAll("text")
.filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
.on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
.on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = diameter / v[2]; view = v;
node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
circle.attr("r", function(d) { return d.r * k; });
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.