forked from salx/SR_fixed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeschlecht.js
322 lines (280 loc) · 10.2 KB
/
geschlecht.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var margin = {top: 280, right: 280, bottom: 250, left: 280},
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]);
*/
/*
var svg = d3.select("body").append("svg")
.attr("width", margin.left + margin.right)
.attr("height", margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
*/
var svg = d3.select(".content").append("svg")
.attr("width", 500)
.attr("height", 500)
.append("g")
.attr("transform", "translate(" + (margin.left-40) + "," + (margin.top-50) + ")");
var tip = d3.tip()
.attr("class", "d3-tip")
.offset([-10, 0])
.html( function(d){
if( d.name === "f" ){
return "<text>Frauen </br> Zum Hineinzoomen klicken</text>";
}else if( d.name === "m" ){
return "<text>Männer</br> Zum Hineinzoomen klicken</text>";
}else if( d.depth === 2 ){
return tip.hide(); //sifu - wie kann man das "richtig" lösen. das stimmt so nicht, produziert einen Fehler auf der Konsole, macht aber was ich will (tip erscheint nicht.)
}else{
return "<text><strong>"+d.name+":</br>"+d.partei+",</br>"+d.info+"</strong></text>";
}
});
svg.call(tip);
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 - 0.01 / ( d.depth + 0.5 ); } )
.innerRadius( function(d){ return radius / 3 * d.depth; } )
.outerRadius( function(d){ return radius / ( 2 + d.depth ) * ( d.depth + 1 ) -1; } )
/*
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx - .01 / (d.depth + .5); })
.innerRadius(function(d) { return radius / 3 * d.depth; })
.outerRadius(function(d) { return radius / 3 * (d.depth + 1) - 1; });
*/
d3.json("geschlecht.json", function(error, root) {
// Compute the initial layout on the entire tree to sum sizes.
// Also compute the full name and fill color for each node,
// and stash the children so they can be restored as we descend.
partition
.value(function(d) { return d.value; })
.nodes(root)
.forEach(function(d) {
d._children = d.children;
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; });
//svg.selectAll( '.center' ).remove();
/*we do element backgrounds using patterns :) <-- damnit i need to cut back on smiley usage..*/
/* you need to play with the x/y width values to get what you want..*/
svg.append("defs").append("pattern")
.attr("id", "centerBackground")
.attr("x", "0")
.attr("y", "0")
.attr("width", 1)
.attr("height", 1)
.attr("patternUnits", "objectBoundingBox")
.append("image")
.attr("xlink:href", "icon_21902.svg")
.attr("x", 0)
.attr("y", 0)
.attr("width", 2*radius/3)
.attr("height", 2*radius/3);
var center = svg.append("circle")
.attr("r", radius / 3)
.style("fill","url(#centerBackground)")
.on("click", zoomOut);
//center.append("title")
//.text("zoom out");
/*
center.append("image") // wird nicht angezeigt. Format ändenr hilft nichts, x,y ändern auch nicht, kann geladen werden
.attr("xlink:href", "icon_21902.svg")
.attr("x", "-50px")
.attr("y", "-50px")
.attr("width", "100px")
.attr("height", "100px");
*/
var path = svg.selectAll("path")
.data(partition.nodes(root).slice(1))
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) { return d.fill; })
.style("fill-opacity", function(d){
if(d.depth===2){
return 0.01;
}else{
return 1;
}
})
.each(function(d) { this._current = updateArc(d); })
//.on("click", zoomIn); //hier: Funktion für click
.on("click", function(d){
if(d.children){
zoomIn(d);
d3.select(".infocontent2").classed("hidden", false);
d3.select(".text.allgemein").classed( "hidden", false);
}else if(!d.children){
d3.select(".text.allgemein").classed( "hidden", true);
//d3.selectAll(".infocontent2.text").classed( "hidden", true );
d3.select(".text." + d.id).classed( "hidden", false);
}
})
.on("mouseover", tip.show )
.on("mouseout", tip.hide);
function zoomIn(p) {
if (p.depth > 1) p = p.parent;
if (!p.children) return;
zoom(p, p);
center.select("image")
.remove();
}
function zoomOut(p) {
if (!p.parent) return;
d3.selectAll(".infocontent2").classed("hidden", true);
d3.selectAll(".infocontent2 .text").classed("hidden", true);//du hast auch versucht elemente zu selecten die sowohl die infoconten2 als auch die text class haben.. whitespace :)
zoom(p.parent, p);
/* Dieser Part produziert einen "not defined fehler"
circle.classed("female", false);
circle.classed("male", false);
center.select("image")
.remove();
d3.select(".text.geschlecht").classed("hidden", false);
*/
}
// Zoom to the specified new root.
function zoom(root, p) {
if (document.documentElement.__transition__) return;
// Rescale outside angles to match the new layout.
var enterArc,
exitArc,
outsideAngle = d3.scale.linear().domain([0, 2 * Math.PI]);
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: outsideAngle(d.x), dx: outsideAngle(d.x + d.dx) - outsideAngle(d.x)};
}
center.datum(root);
// When zooming in, arcs enter from the outside and exit to the inside.
// Entering outside arcs start from the old layout.
if (root === p) enterArc = outsideArc, exitArc = insideArc, outsideAngle.range([p.x, p.x + p.dx]);
path = path.data(partition.nodes(root).slice(1), function(d) { return d.key; });
// When zooming out, arcs enter from the inside and exit to the outside.
// Exiting outside arcs transition to the new layout.
if (root !== p) enterArc = insideArc, exitArc = outsideArc, outsideAngle.range([p.x, p.x + p.dx]);
d3.transition().duration(d3.event.altKey ? 7500 : 750).each(function() {
path.exit().transition()
.style("fill-opacity", function(d) { return d.depth === 1 + (root === p) ? 1 : 0; })
.attrTween("d", function(d) { return arcTween.call(this, exitArc(d)); })
.remove();
path.enter().append("path")
.style("fill-opacity", function(d) { return d.depth === 2 - (root === p) ? 1 : 0; })
.style("fill", function(d) { return d.fill; })
//.on("click", zoomIn)
.on("click", function(d){
if(d.children){
zoomIn(d);
d3.select(".infocontent2").classed("hidden", false);
d3.select(".text.allgemein").classed( "hidden", false);
}else if(!d.children){
d3.select(".text.allgemein").classed( "hidden", true);
//d3.selectAll(".infocontent2.text").classed( "hidden", true );
d3.select(".text." + d.id).classed( "hidden", false);
}
})
.each(function(d) { this._current = enterArc(d); })
.on("mouseover", tip.show )
.on("mouseout", tip.hide);
/* - hier gibt's einen Fehler
path.transition()
.each("end", function(d, i){
if(labelText === "m"){
circle.classed("male", true)
center.append("image")
.attr("xlink:href", "icon_25455.svg")
.attr("x", "-50px")
.attr("y", "-50px")
.attr("width", "100px")
.attr("height", "100px")
d3.selectAll(".text").classed("hidden", true)
d3.select(".text.m").classed("hidden", false);
}else if(labelText==="f"){
circle.classed("female", true)
center.append("image")
.attr("xlink:href", "icon_25454.png")
.attr("x", "-50px")
.attr("y", "-50px")
.attr("width", "100px")
.attr("height", "100px")
d3.selectAll(".text").classed("hidden", true)
d3.select(".text.f").classed("hidden", false);
}
})
path.transition()
.style("fill-opacity", 1)
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });
*/
path.transition()
.style("fill-opacity", function(d){
if(d.depth===2){
return 0.01;
}else{
return 1;
}
})
.attrTween("d", function(d) { return arcTween.call(this, updateArc(d) ); } );
});
}
});
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 fill(d){
var p = d;
//var c;
if( p.depth === 1 ){
var colors = {
'm': "#cfb725",
'f': "#30b68f",
}
return colors[p.name];
}else if( p.depth === 2 ){
var colors = {
'SPÖ': 'red',
'ÖVP': 'black',
'FPÖ': 'blue',
'Grüne': 'green',
'BZÖ': 'orange',
'unabhängig': '#999',
'Krone': '#999'
}
return colors[p.partei];
}
}
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");