kopia lustrzana https://github.com/learn-awesome/learndb
Use pancake for zoomable treemap
rodzic
3b920350b6
commit
29c40f282b
|
@ -10,7 +10,9 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rgossiaux/svelte-headlessui": "^1.0.2",
|
"@rgossiaux/svelte-headlessui": "^1.0.2",
|
||||||
"@rgossiaux/svelte-heroicons": "^0.1.2",
|
"@rgossiaux/svelte-heroicons": "^0.1.2",
|
||||||
|
"@sveltejs/pancake": "^0.0.18",
|
||||||
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
||||||
|
"d3-hierarchy": "^3.1.2",
|
||||||
"marked": "^4.2.4",
|
"marked": "^4.2.4",
|
||||||
"svelte": "^3.55.0",
|
"svelte": "^3.55.0",
|
||||||
"vite": "^4.0.3"
|
"vite": "^4.0.3"
|
||||||
|
@ -454,6 +456,15 @@
|
||||||
"svelte": "^3.44.0"
|
"svelte": "^3.44.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@sveltejs/pancake": {
|
||||||
|
"version": "0.0.18",
|
||||||
|
"resolved": "https://registry.npmjs.org/@sveltejs/pancake/-/pancake-0.0.18.tgz",
|
||||||
|
"integrity": "sha512-vnpKn+sHE0yvw/6x4juF9EoDS8iYC5JNIVtRgclA37rfxdnIpvbVxStiZ5/gWncjERN4jtSpTAplS1zCARCBXA==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"yootils": "0.0.16"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@sveltejs/vite-plugin-svelte": {
|
"node_modules/@sveltejs/vite-plugin-svelte": {
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte/-/vite-plugin-svelte-2.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte/-/vite-plugin-svelte-2.0.2.tgz",
|
||||||
|
@ -525,6 +536,15 @@
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"peer": true
|
"peer": true
|
||||||
},
|
},
|
||||||
|
"node_modules/d3-hierarchy": {
|
||||||
|
"version": "3.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz",
|
||||||
|
"integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==",
|
||||||
|
"dev": true,
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/debug": {
|
"node_modules/debug": {
|
||||||
"version": "4.3.4",
|
"version": "4.3.4",
|
||||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
||||||
|
@ -887,6 +907,12 @@
|
||||||
"optional": true
|
"optional": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/yootils": {
|
||||||
|
"version": "0.0.16",
|
||||||
|
"resolved": "https://registry.npmjs.org/yootils/-/yootils-0.0.16.tgz",
|
||||||
|
"integrity": "sha512-aIVecm5ucOzwhtKbl0zkfg0ZSOUR9c2da0k8cIc9umjjzkvVCWUUX/UHZ1CLPsv4wmJLqt0aWeLB7p9n9JDwYQ==",
|
||||||
|
"dev": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1117,6 +1143,15 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {}
|
"requires": {}
|
||||||
},
|
},
|
||||||
|
"@sveltejs/pancake": {
|
||||||
|
"version": "0.0.18",
|
||||||
|
"resolved": "https://registry.npmjs.org/@sveltejs/pancake/-/pancake-0.0.18.tgz",
|
||||||
|
"integrity": "sha512-vnpKn+sHE0yvw/6x4juF9EoDS8iYC5JNIVtRgclA37rfxdnIpvbVxStiZ5/gWncjERN4jtSpTAplS1zCARCBXA==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"yootils": "0.0.16"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@sveltejs/vite-plugin-svelte": {
|
"@sveltejs/vite-plugin-svelte": {
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte/-/vite-plugin-svelte-2.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/@sveltejs/vite-plugin-svelte/-/vite-plugin-svelte-2.0.2.tgz",
|
||||||
|
@ -1174,6 +1209,12 @@
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"peer": true
|
"peer": true
|
||||||
},
|
},
|
||||||
|
"d3-hierarchy": {
|
||||||
|
"version": "3.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz",
|
||||||
|
"integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"debug": {
|
"debug": {
|
||||||
"version": "4.3.4",
|
"version": "4.3.4",
|
||||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
||||||
|
@ -1397,6 +1438,12 @@
|
||||||
"integrity": "sha512-fanAXjSaf9xXtOOeno8wZXIhgia+CZury481LsDaV++lSvcU2R9Ch2bPh3PYFyoHW+w9LqAeYRISVQjUIew14g==",
|
"integrity": "sha512-fanAXjSaf9xXtOOeno8wZXIhgia+CZury481LsDaV++lSvcU2R9Ch2bPh3PYFyoHW+w9LqAeYRISVQjUIew14g==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {}
|
"requires": {}
|
||||||
|
},
|
||||||
|
"yootils": {
|
||||||
|
"version": "0.0.16",
|
||||||
|
"resolved": "https://registry.npmjs.org/yootils/-/yootils-0.0.16.tgz",
|
||||||
|
"integrity": "sha512-aIVecm5ucOzwhtKbl0zkfg0ZSOUR9c2da0k8cIc9umjjzkvVCWUUX/UHZ1CLPsv4wmJLqt0aWeLB7p9n9JDwYQ==",
|
||||||
|
"dev": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,10 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rgossiaux/svelte-headlessui": "^1.0.2",
|
"@rgossiaux/svelte-headlessui": "^1.0.2",
|
||||||
"@rgossiaux/svelte-heroicons": "^0.1.2",
|
"@rgossiaux/svelte-heroicons": "^0.1.2",
|
||||||
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
"@sveltejs/pancake": "^0.0.18",
|
||||||
"marked": "^4.2.4",
|
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
||||||
|
"d3-hierarchy": "^3.1.2",
|
||||||
|
"marked": "^4.2.4",
|
||||||
"svelte": "^3.55.0",
|
"svelte": "^3.55.0",
|
||||||
"vite": "^4.0.3"
|
"vite": "^4.0.3"
|
||||||
}
|
}
|
||||||
|
|
345
public/map.html
345
public/map.html
|
@ -1,345 +0,0 @@
|
||||||
<!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 parent’s 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>
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<script>
|
||||||
|
import TreemapNode from './TreemapNode.svelte';
|
||||||
|
|
||||||
|
export let root;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<pancake-treemap>
|
||||||
|
<TreemapNode node={root} let:node>
|
||||||
|
<slot {node}></slot>
|
||||||
|
</TreemapNode>
|
||||||
|
</pancake-treemap>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
pancake-treemap {
|
||||||
|
position: absolute;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
</style>
|
|
@ -1 +1,181 @@
|
||||||
<iframe title="treemap" src="/map.html" class="w-full h-screen"></iframe>
|
<script>
|
||||||
|
import * as Pancake from '@sveltejs/pancake';
|
||||||
|
import * as d3 from 'd3-hierarchy';
|
||||||
|
import { tweened } from 'svelte/motion';
|
||||||
|
import * as eases from 'svelte/easing';
|
||||||
|
import { fade } from 'svelte/transition';
|
||||||
|
import * as yootils from 'yootils';
|
||||||
|
import PancakeTreemap from './PancakeTreemap.svelte';
|
||||||
|
import topics from '../db/topics.json';
|
||||||
|
|
||||||
|
const treemap = d3.treemap();
|
||||||
|
|
||||||
|
const build_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]};
|
||||||
|
}
|
||||||
|
|
||||||
|
const hierarchy = d3.hierarchy(build_hierarchy(topics, null))
|
||||||
|
.sum(d => d.value)
|
||||||
|
.sort((a, b) => b.value - a.value)
|
||||||
|
|
||||||
|
const root = treemap(hierarchy);
|
||||||
|
|
||||||
|
let selected = root;
|
||||||
|
|
||||||
|
const select = node => {
|
||||||
|
while (node.parent && node.parent !== selected) {
|
||||||
|
node = node.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node && node.children) {
|
||||||
|
selected = node;
|
||||||
|
} else {
|
||||||
|
window.location.href = "/#/topic/" + node.data.name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const breadcrumbs = node => {
|
||||||
|
const crumbs = [];
|
||||||
|
while (node) {
|
||||||
|
crumbs.unshift(node.data.name)
|
||||||
|
node = node.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return crumbs.join('/');
|
||||||
|
};
|
||||||
|
|
||||||
|
const extents = tweened(undefined, {
|
||||||
|
easing: eases.cubicOut,
|
||||||
|
duration: 600
|
||||||
|
});
|
||||||
|
|
||||||
|
const is_visible = (a, b) => {
|
||||||
|
while (b) {
|
||||||
|
if (a.parent === b) return true;
|
||||||
|
b = b.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
$: $extents = {
|
||||||
|
x1: selected.x0,
|
||||||
|
x2: selected.x1,
|
||||||
|
y1: selected.y1,
|
||||||
|
y2: selected.y0
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button class="breadcrumbs" disabled="{!selected.parent}" on:click="{() => selected = selected.parent}">
|
||||||
|
{breadcrumbs(selected)}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div class="chart">
|
||||||
|
<Pancake.Chart x1={$extents.x1} x2={$extents.x2} y1={$extents.y1} y2={$extents.y2}>
|
||||||
|
<PancakeTreemap {root} let:node>
|
||||||
|
{#if is_visible(node, selected)}
|
||||||
|
<div
|
||||||
|
transition:fade={{duration:400}}
|
||||||
|
class="node"
|
||||||
|
class:leaf={!node.children}
|
||||||
|
on:click="{() => select(node)}"
|
||||||
|
>
|
||||||
|
<div class="pancontents">
|
||||||
|
<strong on:click|stopPropagation={() => window.location.href = "/#/topic/" + node.data.name}>
|
||||||
|
{node.data.name.split('/').reverse()[0]}
|
||||||
|
</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</PancakeTreemap>
|
||||||
|
</Pancake.Chart>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.breadcrumbs {
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.3rem 0.4rem;
|
||||||
|
background-color: transparent;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: inherit;
|
||||||
|
text-align: left;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.breadcrumbs:disabled {
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chart {
|
||||||
|
width: calc(100% + 2px);
|
||||||
|
height: 900px;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0 -1px 36px -1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.node {
|
||||||
|
position: absolute;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
background-color: white;
|
||||||
|
overflow: hidden;
|
||||||
|
pointer-events: all;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pancontents {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
padding: 0.3rem 0.4rem;
|
||||||
|
border: 1px solid white;
|
||||||
|
background-color: hsl(240, 8%, 70%);
|
||||||
|
color: white;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pancontents:hover {
|
||||||
|
background-color: #F1EB9A;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
.node:not(.leaf) .pancontents {
|
||||||
|
background-color: hsl(240, 8%, 44%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.node:not(.leaf) .pancontents:hover {
|
||||||
|
background-color: #F1EB9A;
|
||||||
|
}
|
||||||
|
|
||||||
|
strong {
|
||||||
|
display: block;
|
||||||
|
font-size: 16px;
|
||||||
|
white-space: nowrap;
|
||||||
|
line-height: 1;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</style>
|
|
@ -0,0 +1,16 @@
|
||||||
|
<script>
|
||||||
|
import * as Pancake from '@sveltejs/pancake';
|
||||||
|
import { getContext } from 'svelte';
|
||||||
|
|
||||||
|
export let node;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Pancake.Box x1={node.x0} x2={node.x1} y1={node.y1} y2={node.y0}>
|
||||||
|
<slot {node}></slot>
|
||||||
|
</Pancake.Box>
|
||||||
|
|
||||||
|
{#each (node.children || []) as child}
|
||||||
|
<svelte:self node={child} let:node>
|
||||||
|
<slot {node}></slot>
|
||||||
|
</svelte:self>
|
||||||
|
{/each}
|
|
@ -1,17 +1,9 @@
|
||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import { svelte } from '@sveltejs/vite-plugin-svelte'
|
import { svelte } from '@sveltejs/vite-plugin-svelte'
|
||||||
import fs from 'fs';
|
|
||||||
|
|
||||||
function copyTopicJSONToPublic(){
|
|
||||||
// This is needed for map.html which needs to fetch all topics from server
|
|
||||||
console.log("Copying db/topics.json to public/alltopics.json ...");
|
|
||||||
fs.copyFileSync("db/topics.json", "public/alltopics.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [
|
plugins: [
|
||||||
copyTopicJSONToPublic(),
|
|
||||||
svelte()
|
svelte()
|
||||||
],
|
],
|
||||||
})
|
})
|
Ładowanie…
Reference in New Issue