learndb/static/map.html

341 wiersze
9.4 KiB
HTML
Czysty Zwykły widok Historia

2022-06-02 13:40:28 +00:00
<!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">
2022-06-03 10:49:01 +00:00
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
2022-06-02 13:40:28 +00:00
<style>
body {
margin: 0px;
padding: 0px;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
#chart {
margin: 0px;
padding: 0px;
background: #E0E7FF;
}
text {
2022-06-03 10:39:27 +00:00
cursor: pointer;
2022-06-03 13:35:57 +00:00
fill: #7a28cb;
2022-06-02 13:40:28 +00:00
}
.grandparent text {
font-weight: bold;
}
rect {
fill: none;
stroke: #fff;
}
rect.parent,
.grandparent rect {
stroke-width: 2px;
}
.grandparent rect {
2022-06-03 13:35:57 +00:00
fill: #f4b393;
2022-06-02 13:40:28 +00:00
}
.grandparent:hover rect {
2022-06-03 13:35:57 +00:00
fill: #f4d393;
2022-06-02 13:40:28 +00:00
}
.children rect.parent,
.grandparent rect {
cursor: pointer;
}
2022-06-03 10:39:27 +00:00
.leaf rect.parent {
cursor: pointer;
2022-06-03 13:35:57 +00:00
fill: #ffdbd1;
2022-06-03 10:39:27 +00:00
}
.leaf rect.parent:hover {
cursor: pointer;
2022-06-03 13:35:57 +00:00
fill: #ffcfd4;
2022-06-03 10:39:27 +00:00
}
2022-06-02 13:40:28 +00:00
.children rect.parent {
2022-06-03 13:35:57 +00:00
fill: #ceec97;
2022-06-02 13:40:28 +00:00
fill-opacity: .5;
}
.children:hover rect.child {
2022-06-03 13:35:57 +00:00
fill: #deec97;
2022-06-02 13:40:28 +00:00
}
</style>
2022-06-03 10:49:01 +00:00
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
2022-06-02 13:40:28 +00:00
</head>
<body>
<p id="chart"></p>
<script type="text/javascript">
function hierarchy(topic_array, parent_id){
// 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_id === id)
.map(item => ({ ...item, children: nest(items, item.name), value: 100 }))
.map(item => {
return item.children.length == 0 ?
2022-06-03 13:35:57 +00:00
{id: item.name, name: item.name, value: 100} :
{id: item.name, name: item.name, children: item.children}
2022-06-02 13:40:28 +00:00
});
let topnodes = nest(topic_array, ''); // includes childless nodes
// put childless nodes under misc
let misc = {id: 'misc', name: 'Misc', children: topnodes.filter(t => t.value).slice(0,15)};
2022-06-03 13:35:57 +00:00
return {name: "", children: [...topnodes.filter(t => t.children), misc]};
2022-06-02 13:40:28 +00:00
}
d3.json("/learn/topics.json?_shape=array&_size=5000", function(alltopics){
// console.log({alltopics});
var root = hierarchy(alltopics, "");
2022-06-02 20:12:54 +00:00
// console.log({root});
2022-06-02 13:40:28 +00:00
initialize(root);
accumulate(root);
layout(root);
display(root);
});
var margin = {top: 20, 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);
2022-06-03 10:39:27 +00:00
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")
2022-06-02 13:40:28 +00:00
.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")
2022-06-03 10:39:27 +00:00
.on("click", (n) => { window.parent.location.href = "/#/topic/" + n.name;})
2022-06-03 13:35:57 +00:00
.text(function(d) { return d.name.split('/').reverse()[0].split('{')[0].split('(')[0]
2022-06-02 13:40:28 +00:00
.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; })
2022-06-03 10:39:27 +00:00
.attr("y", function(d) { return y(d.y) + 10; })
.attr("text-decoration","underline");
2022-06-02 13:40:28 +00:00
}
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>