forked from CAHLR/d3-scatterplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.html
More file actions
1535 lines (1401 loc) · 56 KB
/
plot.html
File metadata and controls
1535 lines (1401 loc) · 56 KB
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<!-- Example based on http://bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->
<!-- The css script below determines the appearance of the webpage -->
<style>
body {
font: 11px sans-serif;
}
.lasso path {
stroke: rgb(80,80,80);
stroke-width:2px;
}
.lasso .drawn {
fill-opacity:.05 ;
}
.lasso .loop_close {
fill:none;
stroke-dasharray: 4,4;
}
.lasso .origin {
fill:#3399FF;
fill-opacity:.5;
}
.not_possible {
fill:rgb(200,200,200);
}
.possible {
fill:#EC888C;
}
.dot{
stroke: #000;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tooltip {
position: absolute;
width: 250px;
height: 28px;
background: rgba(255,255,255,0.80);
pointer-events: none;
}
.tooltip1 {
position: absolute;
width: 100px;
height:20px;
background: rgba(255,255,255,0.9);
pointer-events: none;
}
/*The dropdown selectors*/
/*Coloring*/
.select1{
margin-left: 122px;
margin-top: 810px;
position: absolute;
}
/*Searching*/
.select2{
margin-left: 140px;
margin-top: 960px;
position: absolute;
}
/*?? Used in table in tabulate*/
.select3{
position: absolute;
margin-left: 50px;
margin-top: 600px;
}
/*Transparent*/
.select4{
margin-left: 237px;
margin-top: 1060px;
position: absolute;
}
/*Click on feature*/
.select5{
margin-left: 190px;
margin-top: 870px;
position: absolute;
}
/*Shaping*/
.select6{
margin-left: 135px;
margin-top: 910px;
position: absolute;
}
/*?? Default input textbox*/
input{
width: 145px;
}
/*The checkbox*/
.input1{
width : 40px;
}
/*Input textbox for circle and zoom*/
.input2{
width : 225px;
height : 25px;
}
/*Transparent*/
.form1 {
text-align: left;
position: absolute;
margin-left: 20px;
margin-top: 1100px;
}
/*Search*/
.form2 {
text-align: left;
position: absolute;
margin-left: 20px;
margin-top: 1010px;
}
/*Unused*/
.form3 {
text-align: left;
position: absolute;
margin-left: 680px;
margin-top: 850px;
}
/*Wrapper for each option*/
/*Coloring column*/
.p1 {
margin-left: 20px;
margin-top: 810px;
position: absolute;
font-size: 150%;
}
/*Searching column*/
.p2 {
margin-left: 20px;
margin-top: 960px;
position: absolute;
font-size: 150%;
}
/*Side table*/
.p3 {
margin-left: 1000px;
margin-top: 20px;
position: absolute;
white-space: nowrap;
font-size: 150%;
}
/*Transparent column*/
.p4 {
margin-left: 20px;
margin-top: 1060px;
position: absolute;
font-size: 150%;
}
/*Spectrum and log*/
.p5 {
margin-left: 20px;
margin-top: 835px;
position: absolute;
font-size: 150%;
}
/*Click on feature*/
.p6 {
margin-left: 20px;
margin-top: 870px;
position: absolute;
font-size: 150%;
}
/*Shaping column*/
.p7 {
margin-left: 20px;
margin-top: 910px;
position: absolute;
font-size: 150%;
}
/*Zoom*/
.p8 {
margin-left: 20px;
margin-top: 770px;
position: absolute;
font-size: 150%;
}
/*Circle*/
.p9 {
margin-left: 20px;
margin-top: 730px;
position: absolute;
font-size: 150%;
}
/*Exact match - search*/
.p10 {
margin-left: 20px;
margin-top: 985px;
position: absolute;
font-size: 150%;
}
/*Exact match - transparent*/
.p11 {
margin-left: 20px;
margin-top: 1085px;
position: absolute;
font-size: 150%;
}
/*Display top predicted words*/
.p12 {
margin-left: 20px;
margin-top: 10px;
position: absolute;
font-size: 200%;
}
/*Display top frequent words*/
.p13 {
margin-left: 20px;
margin-top: 60px;
position: absolute;
font-size: 200%;
}
.bar rect {
fill: steelblue;
}
.bar text {
fill: #fff;
font: 10px sans-serif;
}
/*Never referenced - unused*/
.div1 {
position: absolute;
height: 400px;
width: 600px;
margin-left:1000px;
}
/*Never referenced - unused*/
.div2 {
position: absolute;
margin-top: 400px;
height: 400px;
width: 600px;
margin-left: 1000px;
}
/*Never referenced - unused*/
#legend-svg {
vertical-align: top;
}
.point {
fill: steelblue;
stroke: #000;
}
</style>
<body>
<p class="p12" id="predicted_words"></p>
<p class="p13" id="frequent_words"></p>
<!-- <p class="p9"><b>Circle</b><input class="input1" type="checkbox" id="cbox4" value="fourth_checkbox"></input><input class = "input2" type="text" id="drawxy" placeholder="Select 1 pt after check"> </input> <button class = "input2" onclick="handleClick4()" value="Draw" >Draw</button></p> -->
<p class="p8"><b>Zoom</b><input class="input1" type="checkbox" id="cbox3" value="third_checkbox"></input><input class = "input2" type="text" id="zoomxy" placeholder="Select 2 pts after check"> </input> <button class = "input2" onclick="handleClick4()" value="Zoom" >Zoom</button></p>
<p class="p1" id="demo" ><b>Color by: </b></p>
<p class="p5">Options: spectrum<input class="input1" type="checkbox" id="cbox1" value="first_checkbox" onclick="handleClick3()">log<input class="input1" type="checkbox" id="cbox2" value="second_checkbox" onclick="handleClick3()"></p>
<p class="p6"><b>Click on feature: </b></p>
<p class="p7"><b>Shape by: </b></p>
<p class="p2" id="demo2" ><b>Search by: </b></p>
<p class="p10">Exact match: <input class="input1" type="checkbox" id="cbox5" value="fifth_checkbox" onclick="handleCheck()"></p>
<p class="p4" id="demo4" ><b>Transparent attribute: </b></p>
<p class="p11">Exact match: <input class="input1" type="checkbox" id="cbox6" value="sixth_checkbox" onclick="handleCheck1()"></p>
<!-- Side Table -->
<p class="p3" id="demo3"></p>
<!-- The handleClick() function will be executed when the search button is pressed -->
<form name="myform" class="form2" onSubmit="return handleClick()">
<input type="text" id="searchText" placeholder="Search text">
<input name="Submit" type="submit" value="Apply" >
</form>
<!-- The handleClick1() function will be executed when the transparent button is pressed -->
<form name="myform1" class="form1" onSubmit="return handleClick1()">
<p>Enter attribute value, and opacity values b/w 0 and 1:</p>
<input type="text" id="transpText" placeholder="Match attri. val">
<input type="text" id="opacityMatch" placeholder="Opacity (match)">
<input type="text" id="opacityNoMatch" placeholder="Opacity (no match)">
<input name="Submit" type="submit" value="Apply">
</form>
<!-- These javascript modules are used to draw the interactive plot -->
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="//d3js.org/d3-path.v0.1.min.js"></script>
<script src="//d3js.org/d3-shape.v0.6.min.js"></script>
<script src="http://axc.net/code_libraries/lasso/lasso.min.js"></script>
<script src="vendor/crossfilter.min.js"></script>
<script src="math.min.js"></script>
<script>
// The location of svg plot is determined by the following margins
var margin = {top: 90, right: 40, bottom: 40, left: 40},
width = 700 - margin.left - margin.right,
height = 750 - margin.top - margin.bottom;
// This step is performed to parse the url to identify the dataset and the default coloring column
var query = window.location.search.substring(1);
var temp_query = query.split("&");
var dicts = {};
var tvars;
for(var i=0;i<temp_query.length;i++) {
tvars = temp_query[i].split("=");
dicts[tvars[0]]=tvars[1].replace(/%20/g, " ");
}
if ("dataset" in dicts) {
dataset = dicts["dataset"];
} else {
dataset = "joined_data.csv";
}
var weights_2darray = [], biases_1darray = [], vocab_1darray = [], vectorspace_2darray = [], bow_2darray = [];
// Semantic model option set up
if ("semantic_model" in dicts && dicts["semantic_model"] == "true") {
console.log('Using semantic model.\nGetting matrices...');
var weightsfile = dataset.split(/\.t[a-z]{2}$/)[0]+'_weights.txt';
var biasesfile = dataset.split(/\.t[a-z]{2}$/)[0]+'_biases.txt';
var vocabfile = dataset.split(/\.t[a-z]{2}$/)[0]+'_vocab.txt';
var vectorfile = 'VS-' + dataset.split("_semantic")[0]+'.txt';
var bowfile = dataset.split(/\.t[a-z]{2}$/)[0]+'_bow.txt';
console.log("Reading " + bowfile);
d3.tsv(bowfile, function(text){
bow_2darray = text.map( Object.values );
bow_2darray = bow_2darray.map(function(entry) {
return entry.map(function(elem) {
return Math.round(parseFloat(elem));
});
});
});
console.log("Reading " + vectorfile);
d3.tsv(vectorfile, function(text){
vectorspace_2darray = text.map( Object.values );
vectorspace_2darray = vectorspace_2darray.map(function(arr) {
// username column ends up last in the dictionary, due to alphanumeric sort
return arr.slice(0,-1).map(function(elem) {
return parseFloat(elem);
});
});
});
console.log("Reading " + weightsfile);
d3.tsv(weightsfile, function(text){
weights_2darray = text.map( Object.values );
weights_2darray = weights_2darray.map(function(entry) {
return entry.map(function(elem) {
return parseFloat(elem);
});
});
});
console.log("Reading " + biasesfile);
d3.tsv(biasesfile, function(text){
biases_1darray = text.map( Object.values );
biases_1darray = Object.values(biases_1darray.map(Number));
});
console.log("Reading " + vocabfile);
d3.tsv(vocabfile, function(text){
vocab_1darray = text.map( Object.values );
vocab_1darray = Object.values(vocab_1darray.map(String));
});
}
/*
* value accessor - returns the value to encode for a given data object.
* scale - maps value to a visual display encoding, such as a pixel position.
* map function - maps from data value to display value
* axis - sets up axis
*/
// setup x
var xValue = function(d) { return d.x;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) {return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function(d) { return d["y"];}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var tooltip1 = d3.select("body").append("div")
.attr("class", "tooltip1")
.style("opacity", 0);
// to print all the key values pairs of a point (used to display the summary on the webpage)
var print_array = function(arr, d) {
var x = "";
for (var i=0; i<arr.length; i++) {
x = x + "<b>" + arr[i] + "</b>: " + d[arr[i]] + "<br>"
}
x = x + d.x + "<br>" + d["y"];
return x;
};
// setup fill color
var color_column;
// Can generate more with http://jnnnnn.github.io/category-colors-2L-inplace.html if want more
var d3_category20_shuffled = [
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf",
"#aec7e8", "#ffbb78", "#98df8a", "#ff9896", "#c5b0d5", "#c49c94", "#f7b6d2", "#c7c7c7", "#dbdb8d", "#9edae5"
];
// coloring will be done according to the values determined by cValue
var cValue = function(d) {return d[color_column];},
cValue2 = function(d) {return Math.log(parseFloat(d[color_column]));},
color = d3.scale.ordinal().range(d3_category20_shuffled);
// used to search a particular substring in the list of requested feature column
// used to determine whether we should add find to arri, hence the t/f -> f/t
var searchdic = function(arri, find) {
for(var i=0;i<arri.length;i++) {
if(JSON.stringify(find) === JSON.stringify(arri[i])){
return false;
}
}
return true;
}
// displays the summary in tabular form
var tabulate = function(data_tab, columns) {
var table = d3.select("body").append("table")
.attr("class", "select3"),
thead = table.append("thead"),
tbody = table.append("tbody");
// append the header row
thead.append("tr")
.selectAll("th")
.data(columns)
.enter()
.append("th")
.text(function(column) { return column; });
// create a row for each object in the data
var rows = tbody.selectAll("tr")
.data(data_tab)
.enter()
.append("tr");
// create a cell in each row for each column
var cells = rows.selectAll("td")
.data(function(row) {
return columns.map(function(column) {
return {column: column, value: row[column]};
});
})
.enter()
.append("td")
.attr("style", "font-family: Courier") // sets the font style
.html(function(d) { return d.value; });
/*
crossfilter dimensions and group by
http://animateddata.co.uk/articles/crossfilter/
*/
var output = "";
var cf = crossfilter(data_tab);
/* crossfilter currently only supports up to 32 columns) */
for (var i=0;i<columns.length && i<32;i++) {
var byParty = cf.dimension(function(p) {
return p[columns[i]]; });
output = output + "<b>" +columns[i] + "</b>" + "<br>";
var groupByParty = byParty.group();
groupByParty.top(5).forEach(function(p, i) {
output = output + p.key + ": " + p.value + "<br>";
console.log(p.key + ": " + p.value);
});
output = output + "<br>";
}
// side table
document.getElementById("demo3").innerHTML = output;
return table;
}
// create the dropdown menu
// Coloring
var dropDown = d3.select("body").append("select")
.attr("class", "select1")
.attr("name", "color_column");
// Searching
var dropDown1 = d3.select("body").append("select")
.attr("class", "select2")
.attr("name", "color_column");
// Transparent
var dropDown2 = d3.select("body").append("select")
.attr("class", "select4")
.attr("name", "color_column");
// Click on feature
var dropDown3 = d3.select("body").append("select")
.attr("class", "select5")
.attr("name", "color_column");
// Shaping
var dropDown4 = d3.select("body").append("select")
.attr("class", "select6")
.attr("name", "color_column");
var categories = [];
// category_search stores the name of column according to which searching is to be done
var category_search_data = [];
// categories stores the name of all the columns
var category_search;
categories.push("Select");
// check whether the searching column is provided in the url or not
if ("search" in dicts) {
category_search = dicts["search"];
category_search_data.push(category_search);
}
// color_column stores the name of column according to which coloring is to be done
// check whether the coloring column is provided in the url or not
if ("color" in dicts) {
color_column = dicts["color"];
categories.push(color_column);
} else {
color_column = "Select";
}
// categories_copy_color is just the copy of categories
var categories_copy_color = [];
categories_copy_color.push(color_column);
var columns = [], temp = [];
var x_max, x_min, y_max, y_min;
// column for the transparent value
var transparent_column = "Select", feature_column = "", shaping_column = "Select";
console.log('Loading main data')
// getting header from csv file to make drowdown menus
d3.tsv(dataset, function(data) {
console.log(data[0]);
temp = Object.keys(data[0]);
// remove x and y
temp.splice(temp.indexOf('x'), 1);
temp.splice(temp.indexOf('y'), 1);
for(var i=0;i<temp.length;i++)
if (temp[i] != category_search) {
category_search_data.push(temp[i]);
}
for(var i=0;i<temp.length;i++) {
// color_column already pushed
if (temp[i] != color_column) {
categories.push(temp[i]);
categories_copy_color.push(temp[i]);
}
columns.push(temp[i]);
}
// check whether the coloring column is provided in the url or not
// ?? is this necessary? color_column is already defined with the same procedure outside the function
if ("color" in dicts) {
color_column = categories[1]; // since color would be first, start with next
} else {
// color_column = "Select"
color_column = categories[0];
}
category_search = category_search_data[0];
// Searching
dropDown1.selectAll("option")
.data(category_search_data)
.enter()
.append("option")
.text(function(d) { return d;})
.text(function(d) {return d;});
// Coloring
dropDown.selectAll("option")
.data(categories_copy_color)
.enter()
.append("option")
.text(function(d) { return d;})
.text(function(d) {return d;});
// Transparent
dropDown2.selectAll("option")
.data(category_search_data)
.enter()
.append("option")
.text(function(d) { return d;})
.text(function(d) {return d;});
// Click on feature
dropDown3.selectAll("option")
.data(category_search_data)
.enter()
.append("option")
.text(function(d) { return d;})
.text(function(d) {return d;});
// Shaping
dropDown4.selectAll("option")
.data(categories)
.enter()
.append("option")
.text(function(d) { return d;})
.text(function(d) {return d;});
shaping_column = categories[0];
feature_column = category_search_data[0];
transparent_column = category_search_data[0];
});
// whenever any one of the drowdown menu's selected column is changes the plot is generated according to the value of dropdown menu selected
// Coloring
dropDown.on("change", plotting);
// Searching
dropDown1.on("change", plotting2);
// Transparent
dropDown2.on("change", plotting3);
// Click on feature
dropDown3.on("change", plotting4);
// Shaping
dropDown4.on("change", plotting5);
if ("q" in dicts) {
highlighting(dicts["q"], "", "");
} else {
highlighting("", "", "");
}
// the functions to call when the value of dropdown menu is changes
// Click on feature
function plotting4(){
feature_column = d3.event.target.value;
}
// Transparent
function plotting3(){
transparent_column = d3.event.target.value;
}
// Searching
function plotting2(){
category_search = d3.event.target.value;
}
// function to call for change event
// Coloring
function plotting(){
color_column = d3.event.target.value;
cValue = function(d) { return d[color_column];};
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
}
// function to call for change event
// Shaping
function plotting5(){
shaping_column = d3.event.target.value;
cValue = function(d) { return d[color_column];};
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
}
var zoomed = 0;
var needZoom = false;
var needDrawCircle = false;
// search event
// it will be executed when search button is pressed and points that matches the searched string will be highlighted
function handleClick(event) {
console.log(document.getElementById("searchText").value);
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
return false;
}
function handleCheck(event) {
if (document.getElementById("searchText").value) {
handleClick();
}
}
// transparent event
// it will be executed when Transparent button is pressed and points that satisfies the condition will be highlighted
function handleClick1(event) {
console.log(document.getElementById("transpText").value);
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
return false;
}
function handleCheck1(event) {
if (document.getElementById("transpText").value) {
handleClick1();
}
}
// ?? I believe this function is unused, and draw also maps to handleClick4
// it will be executed when Draw button is pressed and the plot will highlight those points that covers fixed percentage of point from the point obtained by mouse click
function handleClick2(event){
shaping_column = "Select";
color_column = "Select";
myForm.searchText.value = 0;
myForm1.transpText.value = 0;
myForm1.opacityMatch.value = 0;
myForm1.opacityNoMatch.value = 0;
dropDown4.property( "value", "Select" );
dropDown.property( "value", "Select" );
highlighting("", "", "");
return false;
}
// spectrum / log event
// it will be executed when spectrum/log is checked
// ?? Can we collapse handleClick1,3,4?
function handleClick3(event) {
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
}
// it will be executed when (?? draw and) zoom button is pressed and the plot will zoomed out according to the points obtained by mouse click event
function handleClick4(){
if (!document.getElementById('cbox3').checked) {
document.getElementById("zoomxy").value = ""; // clear the textbox
}
val_search = document.getElementById("searchText").value;
val_transp = document.getElementById("transpText").value;
val_opacityMatch = document.getElementById("opacityMatch").value;
val_opacityNoMatch = document.getElementById("opacityNoMatch").value;
needZoom = true;
highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch);
}
// Checks the url query for name=value and extracts the value
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function linspace(start, end, n) {
var out = [];
var delta = (end - start) / (n - 1);
var i = 0;
while(i < (n - 1)) {
out.push(start + (i * delta));
i++;
}
out.push(end);
return out;
}
coordinatesx = [];
coordinatesy = [];
// provides different colored spectrum
var scale_d = {
'puOr11': ['#7f3b08', '#b35806', '#e08214', '#fdb863', '#fee0b6', '#f7f7f7', '#d8daeb', '#b2abd2', '#8073ac', '#542788', '#2d004b'],
'spectral8': ['#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#e6f598', '#abdda4', '#66c2a5', '#3288bd'],
'redBlackGreen': ['#ff0000', '#AA0000', '#550000', '#005500', '#00AA00', '#00ff00'],
};
scale = scale_d['spectral8'];
// function for plotting
function highlighting(val_search, val_transp, val_opacityMatch, val_opacityNoMatch) {
var svg;
var temp1 = [], temp2 = [], temp3 = [];
var dict1 = {};
// to remove the existing svg plot if any and clear side table
document.getElementById("demo3").innerHTML = "";
document.getElementById("predicted_words").innerHTML = "";
document.getElementById("frequent_words").innerHTML = "";
d3.select("svg").remove();
d3.select("table").remove();
// function zoom() {
// svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
// }
// the location of svg image will be determined
svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform","translate(" + margin.left + "," + margin.top + ")");
/* https://github.com/skokenes/D3-Lasso-Plugin
plugin also handles selected and possible settings */
// Lasso starts
var lasso_start = function() {
d3.select("table").remove();
document.getElementById("demo3").innerHTML = "";
lasso.items()
.attr("r",3.5) // reset size
.style("fill",null) // clear all of the fills (greys out)
.classed({"not_possible":true,"selected":false}); // style as not possible
};
var lasso_draw = function() {
// Style the possible dots
lasso.items().filter(function(d) {return d.possible===true})
.classed({"not_possible":false,"possible":true});
// Style the not possible dot
lasso.items().filter(function(d) {return d.possible===false})
.classed({"not_possible":true,"possible":false})
.style("stroke", "#000");
};
var lasso_end = function() {
// Reset the color of all dots
lasso.items()
.style("fill", function(d) { return color(d[color_column]); });
// Style the selected dots
lasso.items().filter(function(d) {return d.selected===true})
.classed({"not_possible":false,"possible":false})
.attr("r",6.5);
// get values for table -> array inside a list
var zsx = lasso.items().filter(function(d) {return d.selected===true});
x_values = [];
y_values = [];
// adjust the x and y values
for (var i=0; i<zsx[0].length; i++) {
x_values.push(((((zsx[0][i].getBBox().x+6.5) * (x_max - x_min))/width + x_min )));
y_values.push(((((zsx[0][i].getBBox().y+6.5) * (y_min - y_max))/height + y_max)));
}
var selected_data=[], selected_data_indices=[];
// Compare every selected point to all points (tempX)
// in order to match coordinates with actual data
for (var ii=0;ii<x_values.length;ii++) {
console.log("lasso_end gathering selected data");
console.log(temp1.length);
for (var jj=0;jj<temp1.length;jj++) {
x_values[ii] = +(x_values[ii].toFixed(3));
y_values[ii] = +(y_values[ii].toFixed(5));
if ( (x_values[ii] === +(temp1[jj].toFixed(3))) && (y_values[ii] === +(temp2[jj].toFixed(5))) ) {
all_values = {};
for (var k=1;k<categories.length;k++) {
all_values[categories[k]] = (dict1[categories[k]][jj]);
}
if(searchdic(selected_data,all_values)==true){
selected_data.push(all_values);
selected_data_indices.push(jj);
break;
}
}
}
}
// render the table for the points selected by lasso
if (selected_data.length > 0) {
console.log("Rendering table...");
console.log(selected_data);
console.log(columns);
console.log(x_values);
var peopleTable = tabulate(selected_data, columns, x_values);
if ("semantic_model" in dicts && dicts["semantic_model"] == "true") {
console.log("Predicting words...");
classify(selected_data_indices, vectorspace_2darray, weights_2darray, biases_1darray, vocab_1darray);
benchmark(selected_data_indices, bow_2darray, vocab_1darray);
}
}
// Reset the style of the not selected dots (we made them 0.5 smaller)
lasso.items().filter(function(d) {return d.selected===false})
.classed({"not_possible":false,"possible":false})
.attr("r",3)
.style("stroke", "#000");
};
// Create the area where the lasso event can be triggered
var lasso_area = svg.append("rect")
.attr("width",width)
.attr("height",height)
.style("opacity",0);
// Define the lasso
var lasso = d3.lasso()
.closePathDistance(75) // max distance for the lasso loop to be closed
.closePathSelect(true) // can items be selected by closing the path?
.hoverSelect(true) // can items by selected by hovering over them?
.area(lasso_area) // area where the lasso can be started
.on("start",lasso_start) // lasso start function
.on("draw",lasso_draw) // lasso draw function
.on("end",lasso_end); // lasso end function
// Init the lasso object on the svg:g that contains the dots
svg.call(lasso);
var classify = function(indices, vs_source, weights_source, biases_source, vocab_source) {
var sum_vectors = vs_source[indices[0]];
for (i=1; i < indices.length; i++) {
sum_vectors = math.add(sum_vectors, vs_source[indices[i]]);
}
var avg_vector = sum_vectors.map(function(x) { return x / indices.length; });
var mul = math.multiply(avg_vector, weights_source);
var add = math.add(mul, biases_source);
// get indices of 10 greatest elements
var topIndices = findIndicesOfMax(add, 10);
console.log("Top predicted vocab words:");
var strbuilder = "Predicted words:";
for (i=0; i<10; i++) {
console.log((i+1) + ": " + vocab_source[topIndices[i]]);
strbuilder += " " + vocab_source[topIndices[i]] + ",";
}
document.getElementById("predicted_words").innerHTML = strbuilder.slice(0, -1);
}
var benchmark = function(indices, bow_source, vocab_source) { // may need to adjust
var num_indices = indices.length;
var vocab_freq = bow_source[indices[0]];
for (i=1; i < num_indices; i++) {
vocab_freq = math.add(vocab_freq, bow_source[indices[i]]);
}
// get indices of 10 greatest elements
var topIndices = findIndicesOfMax(vocab_freq, 10);
console.log("Most frequent words:");
var strbuilder = "Most frequent words:";
for (i=0; i<10; i++) {
console.log((i+1) + ": " + vocab_source[topIndices[i]]);
strbuilder += " " + vocab_source[topIndices[i]] + ",";
}
document.getElementById("frequent_words").innerHTML = strbuilder.slice(0, -1);
}
// Utility function used for predicting words in semantic setting
// Source: https://stackoverflow.com/a/11792230/7100714
function findIndicesOfMax(inp, count) {
var outp = new Array();
for (var i = 0; i < inp.length; i++) {
outp.push(i);
if (outp.length > count) {
outp.sort(function(a, b) { return inp[b] - inp[a]; });
outp.pop();
}
}
return outp;
}
console.log('Loading main data, again') // load data
d3.tsv(dataset, function(error, data) {
// change string (from CSV) into number format
var numerics = {}, symbol = {};
//Omitting Select (0)
for(var i=1;i<categories.length;i++) {
// initialize the value for each category key to empty list
dict1[categories[i]] = [];
// initialize all categories as numeric
numerics[categories[i]] = 1;
}
counter = 0;
data.forEach(function(d) {
// coerce the data to numbers
d.x = +d.x;
d["y"] = +d["y"];
for(var i=1;i<categories.length;i++){
// add every attribute of point to the {category:[val1,val2,...]}
dict1[categories[i]].push(d[categories[i]]);
// revoke a category's numerics status if find an entry has a non-Int or non-null value for that category
numerics[categories[i]] = numerics[categories[i]] && (d[categories[i]] == "" || d[categories[i]] == parseFloat(d[categories[i]]));
}
// fill the symbol dictionary with all possible values of the shaping column as keys
// value is the order of points
if (!(d[shaping_column] in symbol)) {
symbol[d[shaping_column]] = counter;
counter = counter + 1;
}
// push all x values, y values, and all category search values into temp1/2/3
temp1.push(d.x);
temp2.push(d["y"]);