forked from ccywch/kg_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswerBox-v4-small.js
728 lines (643 loc) · 39.2 KB
/
answerBox-v4-small.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
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
// ==UserScript==
// @name knowledgeGraph-small
// @namespace Chen Chunyang
// @require http://d3js.org/d3.v3.min.js
// @require https://d3js.org/d3.v3.min.js
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @require http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js
// @require http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js
// @resource kg_css https://graphofknowledge.appspot.com/dist/plugin.css
// @include https://www.google.com*
// @version 3.0
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
//var kg_cssSrc = GM_getResourceText ("kg_css"); //add external css file for tip event
//GM_addStyle (kg_cssSrc);
// All css files should appear in the end of the document, so as to load the document content faster
// function definition, used for drawing trend graph
function tagTrend(data, width_raw, height_raw, top_margin, position){
if (top_margin < 8){
top_margin = 8;
}
var margin = {
top: top_margin, // if there is graph title, set this number = x + 20
right: 12, // if there is line chart tag (line tag), set this number = 80
bottom: 22,
left: 45
},
width = width_raw - margin.left - margin.right,
height = height_raw - margin.top - margin.bottom;
var border = 0;
var bordercolor = 'gray';
var parseDate = d3.time.format("%Y%m%d").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.popularity);
});
var svg = d3.select(position).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 + ")")
.attr("border",border)
;
//add border to the svg
var borderPath = svg.append("rect")
.attr("x", -45)
.attr("y", -0) // if this is graph title, set this number = -20
.attr("height", height_raw - top_margin)
.attr("width", width_raw)
.style("stroke", bordercolor)
.style("fill", "none")
.style("stroke-width", border);
color.domain(d3.keys(data[0]).filter(function (key) {
return key !== "date";
}));
data.forEach(function (d) {
d.date = parseDate(d.date);
});
var cities = color.domain().map(function (name) {
return {
name: name,
values: data.map(function (d) {
return {
date: d.date,
popularity: +d[name]
};
})
};
});
x.domain(d3.extent(data, function (d) {
return d.date;
}));
y.domain([
d3.min(cities, function (c) {
return d3.min(c.values, function (v) {
return v.popularity;
});
}),
d3.max(cities, function (c) {
return d3.max(c.values, function (v) {
return v.popularity;
});
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.style("font-size", "11px")
.call(xAxis);
// add the y axis and y-label
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(0,0)")
.style("font-size", "11px")
.call(yAxis);
svg.append("text")
.attr("class", "ylabel")
.attr("y", 0 - margin.left + 45) // x and y switched due to rotation!!
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("transform", "rotate(-90)")
.style("text-anchor", "middle")
.style("font-size", "11px")
.text("question number /month");
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function (d) {
return line(d.values);
})
.style("stroke", function (d) {
return color(d.name);
});
} // end of function tagTrend()
var fireOnHashChangesToo = true;
var pageURLCheckTimer = setInterval (
function () {
if (this.lastPathStr !== location.pathname || this.lastQueryStr !== location.search || (fireOnHashChangesToo && this.lastHashStr !== location.hash)
) {
this.lastPathStr = location.pathname;
this.lastQueryStr = location.search;
this.lastHashStr = location.hash;
gmMain ();
}
}, 200
);
function gmMain () {
console.log ('A "New" page has loaded.'); //for debugging
// DO WHATEVER YOU WANT HERE.
var checkTopPanel = document.getElementById("topPanel") ;
var checkRightColumn = document.getElementById("rightColumn");
//var checkGraph = document.getElementById("graph");
if (checkTopPanel != null) { //delete the dom if exists
checkTopPanel.parentElement.removeChild(checkTopPanel);
}
if (checkRightColumn != null) { //delete the dom if exists
checkRightColumn.parentElement.removeChild(checkRightColumn);
}
/* // remove google advertisements
var ads = document.getElementById("tvcap");
if (ads != null) { //delete the dom if exists
ads.parentElement.removeChild(ads);
}
*/
var input = document.getElementById("lst-ib").value.toLowerCase(); //Get the content in the search bar as the tag (lowercase and replace blank)
// Insert Answering Box (right column) into the Google search page
// There are 2 sections in answering-box. If there is no content in certain section, then do not appendChild to answering-box.
var str_HTML;
var baseHeight = 120 + $('#rhs').height(); //Align our answer box behind Google's direct answer or ads. Google's navigation bar height = 120
var winWidth = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
var rightColumnWidth = Math.max((winWidth / 2 + 50), 500);
var layoutTemplate = 0; // 0-large screen(1500px+); 1-medium screen(1000-1500px); 2-small screen(1000px-)
if (winWidth < 1500) {
if (winWidth > 1200){
layoutTemplate = 1;
} else {
layoutTemplate = 2;
}
}
var rightColumn = document.createElement("div");
rightColumn.id = "rightColumn";
rightColumn.style = 'font-size:16px; position:absolute; top:'+ (baseHeight+10) +'px; left:'+ Math.max((winWidth/2-80), 700) +'px; width: '+ rightColumnWidth +'px';
document.body.appendChild(rightColumn);
var topPanel = document.createElement("div");
topPanel.className = 'container img-rounded';
topPanel.id = 'topPanel';
// The following 2 lines could lead to some conflicts when re-submitting a search. Therefore, using substitute method at the end of the main function (i.e. gmMain())
var insertPoint = $('#topabar').get(0); //var insertPoint = $('#extabar').get(0).firstChild;
insertPoint.parentElement.insertBefore(topPanel,insertPoint);
// initial analysis of input
// algorithm
var tomTask = 0;
var tag, graphResult;
var inputList = [];
var languageList = ['c#', 'java', 'python', 'c++', 'javascript', 'php']; // for Analogies
var rsLanguage = '';
var keywordList = ['library', 'libraries', 'lib', 'libs', 'tool', 'tools', 'software', 'softwares', 'language', 'languages'];
var showConcept = true;
var showLibrary = true;
var showLanguage = true;
if (input.replace(/\+\+/g,"@@@").indexOf("+") > 0){
// (+)sign exists -----> do switch 1 only
tomTask = 1;
}
else {
// (+)sign is not founded
input = input.replace(/\+\+/g, '@@@').replace(/\+/g, ' ').replace(/@@@/g, '++');
inputList = input.split(" ");
if (inputList.length<6) { // no more than 5 spaces in the input
// check whether there is language name in the inputs
for (i=0; i<inputList.length; i++){
for (j=0; j<languageList.length; j++){
if (inputList[i] == languageList[j]){
rsLanguage = inputList[i];
input = input.replace(rsLanguage, '').replace(/ +/g, ' ').replace(/^ */g, '').replace(/ *$/g, '');
j = languageList.length; // for break purpose (quit for-loop)
i = inputList.length; // for break purpose (quit for-loop)
}
}
}
if (rsLanguage) { // rsLanguage is not null
// ------> do switch 2
tomTask = 2;
}
else {
// ------> do switch default
tomTask = 0; // default value
// check whether there is keyword (initial defined) in the inputs
for (i=0; i<inputList.length; i++){
for (j=0; j<keywordList.length; j++){
if (inputList[i] == keywordList[j]){
showConcept = false;
input = input.replace(keywordList[j], '').replace(/ +/g, ' ').replace(/^ */g, '').replace(/ *$/g, '');
if (keywordList[j] == 'lib' || keywordList[j] == 'libs' || keywordList[j] == 'library' || keywordList[j] == 'libraries') {
showLanguage = false;
for (k=0; k<inputList.length; k++){
// examine if input contains (similar,equivalent)
if (inputList[k] == 'similar' || inputList[k] == 'equivalent'){
// -----> do switch 2
tomTask = 2;
input = input.replace(inputList[k], '').replace(/ +/g, ' ').replace(/^ */g, '').replace(/ *$/g, '');
k = inputList.length; // quit for-loop
}
}
} // if contains (lib||library)
else {
if (keywordList[j] == 'language' || keywordList[j] == 'languages'){
showLibrary = false;
}
}
j = keywordList.length; // for break purpose (quit for-loop)
i = inputList.length; // for break purpose (quit for-loop)
} // if contains (keywords)
} // for j
} // for i
}
// get tag (graph content)
console.log('---'+ input +'---');
console.log(input.replace(/#/g,"+++").replace(/ /g,"&&")); //"replace" method can only replace the first occurrence, so we use regex here
var result = $.ajax({type: "GET", url: "https://graphofknowledge.appspot.com/tagidjson/"+input.replace(/#/g,"+++").replace(/ /g,"&&"), async: false}).responseText;
tag = result.split("&&")[0]; //the tag
graphResult = result.split("&&")[1]; //the kg
if (tag == ""){
// no tag returned
if (rsLanguage) { // rsLanguage is not null
input = input + '&&' + rsLanguage; // retrieve the initial input
console.log('---'+ input +'---');
result = $.ajax({type: "GET", url: "https://graphofknowledge.appspot.com/tagidjson/"+input.replace(/#/g,"+++").replace(/ /g,"&&"), async: false}).responseText;
tag = result.split("&&")[0]; //the tag
graphResult = result.split("&&")[1];
if (tag == ''){
// still no tag returned
tomTask = -1;
}
}
else {
// no rsLanguage (and also no tag)
tomTask = -1;
}
}
}
else {
// ------> do switch -1 only (do nothing)
tomTask = -1; // more than 6 spaces in the input, do nothing
console.log('the input is too long');
}
}
for (tom_i=0; tom_i<1; tom_i++){ // i_control may be changed, then do switch again
switch(tomTask){
case -1:
// do nothing
break;
default:
// wiki and top-questions (right panels)
// (1) Get tagWiki content
var wikiResult = JSON.parse($.ajax({type: "GET", url: "https://api.stackexchange.com/2.2/tags/"+tag.split("_").pop().replace(/#/g,"%23")+"/wikis?site=stackoverflow", async: false}).responseText);
str_HTML = '';
if (wikiResult["items"] && wikiResult["items"].length != 0 && graphResult != "") { // the first term means xxx is not null, and both wiki data and graph data exists
// Insert tagWiki into right column
str_HTML += '<div class="panel-heading"><div class="row"><div class="col-md-5">'+ tag +'</div><div class="col-md-7 text-right">Community interest in Stack Overflow</div></div></div><div style="font-size:0.875em;"><div class="row"><div class="col-md-7" id="wikiPanelL"><div class="panel-body">';
str_HTML += '<p>'+ wikiResult["items"][0]["excerpt"] +'</p>';
// Insert a link to knowledge graph website
var linkURL = "";
// Direct to techGraph or techTask pages
if (tag.indexOf("_") == -1)
{linkURL = "https://graphofknowledge.appspot.com/tagid/" +tag.replace(/#/g,"+++");}
else
{linkURL = "https://graphofknowledge.appspot.com/techtask/" +tag.replace(/#/g,"+++").replace("_","&");}
str_HTML += '<span style="font-style:italic;font-size:0.875em;">Refer to <a href="'+ linkURL +'" target="_blank">'+ linkURL.substr(0,37) +' <span class="glyphicon glyphicon-option-horizontal"></span> <span class="glyphicon glyphicon-arrow-right"></span></a></span>';
//buttonPosition.onclick = redirectToUrl(tag.replace("#","+++"));
str_HTML += '</div></div><div class="col-md-5" id="wikiPanelR"><svg id="trendGraph"></svg></div></div></div>'; // add trend graph panel<html>, only after rendering drawing function could be called
var wikiPanel = document.createElement("div");
wikiPanel.id = "wiki";
wikiPanel.className = "panel panel-primary";
wikiPanel.innerHTML = str_HTML;
rightColumn.appendChild(wikiPanel);
// rendering(drawing) trend graph
var trendData = {};
trendData = $.ajax({
type: "GET",
url: 'https://128.199.241.136:9001/tagTrend/' + tag.replace(/#/g,"+++").replace(/ /g,"&&"),
async: false,
dataType: 'json',
});
console.log(trendData); // for debug purpose
if (trendData['readyState'] == 4 && trendData['status'] == 200) {
if (trendData.responseJSON['tag_trend'].length > 0) { // returned value is not null
var chartWidth = parseInt(rightColumnWidth * (5/12) - 27);
var chartHeight = parseInt(rightColumnWidth * (5/12) * (5/11));
var chartTopMargin = parseInt(document.getElementById('wikiPanelL').offsetHeight - chartHeight);
if (chartTopMargin > 0){
chartTopMargin = parseInt(chartTopMargin / 2) + 5;
chartHeight += chartTopMargin;
}
else {
chartTopMargin = 0;
}
document.getElementById('trendGraph').style.height = chartHeight + 'px';
document.getElementById('trendGraph').style.width = chartWidth + 'px';
tagTrend(trendData.responseJSON['tag_trend'], chartWidth, chartHeight, chartTopMargin, "#trendGraph");
}
} // if (trend data exist and returned correctly)
} // if (wiki and graph exist)
// (2) insert [A]insert related content and knowledge graph
// and get [C-A]related technologies/tools/libraries (from inside of knowledge graph); (C-A1) add to top panel; (C-A2) add to right panel
// and get [C-B]related languages (from inside of knowledge graph); (C-B1) add to top panel; (C-B2) add to right panel
// and get [C-C]other concepts from knowledge graph (AllTags = Lib + Language + Other); (C-C1) add to top panel; (C-C2) add to right panel
var relatedLib = {};
relatedLib = $.ajax({
type: "GET",
url: 'https://128.199.241.136:9001/similarLib/' + tag.replace(/#/g,"+++").replace(/ /g,"&&"),
async: false,
dataType: 'json',
});
console.log(relatedLib); // for debug purpose
if (relatedLib['readyState'] == 4 && relatedLib['status'] == 200) {
if (relatedLib.responseJSON['lib_list'].length > 0) { // returned value is not null
// (A)
str_HTML = '';
switch(layoutTemplate){ // different template according to screen size
case 1:
var kgWidth = parseInt(rightColumnWidth * (9/12) - 19);
var kgHeight = parseInt(rightColumnWidth * (9/12) - 19);
var numForDisplay = parseInt((kgHeight - 90) / 35); // used to measure related-lib/concepts panel (341px display 7 items; 381px--8; 413--9; 522--12)
var labelWidth = 12; // used to set width(col-12 or 10) of each term in lib/lang/concept, as well as to set the number of characters in each label (y=96-7x, for col-10 display 26 letters)
str_HTML += '<div class="panel-heading"><div class="row"><div class="col-md-3">Related';
if (relatedLib.responseJSON['lib_list'].length >= numForDisplay || relatedLib.responseJSON['language_list'].length >= numForDisplay || relatedLib.responseJSON['concept_list'].length >= numForDisplay){
str_HTML += '<button id="hideRightBtn" type="button" class="btn btn-xs pull-right" style="background-color:#AABBCC; color:white;">show more <span class="glyphicon glyphicon-chevron-down"></span></button>';
}
str_HTML += '</div><div class="col-md-1"> </div><div class="col-md-8">Knowledge Graph</div></div></div>';
str_HTML += '<div class="row"><div class="col-md-3" id="kgGraphPanelL">';
str_HTML += '<ul class="nav nav-tabs"><li class="active"><a data-toggle="tab" href="#kgTab1" style="padding:3px 8px 3px 8px;font-size:0.875em;font-style:italic;"><abbr title="Library">Lib</abbr></a></li><li><a data-toggle="tab" href="#kgTab2" style="padding:3px 8px 3px 8px;font-size:0.875em;font-style:italic;"><abbr title="Language">Lan</abbr></a></li><li><a data-toggle="tab" href="#kgTab3" style="padding:3px 8px 3px 8px;font-size:0.875em;font-style:italic;"><abbr title="Concept">Con</abbr></a></li></ul><div class="tab-content" style="padding-top:10px;">';
str_HTML += '<div id="kgTab1" class="tab-pane fade in active"></div>';
str_HTML += '<div id="kgTab2" class="tab-pane fade"></div>';
str_HTML += '<div id="kgTab3" class="tab-pane fade"></div>';
str_HTML += '</div></div><div class="col-md-9" id="kgGraphPanelR"><div style="padding:6px 0 0 6px;"><svg id="kgGraph"></svg></div></div></div>';
break;
case 2:
var kgWidth = rightColumnWidth - 12;
var kgHeight = rightColumnWidth - 12;
var numForDisplay = 6;
var labelWidth = 10; // used to set width(col-12 or 10) of each term in lib/lang/concept, as well as to set the number of characters in each label (y=96-7x, for col-10 display 26 letters)
str_HTML += '<div class="panel-heading">Related';
if (relatedLib.responseJSON['lib_list'].length >= numForDisplay || relatedLib.responseJSON['language_list'].length >= numForDisplay || relatedLib.responseJSON['concept_list'].length >= numForDisplay){
str_HTML += '<button id="hideRightBtn" type="button" class="btn btn-xs pull-right" style="background-color:#AABBCC; color:white;">show more <span class="glyphicon glyphicon-chevron-down"></span></button>';
}
str_HTML += '</div>';
str_HTML += '<div class="row"><div class="col-md-12" id="kgGraphPanelL">';
str_HTML += '<ul class="nav nav-tabs"><li class="active"><a data-toggle="tab" href="#kgTab1" style="line-height:8px;font-size:0.875em;font-style:italic;">Library</a></li><li><a data-toggle="tab" href="#kgTab2" style="line-height:8px;font-size:0.875em;font-style:italic;">Language</a></li><li><a data-toggle="tab" href="#kgTab3" style="line-height:8px;font-size:0.875em;font-style:italic;">Concept</a></li></ul>';
str_HTML += '<div class="tab-content" style="padding-top:10px;">';
str_HTML += '<div id="kgTab1" class="tab-pane fade in active"></div><div id="kgTab2" class="tab-pane fade"></div><div id="kgTab3" class="tab-pane fade"></div>';
str_HTML += '</div></div></div>';
str_HTML += '<div class="panel-heading">Knowledge Graph</div>';
str_HTML += '<div class="row"><div class="col-md-12" id="kgGraphPanelR"><div style="padding:6px 7px 0 5px;"><svg id="kgGraph"></svg></div></div></div>';
break;
default: // case 0 (large screen, 1500px+)
var kgWidth = parseInt(rightColumnWidth * (7/12) - 19);
var kgHeight = parseInt(rightColumnWidth * (7/12) - 19);
var numForDisplay = parseInt((kgHeight - 90) / 35); // used to measure related-lib/concepts panel (341px display 7 items; 381px--8; 413--9; 522--12)
var labelWidth = 10; // used to set width(col-12 or 10) of each term in lib/lang/concept, as well as to set the number of characters in each label (y=96-7x, for col-10 display 26 letters)
str_HTML += '<div class="panel-heading"><div class="row"><div class="col-md-5">Related';
if (relatedLib.responseJSON['lib_list'].length >= numForDisplay || relatedLib.responseJSON['language_list'].length >= numForDisplay || relatedLib.responseJSON['concept_list'].length >= numForDisplay){
str_HTML += '<button id="hideRightBtn" type="button" class="btn btn-xs pull-right" style="background-color:#AABBCC; color:white;">show more <span class="glyphicon glyphicon-chevron-down"></span></button>';
}
str_HTML += '</div><div class="col-md-1"> </div><div class="col-md-6">Knowledge Graph</div></div></div>';
str_HTML += '<div class="row"><div class="col-md-5" id="kgGraphPanelL">';
str_HTML += '<ul class="nav nav-tabs"><li class="active"><a data-toggle="tab" href="#kgTab1" style="line-height:8px;font-size:0.875em;font-style:italic;">Library</a></li><li><a data-toggle="tab" href="#kgTab2" style="line-height:8px;font-size:0.875em;font-style:italic;">Language</a></li><li><a data-toggle="tab" href="#kgTab3" style="line-height:8px;font-size:0.875em;font-style:italic;">Concept</a></li></ul><div class="tab-content" style="padding-top:10px;">';
str_HTML += '<div id="kgTab1" class="tab-pane fade in active"></div>';
str_HTML += '<div id="kgTab2" class="tab-pane fade"></div>';
str_HTML += '<div id="kgTab3" class="tab-pane fade"></div>';
str_HTML += '</div></div><div class="col-md-7" id="kgGraphPanelR"><div style="padding-top:6px;"><svg id="kgGraph"></svg></div></div></div>';
}
var kgPanel = document.createElement('div');
kgPanel.id = "kgPanel";
kgPanel.className = "panel panel-info";
kgPanel.innerHTML = str_HTML;
rightColumn.appendChild(kgPanel);
if (graphResult != "") { // knowledge graph data is not null
var graphContent = JSON.parse(graphResult);
var edgeDistance = 80*graphContent["links"].length/graphContent["nodes"].length; //the edge distance depends on the ratio of edge and node number
var scaleAdj = kgWidth / 500; // default width of the chart is 500px (500px width is the best fit)
var scaleAdj = parseInt(Math.sqrt(scaleAdj*scaleAdj) * 100) / 100; // square scale or linear scale
var kgTopMargin = parseInt(document.getElementById('kgGraphPanelL').offsetHeight - document.getElementById('kgGraphPanelR').offsetHeight);
if (kgTopMargin > 0){
kgTopMargin = parseInt((document.getElementById('wikiPanelL').offsetHeight - kgHeight) / 2);
kgHeight += kgTopMargin;
}
else {
kgTopMargin = 0;
}
document.getElementById('kgGraph').style.height = (kgHeight) + 'px';
document.getElementById('kgGraph').style.width = (kgWidth) + 'px';
knowledgeGraph(graphContent, kgWidth, kgHeight, 0, edgeDistance, "#kgGraph", scaleAdj);
} // if (knowledge graph data exists)
// (C-A2)
if (showLibrary){
str_HTML = '';
for (var i=0; i<relatedLib.responseJSON['lib_list'].length; i++) {
if (i >= numForDisplay) {
str_HTML += '<div id="lib_right_div'+i+'" style="display:none" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['lib_list'][i][0] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['lib_list'][i][0] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['lib_list'][i][0].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
} else {
str_HTML += '<div id="lib_right_div'+i+'" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['lib_list'][i][0] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['lib_list'][i][0] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['lib_list'][i][0].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
}
}
document.getElementById('kgTab1').innerHTML = str_HTML;
}
// (C-B2)
if (showLanguage){
str_HTML = '';
for (var i=0; i<relatedLib.responseJSON['language_list'].length; i++) {
if (i >= numForDisplay) {
str_HTML += '<div id="lang_right_div'+i+'" style="display:none" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['language_list'][i] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['language_list'][i] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['language_list'][i].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
} else {
str_HTML += '<div id="lang_right_div'+i+'" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['language_list'][i] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['language_list'][i] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['language_list'][i].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
}
}
//if (relatedLib.responseJSON['language_list'].length >= numForDisplay){
// str_HTML += '<button id="hideRightBtn" type="button" class="btn btn-xs" style="background-color:#AABBCC; color:white">show more <span class="glyphicon glyphicon-chevron-down"></span></button>';
//}
document.getElementById('kgTab2').innerHTML = str_HTML;
}
// (C-C2)
if (showConcept){
str_HTML = '';
for (var i=0; i<relatedLib.responseJSON['concept_list'].length; i++) {
if (i >= numForDisplay) {
str_HTML += '<div id="con_right_div'+i+'" style="display:none" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['concept_list'][i] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['concept_list'][i] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['concept_list'][i].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
} else {
str_HTML += '<div id="con_right_div'+i+'" class="col-md-'+labelWidth+'"><abbr title="'+ relatedLib.responseJSON['concept_list'][i] +'"><pre style="padding:3px 0px 3px 10px;"><a href="http://stackoverflow.com/tags/'+ relatedLib.responseJSON['concept_list'][i] +'/info" target="_blank" class="btn btn-link" style="padding:0;">'+ relatedLib.responseJSON['concept_list'][i].substr(0,(96-7*labelWidth)) +'</a></pre></abbr></div>';
}
}
//if (relatedLib.responseJSON['concept_list'].length >= numForDisplay){
// str_HTML += '<button id="hideRightBtn" type="button" class="btn btn-xs" style="background-color:#AABBCC; color:white">show more <span class="glyphicon glyphicon-chevron-down"></span></button>';
//}
document.getElementById('kgTab3').innerHTML = str_HTML;
}
rightColumn.style.top = (parseInt(rightColumn.style.top) + topPanel.offsetHeight - 10) + 'px';
$('#hideRightBtn').get(0).onclick = function(){
if (this.innerHTML.substr(0,1) == 'h'){
for (i=numForDisplay; i<relatedLib.responseJSON['lib_list'].length; i++){
$('#lib_right_div'+i).get(0).style.display = 'none';
}
for (i=numForDisplay; i<relatedLib.responseJSON['language_list'].length; i++){
$('#lang_right_div'+i).get(0).style.display = 'none';
}
for (i=numForDisplay; i<relatedLib.responseJSON['concept_list'].length; i++){
$('#con_right_div'+i).get(0).style.display = 'none';
}
this.innerHTML = 'show more <span class="glyphicon glyphicon-chevron-down"></span>';
}
else {
for (i=numForDisplay; i<relatedLib.responseJSON['lib_list'].length; i++){
$('#lib_right_div'+i).get(0).style.display = '';
}
for (i=numForDisplay; i<relatedLib.responseJSON['language_list'].length; i++){
$('#lang_right_div'+i).get(0).style.display = '';
}
for (i=numForDisplay; i<relatedLib.responseJSON['concept_list'].length; i++){
$('#con_right_div'+i).get(0).style.display = '';
}
this.innerHTML = 'hide... <span class="glyphicon glyphicon-chevron-up"></span>';
}
}; // $('#hideRightBtn')
}
} // if (tag returned correctly, i.e. not null)
} // switch
} // for
} // function gmMain
//Draw knowledge graph
function knowledgeGraph(featureContent, width_raw, height_raw, offset, distance, position, scaleAdjust) {
var margin = {
top: 0,
right: 0,
bottom: 0,
left: offset
},
width = width_raw - margin.left - margin.right,
height = height_raw - margin.top - margin.bottom;
var border = 1;
var bordercolor = 'gray';
var color = d3.scale.category20();
var svg = d3.select(position).append("svg")
// .attr("width", width + margin.left + margin.right)
// .attr("height", height + margin.top + margin.bottom)
.attr("border", border)
.attr("id", "kgInside")
;
//add border to the svg
var borderPath = svg.append("rect")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("height", height)
.attr("width", width)
.style("stroke", bordercolor)
.style("fill", "none")
.style("stroke-width", border);
var force = d3.layout.force()
.gravity(Math.max(Math.min((0.1 - 0.06 / 1.2 * scaleAdjust), 0.07), 0.02)) // (larger scale --> wider/smaller number) 100%-->0.05; 160%-->0.02; 60%-->0.07 (0.03<= x <=0.07)
.charge(-130)
.linkDistance(distance)
.size([width, height]);
//Set up tooltip
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-3, 0])
.html(function (d) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "https://api.stackexchange.com/2.2/tags/"+d.name.replace("#","%23")+"/wikis?site=stackoverflow&key=IQXyZwA1rHRM4rguoGZ)xQ((", false );
xmlHttp.send();
return JSON.parse(xmlHttp.responseText)["items"][0]["excerpt"].split(". ")[0] + ".</span>";
});
svg.call(tip);
var json = featureContent;
force
//.alpha(10)
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.style("stroke", function(d) { return color(d.color); })
.attr("class", "link");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag)
.on('dblclick', reDirect)
.on('mouseover', connectedNodes)
.on('mouseout', allNodes)
.on('contextmenu', function(d){d3.event.preventDefault();tip.show(d);})
.on('mouseleave', tip.hide)
;
node.append("circle")
.attr("r", function(d) { return parseInt(d.degree * Math.max(1, scaleAdjust));}) //if zoomed-in, the circle should look larger
//.attr("r", function(d) { return parseInt(d.degree);})
.style("fill", function (d) {return color(d.group);});
node.append("text")
.attr("dx", 3) //It means the offset of label and circle
.attr("dy", ".35em")
.text(function(d) { return d.name; })
//.style("stroke", "gray")
.style("font-weight", "bold")
.style("font-family", "Arial")
.style("font-size",function(d) { return (Math.max(10,parseInt(d.degree*1.6)))+'px'; });
force.on("tick", function() {
var radius = 10;
//node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
// .attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
scaleAdjust = Math.min(1, scaleAdjust); // no need to increase the edge length (even if it is zoomed-in), but it should decrease the edge length if it is zoomed-out
node.attr("transform", function(d) { return "translate(" + (Math.max(radius, Math.min(width - radius, d.x)) * scaleAdjust) + "," + (Math.max(radius, Math.min(height - radius, d.y)) * scaleAdjust) + ")"; });
link.attr("x1", function(d) { return (d.source.x * scaleAdjust); })
.attr("y1", function(d) { return (d.source.y * scaleAdjust); })
.attr("x2", function(d) { return (d.target.x * scaleAdjust); })
.attr("y2", function(d) { return (d.target.y * scaleAdjust); });
//node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
var linkedByIndex = {};
for (i = 0; i < json.nodes.length; i++)
{
linkedByIndex[i + "," + i] = 1;
};
json.links.forEach(
function (d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
//This function looks up whether a pair are neighbours
function neighboring(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
function connectedNodes() {
//Reduce the opacity of all but the neighbouring nodes
d = d3.select(this).node().__data__;
//console.log(d.name);
node.style("opacity", function (o) {
return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
});
link.style("opacity", function (o) {
return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1;
});
}
function allNodes()
{ node.style("opacity", 1);
link.style("opacity", 1);}
function reDirect()
{
d = d3.select(this).node().__data__;
//console.log(d.name.replace("#", "%23"));
window.location.assign("http://graphofknowledge.appspot.com/tagid/"+d.name.replace("#", "+++")); //c# --> c%23
//document.getElementById('tag').value= d.name;
}
} // function knowledgeGraph
//------------------------------------
// All css files should appear in the end of the document, so as to load the document content faster
// add bootstrap css
$("head").append (
'<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">'
);
// css for trend graph
var cssTrendGraph = '<style type="text/css">';
cssTrendGraph += '.axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; }';
cssTrendGraph += '.line { fill: none; stroke: steelblue; stroke-width: 2px; }';
cssTrendGraph += '</style>';
$("head").append(cssTrendGraph);