forked from stared/wizualizacja-wolnych-lektur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motywy_wolnych_lektur.html
270 lines (219 loc) · 7.32 KB
/
motywy_wolnych_lektur.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
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
<!DOCTYPE html>
<head>
<title>Motywy Wolnych Lektur</title>
<meta name="description" content="Mapa motywów z książek z wolnelektury.pl.">
<meta name="keywords" content="wizualizacja,graf,d3.js,sieć,książka,lektura,liryka,epika,dramat">
<meta name="author" content="Piotr Migdał">
<meta charset="utf-8">
</head>
<style>
.node_circle {
stroke: #777;
stroke-width: 1.0px;
}
.node_label {
pointer-events: none;
}
.link {
stroke: #777;
stroke-opacity: .2;
}
.node_count {
stroke: #777;
stroke-width: 1.0px;
fill: #999;
}
text.legend {
font-family: Verdana;
font-size: 13px;
fill: #000;
}
</style>
<body>
<script src="d3.v3.min.js"></script>
<script>
var width = 1000,
height = 800;
var force = d3.layout.force()
.charge( function(d) { return -35 * Math.sqrt(d.count)} )
.linkDistance(50)
.gravity(0.15)
.size([width - 250, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("motywy_wolnych_lektur.json", function(error, graph) {
var kinds = ["liryka", "epika", "dramat"]
var kind_max = {}
for(var i = 0; i < kinds.length; i++){
kind_max[kinds[i]] = d3.max(graph.nodes, function(d){
return d[kinds[i]] / d.count;
});
};
var kind_to_color = function(d){
return d3.rgb(
255 * d.liryka / d.count / kind_max.liryka,
0.9 * 255 * d.epika / d.count / kind_max.epika, // 90% not to make it too bright
255 * d.dramat / d.count / kind_max.dramat
)
};
// var kind_scales = {};
// for(var i = 0; i < kinds.length; i++){
// var x = d3.scale.linear()
// .domain([
// d3.min(graph.nodes, function(d){
// return d[kinds[i]] / d.count;
// }),
// d3.max(graph.nodes, function(d){
// return d[kinds[i]] / d.count;
// })
// ])
// .range([0, 255]);
// kind_scales[kinds[i]] = x;
// };
// var kind_to_color_alt = function(d){
// return d3.rgb(
// kind_scales.liryka(d.liryka / d.count),
// 0.8 * kind_scales.epika(d.epika / d.count),
// kind_scales.dramat(d.dramat / d.count)
// )
// };
var main = svg.append("g")
.attr("class", "graph");
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = main.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return 2 * d.strength; });
var node = main.selectAll(".node_circle")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node_circle")
.attr("r", function(d) { return 0.5 * Math.sqrt(d.count); })
.style("fill", function(d){ return kind_to_color(d).toString(); } )
.on("mouseover", function(d) { mouseover_node(d); })
.on("mouseout", function(d) { mouseout_node(d) })
// .on("dblclick", function(d){
// window.open('http://wolnelektury.pl/katalog/motyw/' + d.name + '/', '_blank');
// window.focus();
// })
.on("dblclick", function(d){ window.location.href=('http://wolnelektury.pl/katalog/motyw/' + d.name + '/') })
.call(force.drag);
var label = main.selectAll(".node_label")
.data(graph.nodes)
.enter().append("text")
.attr("class", "node_label")
.attr("dx", function(d) { return 2 + 0.5 * Math.sqrt(d.count); })
.attr("dy", ".4em")
.attr("font-family", "Verdana")
.attr("font-size", 10)
.style("fill", "#000000")
.text(function(d) { return d.display; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
// node
// .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
label.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
});
var mouseover_node = function(z){
var neighbors = {};
neighbors[z.index] = true;
link.filter(function(d){
if (d.source == z) {
neighbors[d.target.index] = true
return true
} else if (d.target == z) {
neighbors[d.source.index] = true
return true
} else {
return false
}
})
.style("stroke-opacity", 1);
node.filter(function(d){ return neighbors[d.index] })
.style("stroke-width", 3);
label.filter(function(d){ return !neighbors[d.index] })
.style("fill-opacity", 0.2);
label.filter(function(d){ return neighbors[d.index] })
.attr("font-size", 16)
};
var mouseout_node = function(z){
link
.style("stroke-opacity", 0.2);
node
.style("stroke-width", 1)
label
.attr("font-size", 10)
.style("fill-opacity", 1)
};
var legend = svg.append("g")
.attr("class", "legend")
.attr("transform", function(d) { return "translate(800,400)"; });
legend.append("text")
.attr("class", "legend")
.attr("x", 0)
.attr("y", -10)
.style("text-anchor", "middle")
.text("klikając dwukrotnie otwierasz motyw");
legend.append("circle")
.attr("class", "node_count")
.attr("cx", -60)
.attr("cy", 50)
.attr("r", function(d) { return 0.5 * Math.sqrt(200); });
legend.append("text")
.attr("class", "legend")
.attr("x", 80)
.attr("y", 50)
.style("text-anchor", "end")
.text("200 wystąpień");
legend.append("circle")
.attr("class", "node_count")
.attr("cx", -60)
.attr("cy", 90)
.attr("r", function(d) { return 0.5 * Math.sqrt(2000); });
legend.append("text")
.attr("class", "legend")
.attr("x", 80)
.attr("y", 90)
.style("text-anchor", "end")
.text("2000 wystąpień");
var legend_kinds_data = [{pos: [1, 0, 0], name: "liryka"},
{pos: [0, 1, 0], name: "epika"},
{pos: [0, 0, 1], name: "dramat"},
{pos: [2/3, 2/3, 2/3], name: ""}]
var legend_kinds = legend.selectAll("g.kind")
.data(legend_kinds_data)
legend_kinds.enter().append("g")
.attr("class", "kind")
legend_kinds.append("circle")
.attr("class", "node_circle")
.attr("cx", function(d) { return 20 * (Math.sqrt(3) / 2) * (d.pos[2] - d.pos[0]); })
.attr("cy", function(d) { return 180 + 20 * ( d.pos[0] / 2 - d.pos[1] + d.pos[2] / 2 ); })
.attr("r", function(d) { return 0.5 * Math.sqrt(1000); })
.style("fill", function(d) { return d3.rgb(255 * d.pos[0], 0.9 * 255 * d.pos[1], 255 * d.pos[2]).toString()});
legend_kinds.append("text")
.attr("class", "legend")
.attr("x", function(d) { return 65 * (Math.sqrt(3) / 2) * (1.1 * d.pos[2] - d.pos[0]); })
.attr("y", function(d) { return 180 + 40 * ( d.pos[0] / 2 - d.pos[1] + d.pos[2] / 2 ); })
.style("text-anchor", "middle")
.text(function(d) { return d.name })
legend.append("a")
.attr("xlink:href", "https://github.com/stared/wizualizacja-wolnych-lektur")
.append("text")
.attr("class", "legend")
.attr("y", 270)
.style("text-anchor", "middle")
.style("text-decoration", "underline")
.text("CC BY Piotr Migdał (2013)");
});
</script>