Skip to content

Instantly share code, notes, and snippets.

@thepuzzlemaster
Last active August 29, 2015 14:11
Show Gist options
  • Save thepuzzlemaster/ed21c60589d6c14918a0 to your computer and use it in GitHub Desktop.
Save thepuzzlemaster/ed21c60589d6c14918a0 to your computer and use it in GitHub Desktop.
Tier 8 #2
{"description":"Tier 8 #2","endpoint":"","display":"svg","public":true,"require":[{"name":"lodash","url":"https://raw.githubusercontent.com/lodash/lodash/master/dist/lodash.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/S8s8dCn.png","inline-console":false}
[
{
"id": 534678,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
89,
93,
7,
172
],
[
94,
149,
28,
213
],
[
85,
143,
23,
203
],
[
110,
150,
33,
224
],
[
131,
104,
25,
207
],
[
78,
135,
18,
193
],
[
167,
107,
9,
188
]
]
},
{
"id": 534679,
"route": [
[
43,
107,
9,
188
],
[
128,
100,
23,
203
],
[
120,
146,
35,
228
],
[
167,
107,
9,
188
]
]
},
{
"id": 534680,
"route": [
[
43,
107,
9,
188
],
[
78,
104,
7,
172
],
[
105,
120,
22,
201
],
[
128,
100,
23,
203
],
[
167,
107,
9,
188
]
]
},
{
"id": 534681,
"route": [
[
43,
107,
9,
188
],
[
99,
150,
30,
217
],
[
89,
93,
7,
172
],
[
110,
89,
13,
183
],
[
134,
130,
35,
227
],
[
167,
107,
9,
188
]
]
},
{
"id": 534682,
"route": [
[
43,
107,
9,
188
],
[
74,
125,
13,
183
],
[
105,
120,
22,
201
],
[
74,
120,
11,
180
],
[
78,
104,
7,
172
],
[
115,
90,
15,
187
],
[
167,
107,
9,
188
]
]
},
{
"id": 534683,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
75,
109,
8,
173
],
[
78,
104,
7,
172
],
[
167,
107,
9,
188
]
]
},
{
"id": 534684,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
134,
130,
35,
227
],
[
124,
96,
20,
197
],
[
167,
107,
9,
188
]
]
},
{
"id": 534685,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
131,
104,
25,
207
],
[
74,
114,
9,
176
],
[
135,
114,
30,
217
],
[
167,
107,
9,
188
]
]
},
{
"id": 534686,
"route": [
[
43,
107,
9,
188
],
[
134,
109,
27,
213
],
[
78,
104,
7,
172
],
[
105,
120,
22,
201
],
[
99,
150,
30,
217
],
[
136,
120,
32,
221
],
[
124,
96,
20,
197
],
[
167,
107,
9,
188
]
]
},
{
"id": 534687,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
94,
90,
8,
173
],
[
74,
120,
11,
180
],
[
75,
109,
8,
173
],
[
128,
139,
36,
229
],
[
167,
107,
9,
188
]
]
},
{
"id": 534688,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
134,
109,
27,
213
],
[
134,
130,
35,
227
],
[
115,
149,
35,
227
],
[
167,
107,
9,
188
]
]
},
{
"id": 534689,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
89,
146,
25,
207
],
[
85,
96,
7,
171
],
[
85,
143,
23,
203
],
[
135,
125,
33,
224
],
[
136,
120,
32,
221
],
[
167,
107,
9,
188
]
]
},
{
"id": 534690,
"route": [
[
43,
107,
9,
188
],
[
105,
151,
32,
221
],
[
105,
120,
22,
201
],
[
85,
96,
7,
171
],
[
99,
150,
30,
217
],
[
167,
107,
9,
188
]
]
},
{
"id": 534691,
"route": [
[
43,
107,
9,
188
],
[
104,
89,
11,
179
],
[
105,
120,
22,
201
],
[
124,
143,
36,
229
],
[
78,
135,
18,
193
],
[
167,
107,
9,
188
]
]
},
{
"id": 534692,
"route": [
[
43,
107,
9,
188
],
[
74,
125,
13,
183
],
[
105,
120,
22,
201
],
[
85,
143,
23,
203
],
[
167,
107,
9,
188
]
]
},
{
"id": 534693,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
136,
120,
32,
221
],
[
105,
151,
32,
221
],
[
134,
109,
27,
213
],
[
124,
96,
20,
197
],
[
75,
130,
15,
187
],
[
115,
90,
15,
187
],
[
167,
107,
9,
188
]
]
},
{
"id": 534694,
"route": [
[
43,
107,
9,
188
],
[
135,
125,
33,
224
],
[
74,
114,
9,
176
],
[
110,
150,
33,
224
],
[
105,
120,
22,
201
],
[
128,
100,
23,
203
],
[
124,
96,
20,
197
],
[
94,
149,
28,
213
],
[
167,
107,
9,
188
]
]
},
{
"id": 534695,
"route": [
[
43,
107,
9,
188
],
[
74,
120,
11,
180
],
[
128,
139,
36,
229
],
[
134,
130,
35,
227
],
[
105,
120,
22,
201
],
[
94,
149,
28,
213
],
[
99,
89,
9,
176
],
[
167,
107,
9,
188
]
]
},
{
"id": 534696,
"route": [
[
43,
107,
9,
188
],
[
136,
120,
32,
221
],
[
74,
114,
9,
176
],
[
105,
120,
22,
201
],
[
85,
96,
7,
171
],
[
78,
104,
7,
172
],
[
124,
143,
36,
229
],
[
167,
107,
9,
188
]
]
},
{
"id": 534697,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
128,
139,
36,
229
],
[
131,
104,
25,
207
],
[
85,
96,
7,
171
],
[
167,
107,
9,
188
]
]
},
{
"id": 534698,
"route": [
[
43,
107,
9,
188
],
[
104,
89,
11,
179
],
[
120,
146,
35,
228
],
[
124,
96,
20,
197
],
[
105,
120,
22,
201
],
[
74,
114,
9,
176
],
[
167,
107,
9,
188
]
]
},
{
"id": 534699,
"route": [
[
43,
107,
9,
188
],
[
74,
125,
13,
183
],
[
105,
120,
22,
201
],
[
75,
130,
15,
187
],
[
81,
100,
7,
171
],
[
75,
109,
8,
173
],
[
167,
107,
9,
188
]
]
},
{
"id": 534700,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
124,
96,
20,
197
],
[
128,
139,
36,
229
],
[
120,
93,
18,
193
],
[
94,
90,
8,
173
],
[
167,
107,
9,
188
]
]
},
{
"id": 534701,
"route": [
[
43,
107,
9,
188
],
[
105,
120,
22,
201
],
[
115,
90,
15,
187
],
[
99,
150,
30,
217
],
[
78,
104,
7,
172
],
[
124,
96,
20,
197
],
[
94,
149,
28,
213
],
[
89,
146,
25,
207
],
[
167,
107,
9,
188
]
]
},
{
"id": 534702,
"route": [
[
43,
107,
9,
188
],
[
120,
146,
35,
228
],
[
105,
120,
22,
201
],
[
110,
89,
13,
183
],
[
167,
107,
9,
188
]
]
}
]
console.clear();
var data = tributary.data;
//console.log(data)
data = _.map(data, function(item, i) {
item.route.splice(0, 1);
item.route.splice(item.route.length - 1, 1);
return item;
});
var newData = [];
_.forEach(data, function(item) {
_.forEach(item.route, function(route) {
var data = {
name: 'root.' + item.id,
route: route
};
newData.push(data);
});
})
_.forEach(data, function(item) {
// console.log(item.route.length)
})
var sumData = [];
_.forEach(newData, function(item) {
var sum = item.route[0] + item.route[1] + item.route[2] + item.route[3];
sumData.push({
name: item.name,
sum: sum
});
});
//console.log(_.groupBy(sumData, 'sum'))
var routeObject = _.groupBy(newData, 'route');
var routeArray = [];
_.forEach(routeObject, function(value, key) {
routeArray.push({
name: 'root.' + key,
value: value
});
return value;
});
console.log(routeArray)
//console.log('newData', newData)
newData = newData.concat(routeArray);
var nestedData = d3.nest()
.key(function(d) {
return d.name;
})
.entries(newData)
//console.log(nestedData)
var svg = d3.select('svg')
.append('g');
//return;
nestedData = _.map(nestedData, function(item) {
item.name = item.key;
delete item.key;
return item;
})
//console.log(newData, 'newData')
var diameter = 960;
var radius = diameter / 2;
var innerRadius = radius - 84;
var cluster = d3.layout.cluster()
.size([360, innerRadius])
.sort(null)
.value(function(d) { return d.size; });
var bundle = d3.layout.bundle();
var line = d3.svg.line.radial()
.interpolate("bundle")
.tension(.49)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
var svg = d3.select("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var nodes = cluster.nodes(packageHierarchy(nestedData)),
links = packageImports(nodes);
//console.log('nodes', links)
svg.selectAll(".link")
.data(bundle(links))
.enter().append("path")
.attr("class", "link")
.attr("d", line);
svg.selectAll(".node")
.data(nodes.filter(function(n) { return !n.children; }))
.enter()
.append("g")
.attr("class", "node")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
.append("text")
.attr("dx", function(d) { return d.x < 180 ? 8 : -8; })
.attr("dy", ".31em")
.attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.attr("transform", function(d) { return d.x < 180 ? null : "rotate(180)"; })
.text(function(d) { return d.name.replace('root.', ''); });
d3.select(self.frameElement).style("height", diameter + "px");
// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
var map = {};
function find(name, data) {
var node = map[name], i;
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
node.parent.children.push(node);
node.key = name.substring(i + 1);
}
}
return node;
}
classes.forEach(function(d) {
find(d.name, d);
});
return map[""];
}
// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
var map = {},
imports = [];
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
// For each import, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.values && d.values.length > 0) d.values.forEach(function(i) {
if (i.route)
imports.push({source: map[d.name], target: map['root.' + i.route]});
});
});
return imports;
}
.node {
font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.link {
stroke: steelblue;
stroke-opacity: .4;
fill: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment