-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweel.js
168 lines (150 loc) · 5.25 KB
/
tweel.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
/**
* Tweel (The Twitter Friend Wheel)
* Copyright 2010 Rich Hong
* http://richhong.com/tweel/tweel.html
*
* MIT License: http://www.opensource.org/licenses/mit-license.php
**/
var Log = {
elem: false,
write: function(text){
if (!this.elem)
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
this.elem.style.left = (350 - this.elem.offsetWidth / 2) + 'px';
}
};
function addEvent(obj, type, fn) {
if (obj.addEventListener) obj.addEventListener(type, fn, false);
else obj.attachEvent('on' + type, fn);
};
var rgraph;
var json = [];
// Load graph with screen_name as the root node
function load(screen_name) {
$.ajaxSetup({cache: true});
$.getJSON("http://twitter.com/users/show.json?screen_name="+screen_name+"&callback=?", function(data) {
json[0] = {};
json[0]["id"] = data["id"];
json[0]["name"] = data["screen_name"];
json[0]["data"] = {"name": data["name"]};
json[0]["adjacencies"] = [];
init(screen_name);
});
}
// TODO: only the <100 friends are loaded for each user, use cursor to fix this
function init(screen_name){
$.getJSON("http://twitter.com/statuses/friends/"+screen_name+".json?callback=?", function(data) {
for (var i in data) {
json.push({"id": data[i].id, "name": data[i].screen_name, "data": {"name": data[i].name}, "adjacencies": []});
json[0]["adjacencies"].push(""+data[i].id);
}
//console.log(json);
//load JSON data
rgraph.loadJSON(json, 0);
//compute positions and make the first plot
rgraph.refresh();
});
var infovis = document.getElementById('infovis');
var w = infovis.offsetWidth, h = infovis.offsetHeight;
//init canvas
//Create a new canvas instance.
var canvas = new Canvas('mycanvas', {
//Where to append the canvas widget
'injectInto': 'infovis',
'width': w,
'height': h,
//Optional: create a background canvas and plot
//concentric circles in it.
'backgroundCanvas': {
'styles': {
'strokeStyle': '#555'
},
'impl': {
'init': function(){},
'plot': function(canvas, ctx){
var times = 6, d = 150;
var pi2 = Math.PI * 2;
for (var i = 1; i <= times; i++) {
ctx.beginPath();
ctx.arc(0, 0, i * d, 0, pi2, true);
ctx.stroke();
ctx.closePath();
}
}
}
}
});
//end
//init RGraph
rgraph = new RGraph(canvas, {
//Set Node and Edge colors.
Node: {
color: '#ccddee'
},
Edge: {
color: '#772277'
},
levelDistance: 150,
onBeforeCompute: function(node){
Log.write("centering @" + node.name + "...");
},
onAfterCompute: function(){
Log.write("done");
},
//Add the name of the node in the correponding label
//and a click handler to move the graph.
//This method is called once, on label creation.
onCreateLabel: function(domElement, node){
domElement.innerHTML = node.data.name;
$(domElement).attr("title", node.data.name + " (@" + node.name + ")");
$(domElement).tipTip();
domElement.onclick = function(){
$.getJSON("http://twitter.com/statuses/friends/"+node.name+".json?callback=?", function(data) {
for (var i in data) {
rgraph.graph.addAdjacence(rgraph.graph.getNode(node.id), {"id": data[i].id, "name": data[i].screen_name, "data": {"name": data[i].name}, "adjacencies": []});
}
rgraph.onClick(node.id);
});
};
},
//Change some label dom properties.
//This method is called each time a label is plotted.
onPlaceLabel: function(domElement, node){
var style = domElement.style;
style.display = '';
style.cursor = 'pointer';
if (node._depth == 0) {
style.fontSize = "1.0em";
style.color = "#ccc";
}else if (node._depth == 1) {
style.fontSize = "0.7em";
style.color = "#ccc";
} else if(node._depth == 2){
style.fontSize = "0.6em";
style.color = "#494949";
} else {
style.display = 'none';
}
var left = parseInt(style.left, 10);
var w = domElement.offsetWidth;
style.left = (left - w / 2) + 'px';
}
});
//end
}
$(document).ready(function() {
// Submit event listener
$("#screen_name_form").submit(function() {
$("#screen_name_form").css("display", "none");
$("#infovis").css("display", "block");
load($("#screen_name").val());
});
// Parse url to get parameters
var n = $(document).getUrlParam("screen_name");
if (n) {
$("#screen_name_form").css("display", "none");
$("#infovis").css("display", "block");
load(n);
}
});