-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
655 lines (547 loc) · 17.9 KB
/
main.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
var w = 700;
var h = 700;
var margin = 50;
var selectedViz = 0;
var svgContainer = d3.select(".container").append("svg")
.attr("id", "svg_main")
.attr("width", w)
.attr("height", h);
var axisGroup = svgContainer.append("g")
.attr("id", "axisGroup")
.attr("opacity","1");
var spiderGroup = svgContainer.append("g")
.attr("id", "spiderGroup")
.attr("opacity","1")
.attr("visibility","hidden");
var selectedIdea;
var props;
var var_x;
var var_y;
var var_r;
var var_c;
var xScale = d3.scale.linear()
.domain([0, 10])
.range([0, w-2*margin]);
var yScale = d3.scale.linear()
.domain([0, 10])
.range([h-2*margin,0]);
var rScale = d3.scale.linear()
.domain([0, 10])
.range([5, 25]);
var cScale = d3.scale.linear()
.domain([0, 10])
.range(["#F8E71C", "#D83107"]);
var xAxis = d3.svg.axis()
.scale(xScale)
.ticks(5);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.text("a simple tooltip");
var num_ejes;
var theta;
var r_spider = 300;
function toggleControles() {
$('#controles').toggle('slide', {
direction: 'right'
}, 400);
}
function highlightItem(i) {
unhighlight();
if (selectedViz == 0) {
highlightCircle(i);
} else {
highlightPolygon(i);
}
}
function highlightCircle(i) {
var circles = d3.selectAll("circle.ideas");
var c = d3.select(circles[0][i]);
if (c.classed("invisible")) {
return;
}
var d = ideas[i];
svgContainer.style('cursor','hand');
var circle_x = c.attr("cx");
var circle_y = c.attr("cy");
// Coloco el tooltip, por si highlighteo desde el menú
var svg_position = $("svg").position();
var tooltip_x = (svg_position.left+parseInt(circle_x)+10);
var tooltip_y = (svg_position.top+parseInt(circle_y)-10);
tooltip.style("top",tooltip_y+"px").style("left",tooltip_x+"px");
tooltip.text(d.name);
// Muevo los markers
d3.select("#x_marker_group")
.attr("visibility", "visible")
.attr("transform", "translate("+circle_x+", "+(h-margin+12)+")");
d3.select("#x_marker")
.attr("r", 0)
.transition()
.duration(100)
.attr("r", 10);
d3.select("#x_marker_label")
.text(d[var_x]);
d3.select("#y_marker_group")
.attr("visibility", "visible")
.attr("transform", "translate("+(margin-10)+" , "+circle_y+")");
d3.select("#y_marker")
.attr("r", 0)
.transition()
.duration(100)
.attr("r", 10);
d3.select("#y_marker_label")
.text(d[var_y]);
d3.select("#x_marker_line")
.attr("visibility", "visible")
.attr("x", circle_x)
.attr("y", circle_y)
.attr("width", 1)
.attr("height", ((h-margin)-circle_y));
d3.select("#y_marker_line")
.attr("visibility", "visible")
.attr("x", margin)
.attr("y", circle_y)
.attr("width", (circle_x-margin))
.attr("height", 1);
tooltip.style("visibility", "visible");
}
function highlightPolygon(i) {
var polygon = d3.select("polygon#polygon_"+i);
if (polygon.classed("invisible")) {
return;
}
polygon.style("stroke-width", 1)
.style("stroke-opacity", 1);
var data = polygon.data()[0];
for (var i = 0; i<num_ejes; i++) {
var r_var = r_spider*data[props[i]]/10;
var x = w/2-r_var*Math.sin(theta*i);
var y = h/2-r_var*Math.cos(theta*i);
d3.select("#axis_value_circle_"+i)
.attr("cx", x)
.attr("cy", y)
.attr("visibility", "visible")
d3.select("#axis_value_"+i)
.attr("x", x-3)
.attr("y", y+5)
.attr("visibility", "visible")
.text(data[props[i]]);
}
}
function unhighlight() {
d3.select("circle.highlightCircle")
.transition()
.duration(200)
.attr("r",0);
d3.select('.axis_value')
.attr("visibility", "hidden");
d3.selectAll("polygon")
.style("stroke-width", 0.2)
.style("stroke-opacity", 0.5);
svgContainer.style('cursor','auto');
d3.select("#x_marker_group")
.attr("visibility", "hidden");
d3.select("#y_marker_group")
.attr("visibility", "hidden");
d3.select("#x_marker_line")
.attr("visibility", "hidden")
d3.select("#y_marker_line")
.attr("visibility", "hidden")
tooltip.style("visibility", "hidden");
for (var i = 0; i<num_ejes; i++) {
d3.select("#axis_value_"+i)
.attr("visibility", "hidden");
d3.select("#axis_value_circle_"+i)
.attr("visibility", "hidden");
}
}
function selectItem(i) {
if ($("#list_item_idea_"+(i+1)).hasClass("invisible")) {
return;
}
unhighlight();
showItemDetail();
selectedIdea = i;
if (selectedViz == 0) {
selectCircle(i);
} else {
selectPolygon(i);
}
loadData(i);
showData();
}
function selectPolygon(i) {
d3.selectAll("polygon")
.classed("selected", false)
.style("stroke-width", 0.2)
.style("stroke-opacity", 0.5);
var polygons = d3.selectAll("polygon");
d3.select(polygons[0][i]).classed("selected", true);
}
function selectCircle(i) {
var circles = d3.selectAll("circle.ideas");
var c = d3.select(circles[0][i]);
d3.select("circle.selectedCircle")
.attr("r",0)
.attr("cx", c.attr("cx"))
.attr("cy", c.attr("cy"))
.transition()
.duration(200)
.attr("r",c.attr("r")*2);
}
function unselect() {
selectedIdea = undefined;
d3.select("circle.selectedCircle")
.transition()
.duration(200)
.attr("r",0);
d3.selectAll("polygon")
.classed("selected", false);
}
function showData() {
$('#data').show('slide', {
direction: 'left'
}, 400);
}
function hideData() {
$('#data').hide('slide', {
direction: 'left'
}, 400);
}
function loadData(i) {
if (i != undefined) {
$('#item_detail h2 span').text(ideas[i].name);
$('#item_detail div#description p').text(ideas[i].description);
for (var j = 0; j<props.length; j++){
$('#item_detail #'+props[j]+' .value').text(ideas[i][props[j]]);
};
} else {
}
}
function load() {
props = Object.keys(ideas[0]);
props = props.slice(3,props.length);
console.log(props);
// Hallo el número de ejes
num_ejes = props.length;
theta = 2*Math.PI/num_ejes;
// Cargo combos de configuración de c
var select_x = document.getElementById('selec_eje_x');
var select_y = document.getElementById('selec_eje_y');
var select_r = document.getElementById('selec_r');
var select_c = document.getElementById('selec_c');
var select_num_ejes = document.getElementById('selec_num_ejes');
for (var i = 0; i<props.length; i++){
select_x.appendChild(createOpt(props[i]));
select_y.appendChild(createOpt(props[i]));
select_r.appendChild(createOpt(props[i]));
select_c.appendChild(createOpt(props[i]));
var opt = document.createElement('option');
opt.value = i+1;
opt.innerHTML = i+1;
select_num_ejes.appendChild(opt);
$('#item_detail').append('<div id="'+props[i]+'" class="prop"><span class="label">'+props[i]+'</span><span class="value"></span></div>');
}
select_x.selectedIndex = "0";
select_y.selectedIndex = "1";
select_r.selectedIndex = "2";
select_c.selectedIndex = "3";
function createOpt(value) {
var opt = document.createElement('option');
opt.value = props[i];
opt.innerHTML = props[i];
return opt;
}
// Cargo lista de items
for (var i=0; i<ideas.length; i++) {
$('#item_list ul').append('<li><a class="list_item" id="list_item_'+ideas[i].id+'" href="javascript:selectItem('+i+')">'+ideas[i].name+'</a><div class="onoffswitch"><input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch_'+i+'" checked><label class="onoffswitch-label" for="myonoffswitch_'+i+'" onclick="javascript:toggleItemVisibility('+i+')" ></label></div></li>');
}
var_x = props[0];
var_y = props[1]
var_r = props[2];
var_c = props[3];
var xAxisGroup = axisGroup.append("g")
.attr("class", "axis")
.attr("transform", "translate("+margin+"," + (h - margin) + ")")
.call(xAxis);
var yAxisGroup = axisGroup.append("g")
.attr("class", "axis")
.attr("transform", "translate("+margin+","+margin+")")
.call(yAxis);
axisGroup.append("text")
.attr("id","x_axis_label")
.attr("class", "axis_label")
.attr("x", (w-100)+"px")
.attr("y", (h-10)+"px")
.text(var_x);
axisGroup.append("text")
.attr("id","y_axis_label")
.attr("class", "axis_label")
.attr("x", (70)+"px")
.attr("y", (60)+"px")
.text(var_y);
axisGroup.append("circle")
.attr("class","selectedCircle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r",0);
axisGroup.append("circle")
.attr("class","highlightCircle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r",0);
axisGroup.append("rect")
.attr("id", "y_marker_line")
.attr("x", 0)
.attr("y", 0)
.attr("width", 0)
.attr("height", 0);
axisGroup.append("rect")
.attr("id", "x_marker_line")
.attr("x", 0)
.attr("y", 0)
.attr("width", 0)
.attr("height", 0);
axisGroup.selectAll("circle.ideas")
.data(ideas)
.enter().append("circle")
.attr("class", "ideas")
.attr("cx", function(d, i) { return xScale(d[var_x])+margin; })
.attr("cy", function(d, i) { return margin + yScale(d[var_y]); })
.attr("r", function(d) { return rScale(d[var_r])})
.style("fill", function(d) { return cScale(d[var_c])})
.on("mouseover", function(d, i){
highlightCircle(i);
})
.on("mousemove", function(){
return tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");})
.on("mouseout", function(){
unhighlight();
})
.on("click", function(d,i) {
if (i == selectedIdea) {
unselect();
hideData();
} else {
selectItem(i);
}
return;
});
// Radar chart
// Pinto los ejes
var spiderAxis = spiderGroup.append("g")
.attr("class", "axis");
for (var i = 0; i<num_ejes; i++) {
spiderAxis.append("line")
.attr("x1", w/2)
.attr("y1", h/2)
.attr("x2", w/2-r_spider*Math.sin(theta*i))
.attr("y2", h/2-r_spider*Math.cos(theta*i));
spiderGroup.append("text")
.attr("id","#"+i+"_axis_label")
.attr("class", "axis_label")
.attr("x", w/2-(r_spider+20)*Math.sin(theta*i))
.attr("y", h/2-(r_spider+20)*Math.cos(theta*i))
.text(props[i])
.each(function(d) {
d3.select(this).attr("transform", "translate(-" +this.getComputedTextLength()/2+ ",0)");
});
}
spiderGroup.selectAll(".area")
.data(ideas)
.enter()
.append("polygon")
.attr("id", function(d,i) {return "polygon_"+i})
.attr("class", "")
.attr("points",function(d) {
var str="";
for (var i = 0; i<num_ejes; i++) {
var r_var = r_spider*d[props[i]]/10;
var x = w/2-r_var*Math.sin(theta*i);
var y = h/2-r_var*Math.cos(theta*i);
str=str+x+","+y+" ";
}
return str;
})
.on("mouseover", function(d,i) {
svgContainer.style('cursor','hand');
highlightPolygon(i);
return;
})
.on("mouseout", function(d,i) {
unhighlight()
return;
})
.on("click", function(d,i) {
if (i == selectedIdea) {
unselect();
hideData();
} else {
selectItem(i);
return;
}
});
for (var i = 0; i<num_ejes; i++) {
spiderGroup.append("circle")
.attr("id","axis_value_circle_"+i)
.attr("class", "axis_value_circle")
.attr("cx", w/2-(r_spider+20)*Math.sin(theta*i))
.attr("cy", h/2-(r_spider+20)*Math.cos(theta*i))
.attr("r", 12)
.attr("visibility", "hidden");
spiderGroup.append("text")
.attr("id","axis_value_"+i)
.attr("class", "axis_value")
.attr("x", w/2-(r_spider+20)*Math.sin(theta*i)-6)
.attr("y", h/2-(r_spider+20)*Math.cos(theta*i)+5)
.attr("visibility", "hidden")
.text("");
}
$("#item_list ul li").on("mouseover", function() {
highlightItem($(this).index());
})
$("#item_list ul").on("mouseout", function() {
unhighlight();
})
var x_marker_group = axisGroup
.append("g")
.attr("id", "x_marker_group")
x_marker_group.append("circle")
.attr("id", "x_marker")
.attr("r", "0")
.attr("cx", "0")
.attr("cy", "0");
x_marker_group.append("text")
.attr("id", "x_marker_label")
.attr("x", "-3")
.attr("y", "5")
.text("");
var y_marker_group = axisGroup
.append("g")
.attr("id", "y_marker_group")
y_marker_group.append("circle")
.attr("id", "y_marker")
.attr("r", "0")
.attr("cx", "0")
.attr("cy", "0");
y_marker_group.append("text")
.attr("id", "y_marker_label")
.attr("x", "-3")
.attr("y", "5")
.text("");
}
function change_x() {
var_x = document.getElementById("selec_eje_x").value;
redrawAxis();
}
function change_y() {
var_y = document.getElementById("selec_eje_y").value;
redrawAxis();
}
function change_r() {
var_r = document.getElementById("selec_r").value;
redrawAxis();
}
function change_c() {
var_c = document.getElementById("selec_c").value;
redrawAxis();
}
function change_num_ejes() {
num_ejes = document.getElementById("selec_num_ejes").value;
console.log("Ejes: "+num_ejes);
redrawSpider();
}
function redrawAxis() {
axisGroup.selectAll("circle.ideas")
.data(ideas)
.transition()
.duration(500)
.attr("cx", function(d, i) { return xScale(d[var_x])+margin; })
.attr("cy", function(d, i) { return margin + yScale(d[var_y]); })
.attr("r", function(d) { return rScale(d[var_r])})
.style("fill", function(d) { return cScale(d[var_c])});
if (selectedIdea != undefined) {
axisGroup.selectAll("circle.selectedCircle")
.transition()
.duration(500)
.attr("cx", xScale(ideas[selectedIdea][var_x])+margin)
.attr("cy", margin + yScale(ideas[selectedIdea][var_y]))
.attr("r", rScale(ideas[selectedIdea][var_r])*2);
}
axisGroup.select("#x_axis_label")
.text(var_x);
axisGroup.select("#y_axis_label")
.text(var_y);
}
function redrawSpider() {
}
function selectAxis() {
selectedViz = 0;
$(".axisSelector").toggleClass("selected");
$(".spiderSelector").toggleClass("selected");
$("#axis_controles").show();
$("#spider_controles").hide();
console.log("Seleccionando axis");
svgContainer.selectAll("#axisGroup")
.transition()
.duration(500)
.attr("visibility","visible");
svgContainer.selectAll("#spiderGroup")
.transition()
.duration(500)
.attr("visibility","hidden");
if (selectedIdea != undefined) {
selectItem(selectedIdea);
}
}
function selectSpider() {
selectedViz = 1;
$(".axisSelector").toggleClass("selected");
$(".spiderSelector").toggleClass("selected");
$("#axis_controles").hide();
$("#spider_controles").show();
console.log("Seleccionando spider");
svgContainer.selectAll("#axisGroup")
.transition()
.duration(500)
.attr("visibility","hidden");
svgContainer.selectAll("#spiderGroup")
.transition()
.duration(500)
.attr("visibility","visible");
if (selectedIdea != undefined) {
selectPolygon(selectedIdea);
}
}
function showItemList() {
$("#item_list").animate({left: "0"}, 300);
$("#item_detail").animate({left: "230px"}, 300);
unselect();
}
function showItemDetail() {
$("#item_list").animate({left: "-230px"}, 300);
$("#item_detail").animate({left: "0px"}, 300);
}
function toggleItemVisibility(i) {
unhighlight();
var circles = d3.selectAll("circle.ideas");
var c = d3.select(circles[0][i]);
c.classed("invisible", !c.classed("invisible"));
var p = d3.select("polygon#polygon_"+i);
p.classed("invisible", !p.classed("invisible"));
$("#list_item_idea_"+(i+1)).toggleClass("invisible");
}
function download() {
var e = document.createElement('script');
if (window.location.protocol === 'https:') {
e.setAttribute('src', 'https://rawgit.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js');
} else {
e.setAttribute('src', 'http://nytimes.github.com/svg-crowbar/svg-crowbar.js');
}
e.setAttribute('class', 'svg-crowbar');
document.body.appendChild(e);
};