-
Notifications
You must be signed in to change notification settings - Fork 1
/
index2.html
124 lines (94 loc) · 2.34 KB
/
index2.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 1000;
var tree = d3.layout.tree()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
function myFunc(data) {
console.log(JSON.stringify(data, undefined, 2));
}
function toFlare(data, id_attr, parent_id_attr, root_id_value) {
var flareData = [];
//root node
var root = {};
if (root_id_value) {
console.log(root);
} else {
//Asume the first one
root = data.shift();
root.id = root[id_attr];
}
flareData.push(root);
var nestedData = d3.nest()
.key(function(d) {
d.id = d[id_attr];
return d[parent_id_attr];
})
.rollup(function(v) {
return v
})
.map(data);
function completeChildren(parentsArray) {
parentsArray.forEach(function(e){
if(nestedData[e.id]){
e.children = nestedData[e.id];
delete nestedData[e.id];
completeChildren(e.children);
}
});
}
completeChildren(flareData);
return flareData;
}
d3.json("flare2.json", function(error, data) {
if (error) throw error;
var newData = toFlare(data, "unidad", "reporta_a");
var nodes = tree.nodes(newData),
links = tree.links(nodes);
myFunc(nodes);
var link = svg.selectAll("path.link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll("g.node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.attr("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.unidad; });
});
d3.select(self.frameElement).style("height", height + "px");
</script>
</html>