This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bilevelPartition.js
170 lines (133 loc) · 5.03 KB
/
bilevelPartition.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2013-present The TagSpaces Authors.
* Use of this source code is governed by the MIT license which can be found in the LICENSE.txt file. */
define(function(require, exports, module) {
"use strict";
console.log("Loading Quantified Self");
var TSCORE = require("tscore");
require("d3");
// Based on http://bl.ocks.org/mbostock/5944371
var drawPartition = function(svg, treeData) {
var margin = {top: 350, right: 480, bottom: 350, left: 480},
radius = Math.min(margin.top, margin.right, margin.bottom, margin.left);
var hue = d3.scale.category10();
var luminance = d3.scale.sqrt()
.domain([0, 1e6])
.clamp(true)
.range([90, 20]);
svg.append("g")
.attr("transform", "translate(.5,.5)");
var partition = d3.layout.partition()
.sort(function(a, b) { return d3.ascending(a.name, b.name); })
.size([2 * Math.PI, radius]);
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx; })
.innerRadius(function(d) { return radius / 3 * d.depth; })
.outerRadius(function(d) { return radius / 3 * (d.depth + 1); });
// Compute the initial layout on the entire tree to sum sizes.
// Also compute the full name and fill color for each node.
partition
.value(function(d) { return d.size; })
.nodes(treeData)
.forEach(function(d) { d.sum = d.value; d.key = key(d); d.fill = fill(d); });
// Now redefine the value function to use the previously-computed sum.
partition
.children(function(d, depth) { return depth < 2 ? d.children : null; })
.value(function(d) { return d.sum; });
var center = svg.append("circle")
.attr("r", radius / 3)
.on("click", zoomOut);
center.append("title")
.text("zoom out");
var path = svg.selectAll("path")
.data(partition.nodes(treeData).slice(1))
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) { return d.fill; })
.each(function(d) { this._current = updateArc(d); })
.on("click", zoomIn);
function zoomIn(p) {
if (p.depth > 1) {
p = p.parent;
}
if (!p.children) {
return;
}
zoom(p, p);
}
function zoomOut(p) {
if (!p.parent) {
return;
}
zoom(p.parent, p);
}
// Zoom to the specified new root.
function zoom(root, p) {
if (document.documentElement.__transition__) {
return;
}
// Stash the original root dimensions for transitions.
var angle1 = d3.scale.linear()
.domain([0, 2 * Math.PI])
.range([root.x, root.x + root.dx]);
function insideArc(d) {
return p.key > d.key ? {depth: d.depth - 1, x: 0, dx: 0} : p.key < d.key ? {depth: d.depth - 1, x: 2 * Math.PI, dx: 0} : {depth: 0, x: 0, dx: 2 * Math.PI};
}
function outsideArc(d) {
return {depth: d.depth + 1, x: angle1(d.x), dx: angle1(d.x + d.dx) - angle1(d.x)};
}
// When zooming in, arcs enter from the outside and exit to the inside.
// When zooming out, arcs enter from the inside and exit to the outside.
var enterArc = root === p ? outsideArc : insideArc,
exitArc = root === p ? insideArc : outsideArc;
center.datum(root);
d3.transition().duration(750).each(function() {
path = path.data(partition.nodes(root).slice(1), function(d) { return d.key; });
path.transition()
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });
path.enter().append("path")
.style("fill-opacity", 0)
.style("fill", function(d) { return d.fill; })
.on("click", zoomIn)
.each(function(d) { this._current = enterArc(d); })
.transition()
.style("fill-opacity", 1)
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });
path.exit().transition()
.style("fill-opacity", 0)
.attrTween("d", function(d) { return arcTween.call(this, exitArc(d)); })
.remove();
});
}
function key(d) {
var k = [], p = d;
while (p.depth) {
k.push(p.name);
p = p.parent;
}
return k.reverse().join(".");
}
function fill(d) {
var p = d;
while (p.depth > 1) {
p = p.parent;
}
var c = d3.lab(hue(p.name));
c.l = luminance(d.sum);
return c;
}
function arcTween(b) {
var i = d3.interpolate(this._current, b);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
function updateArc(d) {
return {depth: d.depth, x: d.x, dx: d.dx};
}
d3.select(self.frameElement).style("height", margin.top + margin.bottom + "px");
};
// Methods
exports.drawPartition = drawPartition;
});