-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (112 loc) · 3.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const WEB = typeof XMLHttpRequest !== 'undefined';
const DEFAULT_TREEMAP = 'vast.json';
const d3 = WEB ? window.d3 : require('d3');
async function makeTreeMap(jsonFilePath = DEFAULT_TREEMAP) {
const margin = { top: 40, right: 10, bottom: 10, left: 10 };
const width = 960 - margin.left - margin.right;
const height = 500 - margin.top - margin.bottom;
const color = d3.scaleOrdinal().range(d3.schemeCategory20c);
const treemap = d3.treemap().size([width, height]);
const vastfile = await readJson(jsonFilePath);
verifyVastFile(vastfile);
const vast = vastfile.vast;
adjustNodeSize(vast);
if (WEB) console.log('vast:', vast);
const root = d3.hierarchy(vast, (d) => d.children)
.sum((d) => (d.size || 0) + (d.dsize || 0));
if (WEB) console.log('root:', root);
const tree = treemap(root);
const tmapfile = generateTmapFile(tree, vastfile.colors, jsonFilePath);
if (!WEB) {
const tmapjson = JSON.stringify(tmapfile, null, 2);
console.log(tmapjson);
return;
} else {
console.log('treemap:', tmapfile);
}
const div = d3.select("body").append("div")
.style("position", "relative")
.style("width", (width + margin.left + margin.right) + "px")
.style("height", (height + margin.top + margin.bottom) + "px")
.style("left", margin.left + "px")
.style("top", margin.top + "px");
const node = div.datum(root).selectAll(".node")
.data(tree.leaves())
.enter().append("div")
.attr("class", "node")
.style("left", (d) => d.x0 + "px")
.style("top", (d) => d.y0 + "px")
.style("width", (d) => Math.max(0, d.x1 - d.x0 - 1) + "px")
.style("height", (d) => Math.max(0, d.y1 - d.y0 - 1) + "px")
.style("background", (d) => color(d.parent.data.name))
.text((d) => d.data.name);
d3.selectAll("input").on("change", function change() {
const value = this.value === "count"
? (d) => { return d.size ? 1 : 0; }
: (d) => { return d.size; };
const newRoot = d3.hierarchy(vast, (d) => d.children)
.sum(value);
node.data(treemap(newRoot).leaves())
.transition()
.duration(1500)
.style("left", (d) => d.x0 + "px")
.style("top", (d) => d.y0 + "px")
.style("width", (d) => Math.max(0, d.x1 - d.x0 - 1) + "px")
.style("height", (d) => Math.max(0, d.y1 - d.y0 - 1) + "px")
});
}
// This is to hijack d3.hierarchy().sum() so node.value = data.size || sum(...).
function adjustNodeSize(t) {
let sum = 0;
for (let node of t.children || [])
sum += adjustNodeSize(node);
if (t.size > 0)
t.dsize = -sum;
if (sum > t.size)
console.warn('Node size is less than the sum of its children:', t.size, sum, t.name);
return t.size || sum;
}
function generateTmapFile(tree, colors, source) {
return {
type: 'tmap',
version: '1.0.0',
timestamp: new Date().toJSON(),
source,
colors,
treemap: cleanupTreemap(tree),
};
}
function verifyVastFile(json) {
if (json.format != 'vast' || json.version != '1.0.0')
throw new Error('Invalid VAST');
}
function cleanupTreemap(t) {
return {
x0: t.x0,
y0: t.y0,
x1: t.x1,
y1: t.y1,
data: {
name: t.data.name,
type: t.data.type,
ref: t.data.ref,
},
children: t.children &&
t.children.map(cleanupTreemap),
};
}
async function readJson(filepath) {
if (WEB) {
return await new Promise((resolve, reject) =>
d3.json(filepath, (err, res) =>
err ? reject(err) : resolve(res)))
} else {
const fs = require('fs');
const json = await new Promise((resolve, reject) =>
fs.readFile(filepath, 'utf8',
(err, res) => err ? reject(err) : resolve(res)));
return JSON.parse(json);
}
}
makeTreeMap(WEB ? undefined : process.argv[2]);