1 | /* |
---|
2 | * Implementacion valores iniciales |
---|
3 | */ |
---|
4 | var webformReport = {}; |
---|
5 | Drupal.behaviors.webformReport = function (context) { |
---|
6 | build_dcs(Drupal.settings.semantico); |
---|
7 | } |
---|
8 | |
---|
9 | |
---|
10 | function build_dcs(json){ |
---|
11 | |
---|
12 | var w = 740, |
---|
13 | h = 750; |
---|
14 | padding = 10 ; |
---|
15 | var bounds = d3.geom.polygon([ |
---|
16 | [padding, padding], |
---|
17 | [padding, h - padding], |
---|
18 | [w - padding, h - padding], |
---|
19 | [w - padding, padding] |
---|
20 | ]); |
---|
21 | |
---|
22 | var svg = d3.select('#dcs_content') |
---|
23 | .append("svg:svg") |
---|
24 | .attr("width", w) |
---|
25 | .attr("height", h) |
---|
26 | .style('border','2px solid black').style('background','#FFFFFF'); |
---|
27 | |
---|
28 | var links = [] ; |
---|
29 | var json_docs = json.documents ; |
---|
30 | var clusters = json.clusters ; |
---|
31 | var nodes = [] , anchorNodes = [] ; |
---|
32 | var l = 0; |
---|
33 | |
---|
34 | var N_foci = clusters.length , |
---|
35 | y_fac = Math.sqrt(N_foci*4/3) , |
---|
36 | x_fac = N_foci/y_fac , |
---|
37 | width_fac = w/(y_fac); |
---|
38 | height_fac = h/(x_fac); |
---|
39 | |
---|
40 | |
---|
41 | var foci = d3.range(N_foci).map(function(i){ |
---|
42 | return { |
---|
43 | i: Math.floor(i/x_fac), |
---|
44 | j: Math.floor(i%x_fac), |
---|
45 | x: width_fac/2 + Math.floor(i/x_fac)*width_fac , |
---|
46 | y: height_fac/2 + Math.floor(i%x_fac)*height_fac } |
---|
47 | }); |
---|
48 | |
---|
49 | |
---|
50 | clusters.forEach(function(d,i){ |
---|
51 | var documents = d.documents ; |
---|
52 | var N = clusters.length ; |
---|
53 | var len_x = Math.sqrt(1.6*N); |
---|
54 | var px = Math.floor(i%len_x) ; |
---|
55 | var qy = Math.floor(i/len_x); |
---|
56 | var coor_x = d.score+px*w/len_x ; |
---|
57 | var coor_y = d.score+qy*h*len_x/N; |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | nodes.push({name:d.phrases[0], |
---|
63 | vert:true, |
---|
64 | score:d.score, |
---|
65 | x:coor_x, |
---|
66 | y:coor_y, |
---|
67 | id:i, |
---|
68 | //fixed:true, |
---|
69 | docs:documents}) ; |
---|
70 | |
---|
71 | l += i === 0 ? 0 : clusters[i-1].documents.length; |
---|
72 | documents.forEach(function(d,j) { |
---|
73 | nodes.push({name:d,vert:false}); |
---|
74 | links.push({ source:i+l , target:i+l+j+1 }); |
---|
75 | }); |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | }); |
---|
80 | |
---|
81 | |
---|
82 | var color = d3.scale.category20(); |
---|
83 | var path = svg.selectAll("path"); |
---|
84 | var circle = svg.selectAll("circle"); |
---|
85 | var line = svg.selectAll("line"); |
---|
86 | |
---|
87 | var labels = svg.selectAll("text"); |
---|
88 | |
---|
89 | var force = d3.layout.force().size([w, h]) |
---|
90 | .nodes(nodes) |
---|
91 | .links(links) |
---|
92 | .charge(-50) |
---|
93 | .gravity(0.07) ; |
---|
94 | |
---|
95 | nodes.forEach(function(item){ |
---|
96 | if(item.vert) { |
---|
97 | anchorNodes.push(item); |
---|
98 | } |
---|
99 | }); |
---|
100 | force.start(); |
---|
101 | |
---|
102 | force.on("tick", function(e) { |
---|
103 | var k = .1 * e.alpha; |
---|
104 | //forces2.forEach(function(item){item.start();}); |
---|
105 | var vertices = [] ; |
---|
106 | nodes.forEach(function(d,i){ |
---|
107 | if (d.vert) { |
---|
108 | d.y += (foci[d.id].y - d.y) * k; |
---|
109 | d.x += (foci[d.id].x - d.x) * k; |
---|
110 | |
---|
111 | vertices.push( [d.x,d.y,d] ) ; |
---|
112 | } |
---|
113 | }); |
---|
114 | |
---|
115 | |
---|
116 | voronoiPol = d3.geom.voronoi(vertices); |
---|
117 | path = path.data(voronoiPol); |
---|
118 | path.enter().insert("path","path") |
---|
119 | .attr("d", function(d) { |
---|
120 | return "M" + d.join("L") + "Z"; |
---|
121 | }) |
---|
122 | .style("fill",function(d,i) { return color(i); } ) |
---|
123 | .on("mousemove",function(d) { |
---|
124 | var style_str = "fill: " + color(d[0]); |
---|
125 | style_str += ";fill-opacity:0.4" ; |
---|
126 | d3.select(this) |
---|
127 | .attr("style",style_str); |
---|
128 | }) |
---|
129 | .on("mouseover",function(d) { |
---|
130 | var style_str = "fill: " + color(d[0]); |
---|
131 | style_str += ";fill-opacity:0.4" ; |
---|
132 | d3.select(this) |
---|
133 | .attr("style",style_str); |
---|
134 | }) |
---|
135 | .on("mouseout",function(d) { |
---|
136 | var style_str = "fill: " + color(d[0]); |
---|
137 | d3.select(this) |
---|
138 | .attr("style",style_str); |
---|
139 | }) |
---|
140 | .on("click",function(d,i) { |
---|
141 | |
---|
142 | /* |
---|
143 | if ( $('#abajo').length != 0 ) { |
---|
144 | return ;} |
---|
145 | */ |
---|
146 | var modal_tuits = modal(); |
---|
147 | var html = '<div id="abajo"></div>'; |
---|
148 | modal_tuits.init({ id:'tuits',content: html,top:'300px',left:'-500px' }); |
---|
149 | //modal_tuits.init({ id:'tuits',content: html}); |
---|
150 | $('#abajo').empty(); |
---|
151 | d3.select('#abajo') |
---|
152 | .append('h2') |
---|
153 | .attr('class','cluster_title') |
---|
154 | .append('p').append('b') |
---|
155 | .text(vertices[i][2].name+':'); |
---|
156 | var lista = d3.select('#abajo').append('ul'); |
---|
157 | vertices[i][2].docs.forEach(function(l,j){ |
---|
158 | var index = json_docs[l].snippet.indexOf(':'); |
---|
159 | var len = json_docs[l].snippet.length; |
---|
160 | lista.append('li') |
---|
161 | // d3.select('div#abajo') |
---|
162 | /* |
---|
163 | .append('div') |
---|
164 | .attr('class','message') |
---|
165 | .append('span') |
---|
166 | .attr('class','message-author') |
---|
167 | */ |
---|
168 | .text(json_docs[l].snippet) |
---|
169 | //.append('span') |
---|
170 | //.attr('class','message-text') |
---|
171 | //.text(json_docs[l].snippet.slice(index+1,len)); |
---|
172 | //.text(j+1+") "+json_docs[l].snippet); |
---|
173 | }); |
---|
174 | |
---|
175 | |
---|
176 | /* |
---|
177 | console.log("\n\n\n",vertices[i][2].name); |
---|
178 | vertices[i][2].docs.forEach(function(l) { |
---|
179 | console.log(l,json_docs[l].snippet); |
---|
180 | });*/ |
---|
181 | }) |
---|
182 | .append("title") |
---|
183 | .text(function(d,i) { |
---|
184 | return vertices[i][2].name; |
---|
185 | }); |
---|
186 | path.attr("d", function(d) { |
---|
187 | return "M" + d.join("L") + "Z"; |
---|
188 | }) |
---|
189 | .style("fill",function(d,i) { return color(i); } ) |
---|
190 | .on("mousemove",function(d) { |
---|
191 | var style_str = "fill: " + color(d[0]); |
---|
192 | style_str += ";fill-opacity:0.4" ; |
---|
193 | d3.select(this) |
---|
194 | .attr("style",style_str); |
---|
195 | }) |
---|
196 | .on("mouseover",function(d) { |
---|
197 | var style_str = "fill: " + color(d[0]); |
---|
198 | style_str += ";fill-opacity:0.4" ; |
---|
199 | d3.select(this) |
---|
200 | .attr("style",style_str); |
---|
201 | }) |
---|
202 | .on("mouseout",function(d) { |
---|
203 | var style_str = "fill: " + color(d[0]); |
---|
204 | d3.select(this) |
---|
205 | .attr("style",style_str); |
---|
206 | }) |
---|
207 | .on("click",function(d,i) { |
---|
208 | /* |
---|
209 | if ( $('#abajo').length != 0 ) { |
---|
210 | return ;} |
---|
211 | */ |
---|
212 | //var offset = $(this).offset(); |
---|
213 | |
---|
214 | var modal_tuits = modal(); |
---|
215 | var html = '<div id="abajo"></div>'; |
---|
216 | //modal_tuits.init({ id:'tuits',content: html,top:d.clientX - offset.left,left:d.clientY - offset.top-50}); |
---|
217 | modal_tuits.init({ id:'tuits',content: html}); |
---|
218 | |
---|
219 | $('#abajo').empty(); |
---|
220 | d3.select('#abajo') |
---|
221 | .append('h2') |
---|
222 | .attr('class','cluster_title') |
---|
223 | .append('p').append('b') |
---|
224 | .text(vertices[i][2].name+':'); |
---|
225 | var lista = d3.select('#abajo').append('ul'); |
---|
226 | vertices[i][2].docs.forEach(function(l,j){ |
---|
227 | var index = json_docs[l].snippet.indexOf(':'); |
---|
228 | var len = json_docs[l].snippet.length; |
---|
229 | var referencia = ''; |
---|
230 | if (Drupal.settings.acceso == '1') { |
---|
231 | var string = json_docs[l].url.replace(/node/g, ''); |
---|
232 | var nid = string.substring(1); |
---|
233 | var element = "views-semantic-op-" + nid; |
---|
234 | var referencia = ""; |
---|
235 | for (var options in Drupal.settings.options[element]) { |
---|
236 | referencia += ' (<a href="' + Drupal.settings.options[element][options]["link"]+ '" title="' + Drupal.settings.options[element][options]["name"] + '" alt="' + Drupal.settings.options[element][options]["description"] + '" class="node-' + nid + ' semantic-element-node ' + Drupal.settings.options[element][options]["class"] + '" target="' + Drupal.settings.options[element][options]["target"] + '">' + Drupal.settings.options[element][options]["name"] + '</a>)'; |
---|
237 | } |
---|
238 | } |
---|
239 | lista.append('li') |
---|
240 | // d3.select('div#abajo') |
---|
241 | .append('div') |
---|
242 | .attr('class','message') |
---|
243 | .append('span') |
---|
244 | .attr('class','message-author') |
---|
245 | .text(json_docs[l].snippet.slice(index+1,len)) |
---|
246 | .append('span') |
---|
247 | .attr('class','message-text') |
---|
248 | .html(referencia); |
---|
249 | //.text(j+1+") "+json_docs[l].snippet); |
---|
250 | }); |
---|
251 | |
---|
252 | /* |
---|
253 | console.log("\n\n\n",vertices[i][2].name); |
---|
254 | vertices[i][2].docs.forEach(function(l) { |
---|
255 | console.log(l,json_docs[l].snippet); |
---|
256 | });*/ |
---|
257 | }) |
---|
258 | .append("title") |
---|
259 | .text(function(d,i) { |
---|
260 | return vertices[i][2].name; |
---|
261 | }); |
---|
262 | path.exit().remove(); |
---|
263 | |
---|
264 | |
---|
265 | var centers = [] |
---|
266 | nodes.forEach(function(d){ |
---|
267 | if (!d.vert){ |
---|
268 | centers.push(d); |
---|
269 | } |
---|
270 | }); |
---|
271 | labels = labels.data(vertices) |
---|
272 | labels.enter().append("text") |
---|
273 | .text(function(d){ |
---|
274 | str = d[2].name; |
---|
275 | return str.length > 15 ? str.slice(0,15) +". . .":str; |
---|
276 | }) |
---|
277 | .attr("x",function(d){ |
---|
278 | str = d[2].name; |
---|
279 | str = str.length > 15 ? str.slice(0,15) +". . .":str; |
---|
280 | return d[0]-str.length*3/2; |
---|
281 | }) |
---|
282 | .attr("y",function(d){ |
---|
283 | return d[1]; |
---|
284 | }) |
---|
285 | .attr("style",function(d) { |
---|
286 | size = d[2].weight+12 ; |
---|
287 | return "font-size:" + size.toString() ; |
---|
288 | }); |
---|
289 | |
---|
290 | |
---|
291 | labels.text(function(d){ |
---|
292 | str = d[2].name; |
---|
293 | return str.length > 15 ? str.slice(0,15) +". . .":str; |
---|
294 | }) |
---|
295 | .attr("x",function(d){ |
---|
296 | str = d[2].name; |
---|
297 | str = str.length > 15 ? str.slice(0,15) +". . .":str; |
---|
298 | return d[0]-str.length*3/2; |
---|
299 | //return d.x; |
---|
300 | }) |
---|
301 | .attr("y",function(d){ |
---|
302 | return d[1]; |
---|
303 | //return d.y; |
---|
304 | }) |
---|
305 | .attr("style",function(d) { |
---|
306 | size = d[2].weight+12 ; |
---|
307 | return "font-size:" + size.toString() ; |
---|
308 | }); |
---|
309 | labels.exit().remove(); |
---|
310 | |
---|
311 | |
---|
312 | circle = circle.data(centers) |
---|
313 | circle.enter().append("circle") |
---|
314 | // .call(force.drag) |
---|
315 | .attr("r", 10) |
---|
316 | .attr("cx", function(d) { return d.x; }) |
---|
317 | .attr("cy", function(d) { return d.y; }) |
---|
318 | .transition().duration(1000).attr("r", 3) |
---|
319 | circle.attr("cx", function(d) { return d.x; }) |
---|
320 | .attr("cy", function(d) { return d.y; }) |
---|
321 | .append("title") |
---|
322 | .text(function(d) { return d.name; }); |
---|
323 | circle.exit().transition().attr("r", 30).remove(); |
---|
324 | }); |
---|
325 | } |
---|
326 | |
---|
327 | function dcs_load_url(post){ |
---|
328 | if (post) { |
---|
329 | $.post(BASE_URL+"/dcs_data/", post, function(json) { |
---|
330 | $("#dcs_content").empty(); |
---|
331 | build_dcs(json); |
---|
332 | }); |
---|
333 | } else { |
---|
334 | $.get(BASE_URL+"/dcs_data/",function(json) { |
---|
335 | $("#dcs_content").empty(); |
---|
336 | build_dcs(json); |
---|
337 | }); |
---|
338 | } |
---|
339 | } |
---|