learndb/public/map.html

345 wiersze
9.6 KiB
HTML
Czysty Wina Historia

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>TreeMap</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
body {
margin: 0px;
padding: 0px;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
#chart {
margin: 0px;
padding: 0px;
background: #E0E7FF;
}
text {
cursor: pointer;
fill: #171717;
}
.grandparent text {
font-weight: bold;
}
rect {
fill: none;
stroke: #fff;
}
rect.parent,
.grandparent rect {
stroke-width: 2px;
}
.grandparent rect {
fill: #F68787;
}
.grandparent:hover rect {
fill: #FB3569;
}
.children rect.parent,
.grandparent rect {
cursor: pointer;
}
.leaf rect.parent {
cursor: pointer;
fill: #B7FBFF;
}
.leaf rect.parent:hover {
cursor: pointer;
fill: #51EAEA;
}
.children rect.parent {
fill: #F1EB9A;
fill-opacity: .5;
}
.children:hover rect.child {
fill: #F8A978;
}
</style>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
</head>
<body>
<div style="text-align: right;">
<a href="https://github.com/learn-awesome/learndb" target="_blank" style="text-decoration:underline; padding: 2px 3px; font-weight: 700;">Help us improve this taxonomy</a>
</div>
<p id="chart"></p>
<script type="text/javascript">
function hierarchy(topic_array, parent_name){
// this is different from TopicMasonryGrid because this needs a specific object structure down to all leafs, not a map with just 2 levels
// every node needs id, name. Leaf nodes need a value like 100. Non-leafs need a children[].
// see: https://github.com/mekarpeles/math.mx/blob/master/static/data/simple.json
const nest = (items, id) =>
items
.filter(item => item.parent_name === id)
.map(item => ({ ...item, children: nest(items, item.name), value: 100 }))
.map(item => {
return item.children.length == 0 ?
{id: item.name, name: item.name, value: 100} :
{id: item.name, name: item.name, children: item.children}
});
let topnodes = nest(topic_array, null); // includes childless nodes
// put childless nodes under misc
let misc = {id: 'misc', name: 'Misc', children: topnodes.filter(t => t.value).slice(0,15)};
return {name: "", children: [...topnodes.filter(t => t.children), misc]};
}
d3.json("/alltopics.json", function(alltopics){
// console.log({alltopics});
var root = hierarchy(alltopics, null);
// console.log({root});
initialize(root);
accumulate(root);
layout(root);
display(root);
});
var margin = {top: 30, right: 0, bottom: 0, left: 0},
wh = browserDimensions(),
width = wh[0] - (2 * margin.left) - (2*margin.right),
height = wh[1] - (2 * margin.top - margin.bottom),
formatNumber = d3.format(",d"),
transitioning;
var x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([0, height]);
var treemap = d3.layout.treemap()
.children(function(d, depth) { return depth ? null : d._children; })
.sort(function(a, b) { return a.value - b.value; })
.ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
.round(false);
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.bottom + margin.top)
.style("margin-left", -margin.left + "px")
.style("margin.right", -margin.right + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.style("shape-rendering", "crispEdges");
var grandparent = svg.append("g")
.attr("class", "grandparent");
grandparent.append("rect")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top);
grandparent.append("text")
.attr("x", 6)
.attr("y", 6 - margin.top)
.attr("dy", ".75em");
function initialize(root) {
root.x = root.y = 0;
root.dx = width;
root.dy = height;
root.depth = 0;
}
// Aggregate the values for internal nodes. This is normally done by the
// treemap layout, but not here because of our custom implementation.
// We also take a snapshot of the original children (_children) to avoid
// the children being overwritten when when layout is computed.
function accumulate(d) {
return (d._children = d.children)
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value;
}
// Compute the treemap layout recursively such that each group of siblings
// uses the same size (1×1) rather than the dimensions of the parent cell.
// This optimizes the layout for the current zoom state. Note that a wrapper
// object is created for the parent node for each group of siblings so that
// the parents dimensions are not discarded as we recurse. Since each group
// of sibling was laid out in 1×1, we must rescale to fit using absolute
// coordinates. This lets us use a viewport to zoom.
function layout(d) {
if (d._children) {
treemap.nodes({_children: d._children});
d._children.forEach(function(c) {
c.x = d.x + c.x * d.dx;
c.y = d.y + c.y * d.dy;
c.dx *= d.dx;
c.dy *= d.dy;
c.parent = d;
layout(c);
});
}
}
function display(d) {
grandparent
.datum(d.parent)
.on("click", transition)
.select("text")
.text(name(d));
var g1 = svg.insert("g", ".grandparent")
.datum(d)
.attr("class", "depth");
var g = g1.selectAll("g")
.data(d._children)
.enter().append("g");
g.filter(function(d) { return d._children; })
.classed("children", true)
.on("click", transition);
g.filter(function(d) { return !d._children; })
.classed("leaf", true)
.on("click",(n) => { window.parent.location.href = "/#/topic/" + n.name;});
g.filter(function(d) { return d._children; }).selectAll(".child")
.data(function(d) { return d._children || [d]; })
.enter().append("rect")
.attr("class", "child")
.call(rect);
g.append("rect")
.attr("class", "parent")
.call(rect)
.append("title")
.text(function(d) { return formatNumber(d.value); });
g.append("text")
.attr("dy", ".75em")
.on("click", (n) => { window.parent.location.href = "/#/topic/" + n.name;})
.text(function(d) { return d.name.split('/').reverse()[0].split('{')[0].split('(')[0]
.split('[')[0]; })
.call(text);
function transition(d) {
if (transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(750),
t2 = g2.transition().duration(750);
// Update the domain only after entering new elements.
x.domain([d.x, d.x + d.dx]);
y.domain([d.y, d.y + d.dy]);
// Enable anti-aliasing during the transition.
svg.style("shape-rendering", null);
// Draw child nodes on top of parent nodes.
svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; });
// Fade-in entering text.
g2.selectAll("text").style("fill-opacity", 0);
// Transition to the new view.
t1.selectAll("text").call(text).style("fill-opacity", 0);
t2.selectAll("text").call(text).style("fill-opacity", 1);
t1.selectAll("rect").call(rect);
t2.selectAll("rect").call(rect);
// Remove the old node when the transition is finished.
t1.remove().each("end", function() {
svg.style("shape-rendering", "crispEdges");
transitioning = false;
});
}
return g;
}
function text(text) {
text.attr("x", function(d) { return x(d.x) + 10; })
.attr("y", function(d) { return y(d.y) + 10; })
.attr("text-decoration","underline");
}
function rect(rect) {
rect.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y); })
.attr("width", function(d) { return x(d.x + d.dx) - x(d.x); })
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); });
}
function name(d) {
return d.parent
? name(d.parent) + " → " + d.name
: d.name;
}
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
function browserDimensions() {
var w = 0, h = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
w = window.innerWidth;
h = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
w = document.body.clientWidth;
h = document.body.clientHeight;
}
return [w, h];
}
</script>
</body>
</html>