-
Notifications
You must be signed in to change notification settings - Fork 4
/
coicop_hierarchy.html
203 lines (166 loc) · 6.9 KB
/
coicop_hierarchy.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="shortcut icon" type="image/png" href="https://raw.githubusercontent.com/eurostat/eurostat.js/master/img/favicon.png"/>
<title>COICOP hierarchy</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/[email protected]/js/lib.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/[email protected]/js/eurostat-lib.js"></script>
<style>
.node circle {
stroke: gray;
stroke-width: 0.5px;
}
.node {
font-family: sans-serif;
}
.link {
fill: none;
stroke: #ddd;
stroke-width: 5px;
}
</style>
</head>
<body>
<div id="chart"></div>
<script>
/**
*
* @author julien Gaffuri
*
*/
(function($, EstLib) {
$(function() {
//https://bl.ocks.org/syntagmatic/eebcb592d3fdd9ac4a5c92361c1e7066
//based on http://bl.ocks.org/mbostock/4063550
//TODO adapt label depending on level
//TODO sunburst
//https://bl.ocks.org/mbostock/4348373
//https://bl.ocks.org/kerryrodden/477c1bfb081b783f80ad
var dim = EstLib.getMaxSize(),
width = dim.width,
height = dim.height;
var radius = 1100;
//SVG element
var svg = d3.select("#chart").append("svg")
.attr("width", width).attr("height", height)
.append("g")
.attr("transform", "translate(" + (width*0.5) + "," + (height*0.5) + ")");
var g = svg.append("g");
//add zoom/pan behaviour
//add zoom/pan behaviour
svg.append("rect")
.attr("x", -(width*0.5)).attr("y", -(height*0.5)).attr("width", width).attr("height", height)
.style("fill", "none").style("pointer-events", "all")
.call(d3.zoom().scaleExtent([0.25,4])
.on("zoom", function() { g.attr("transform", d3.event.transform); }));
//TODO see options https://github.com/d3/d3-hierarchy/blob/master/README.md#tree
var tree = d3.tree()
.size([360, radius])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
//return the coicop level, from 1 to 5
var getLevel = function(coicop){
if(!coicop) return 3; //TODO
if(coicop == "00") return 1;
return coicop.length;
};
//returns the color of coicop family
var coicopToColor = function(coicop){
var fam = coicop.substring(0,2);
if(fam==="00") return "gray";
return d3.schemeSet3[+(fam)-1];
};
var project = function(x, y) {
var angle = (x - 90) / 180 * Math.PI, radius = y;
return [radius * Math.cos(angle), radius * Math.sin(angle)];
};
//load coicop data
d3.csv("https://raw.githubusercontent.com/eurostat/eurostat.js/master/data/coicop.csv", function(error, data) {
if (error) throw error;
//remove CPs
for(var i=0; i<data.length; i++) data[i].code = data[i].code.replace("CP","");
//index data
data = EstLib.index(data,"code");
var codes = Object.keys(data);
//link to father
for(i=0; i<codes.length; i++){
var childCode = codes[i];
if( ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"].indexOf(childCode.substring(childCode.length-1, childCode.length)) != -1 ) continue;
var father = data[childCode.substring(0, childCode.length-1)];
if(father) data[childCode].father = father;
//else console.log(childCode);
}
//link to children
for(i=0; i<codes.length; i++) {
var child = data[codes[i]];
if(!child.father) continue;
if(!child.father.children) child.father.children = [];
child.father.children.push(child);
}
data["00"].children = [data["01"],data["02"],data["03"],data["04"],data["05"],data["06"],data["07"],data["08"],data["09"],data["10"],data["11"],data["12"]];
data = data["00"];
//TODO see options
var root = d3.hierarchy(data);
tree(root);
//draw links
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + project(d.x, d.y)
+ "C" + project(d.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, d.parent.y);
});
//draw nodes
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; });
var circleSize = [0,20,13,7,5,3];
var fontSize = [0,18,16,12,10,9];
var getFontSize = function(d) { return fontSize[getLevel(d.data.code)]; };
//draw circles
node.append("circle")
.attr("r", function(d) { return circleSize[getLevel(d.data.code)]; })
.attr("fill",function(d) { return coicopToColor(d.data.code); });
//TODO adapt text position
//TODO on mouse over on nodes: show text?
//draw labels - code
node.append("text")
.attr("dy", ".31em")
.attr("font-size", getFontSize)
.attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.attr("transform", function(d) {
var level = getLevel(d.data.code);
switch(level){
case 1: return "rotate("+ (90-d.x) +")translate(80,-30)";
//case 2: return "rotate("+ (90-d.x) +")";
case 2: return d.x < 180 ? "translate(18,0)" : "rotate(180)translate(-18,0)";
case 3: return d.x < 180 ? "translate(11,0)" : "rotate(180)translate(-11,0)";
case 4: return d.x < 180 ? "translate(8,0)" : "rotate(180)translate(-8,0)";
case 5: return d.x < 180 ? "translate(8,0)" : "rotate(180)translate(-8,0)";
}
})
.text(function(d) {
d = d.data;
var level = getLevel(d.code);
switch(level){
case 1: return d.desc + " ("+d.code+")";
case 2: return d.desc + " ("+d.code+")";
case 3: return d.desc + " ("+d.code+")";
case 4: return d.desc + " ("+d.code+")";
case 5: return d.desc + " ("+d.code+")";
}
});
});
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>