-
Notifications
You must be signed in to change notification settings - Fork 0
/
force.html
181 lines (146 loc) · 7.29 KB
/
force.html
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>D3 Test</title>
<script type="text/javascript" src="d3.v2.js"></script>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript" src="./facebox/src/facebox.js"></script>
<link href="./facebox/src/facebox.css" rel="stylesheet" type="text/css">
<!-- for js widget of sina login -->
<script src=" http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=3422703718" type="text/javascript" charset="utf-8"></script>
<!--tutorial followed: http://bl.ocks.org/1021841 -->
<!--tutorial followed: https://github.com/mbostock/d3/wiki/Force-Layout -->
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<body>
<h4> Weibo login placeholder </h4>
<div id="wb_connect_btn"></div>
<script>
var accessToken = "";
WB2.anyWhere(function(W){
W.widget.connectButton({
id: "wb_connect_btn",
type:'4,2',
callback : {
login:function(o){ //登录后的回调函数
//alert("Hello, welcome back: "+o.screen_name)
//accessToken = document.location.hash.substring(1); //解析hash得到access_token值。
console.log("[INFO] Logged in!");
},
logout:function(){ //退出后的回调函数
alert('logout');
}
}
});
});
</script>
<h4> Data </h4>
<button type="button" onclick="retrieve_data()">search</button>
<script type="text/javascript">
/*****************************************************************
* d3 process
*/
// TODO: resize is needed for desktop users!
var w = $(window).width(),
h = $(window).height(),
fill = d3.scale.category10();
/* Q: how to use this first-thing in jquery? A:
$(document).ready( function() {
// put all your jQuery goodness in here.
});
*/
var vis = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var force;
var nodes = [];
var node;
// TODO: find a better to redraw after data retrieval is done!
function simple_draw() {
// force layout
force = d3.layout.force()
.nodes(nodes)
.links([])
.size([w, h])
.start();
//
node = vis.selectAll("circle.node")
.data(nodes)
.enter().append("svg:circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 8)
.style("fill", function(d, i) { return fill(i & 3); })
.style("stroke", function(d, i) { return d3.rgb(fill(i & 3)).darker(2); })
.style("stroke-width", 1.5)
.call(force.drag);
}
vis.style("opacity", 1e-6)
.transition()
.duration(1000)
.style("opacity", 1);
if (force != null){
force.on("tick", function(e) {
// Push different nodes in different directions for clustering.
var k = 6 * e.alpha;
nodes.forEach(function(o, i) {
o.y += i & 1 ? k : -k;
o.x += i & 2 ? k : -k;
});
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
d3.select("body").on("click", function() {
nodes.forEach(function(o, i) {
o.x += (Math.random() - .5) * 40;
o.y += (Math.random() - .5) * 40;
});
force.resume();
});
}
// TODO: it is temporary solution for controlling the process of 'retrieval -> draw'
function retrieve_data() {
search_users_by_keyword("上海交通大学"); // 交通大学
}
// EXP: entry point once the window resized. Get ready for graph resizing....
// NOTE: it's simplified method, for more info, see: resize.casestudy.html
// NOTE: probably it will call multiple times during one resizing session. May cause problem.
$(window).resize(function() {
console.log("[INFO] window resized. w:" + w + " h:"+h);
});
/*****************************************************************
* Weibo data retrieval
*/
// 用关键字搜索用户
function search_users_by_keyword(kw) {
var temp = encodeURIComponent(kw);
console.log(temp);
WB2.anyWhere(function(W){
// 搜索用户时的联想搜索建议
W.parseCMD("/search/suggestions/users.json",// "/friends_timeline.json",
function(sResult, bStatus){
if(bStatus == true) {
alert(sResult);
console.log("Going to user data");
//d3test(sResult);
console.log(sResult);
nodes = sResult; //["users"]; // IDEA: shall we create a data pool for data retrieved async.
console.log(nodes);
nodes.forEach( function(o,i) {console.log( o.screen_name);});
simple_draw(); // TODO: code smell, need to decouple.
}
},
{
q: temp, // "%E4%B8%8A%E6%B5%B7" stands for "上海"
count : 100
// access_token: // Q: is it necessary? Looks like not necessary. A:
},
{
method: 'get'
});
});
}
</script>
</body>
</html>