-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_bar.html
executable file
·329 lines (256 loc) · 8.32 KB
/
test_bar.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
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
<!DOCTYPE HTML>
<style>
.link {
stroke: #aaa;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
arc text {
font: 10px sans-serif;
text-anchor: middle;
}
.arc path {
stroke: #fff;table{
table-layout:fixed;
}
}
table{
table-layout:fixed;
}
</style>
<body style="margin-top: -10px">
<br/>
<table width="960" height ="660" class="noboredr" style=" border-collapse:collapse;" algin="center">
<tr>
<td width="40%" height ="660" id="main" class="noboredr" style="margin-top: -30px">
</td>
<td width="60%" height="660" id="td2" class="noboredr" align="right">
<div id="info" align="center" width="100%" height="300" align="center" ></div>
<div id="showme1" style="overflow-y: auto; height:300;width:95%;" class="noboredr" align="center"><br></div>
<div id="showme" style="overflow-y: auto; height:300; width:95%;" class="noboredr" align="center"><br></div>
</td>
</tr>
</table>
</body>
</html>
<script>
var loadJS = function(url, implementationCode, location){
//url is URL of external file, implementationCode is the code
//to be called from the file, location is the location to
//insert the <script> element
var scriptTag = document.createElement('script');
scriptTag.src = url;
scriptTag.onload = implementationCode;
scriptTag.onreadystatechange = implementationCode;
location.appendChild(scriptTag);
};
var loadJS2 = function(url, implementationCode, location,id){
//url is URL of external file, implementationCode is the code
//to be called from the file, location is the location to
//insert the <script> element
var scriptTag = document.createElement('script');
scriptTag.src = url;
scriptTag.onload = implementationCode(id);
scriptTag.onreadystatechange = implementationCode;
location.appendChild(scriptTag);
};
function piechart(uid)
{
var width = 520,
height = 270,
radius = Math.min(width, height) / 2;
var color = d3.scaleOrdinal(d3.schemeCategory10);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 80);
var pie = d3.pie()
.sort(null)
.value(function(d) { return d.frequency; });
var svg = d3.select("#showme1").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
d3.csv("tag_frequency_user1.csv", type, function(error, data)
{
if (error) throw error;
data=data.filter(function(d){return d.userid==uid;})
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.tag); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.tag; });
});
function type(d) {
d.frequency = +d.frequency;
d.userid = +d.userid;
return d;
}
};
function bar(uid)
{
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 500 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#showme").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 + ")");
// get the data
d3.csv("activity.csv", function(error, data) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.count = d.count;
});
function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
};
data=data.filter(function (d){return d.uid==uid;})
var p=[];
data.forEach(function(d) {
p.push(parseInt(d.count));
});
// Scale the range of the data in the domains
x.domain(data.map(function(d) { return d.label; }));
y.domain([0, getMaxOfArray(p)]);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.label); })
.attr("width",x.bandwidth()).attr("fill","steelblue")
.attr("y", function(d) { return y(d.count); })
.attr("height", function(d) { return height - y(d.count); });
var xaxis= svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + 10) + ")")
.style("text-anchor", "middle")
.text("Time Period");
// add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Answers posted");
});
};
function mainscript(){
var container = d3.select('#main');
var width = 400,
height = 600,
radius = 10;
var svg = container.append("svg")
.attr("width", "450")
.attr("height", "660");
var color = d3.scaleOrdinal(d3.schemeCategory10);
var width=450,
height=660;
var simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("user_profile.json", function(error,graph) {
if (error) throw error;
function onclicks()
{
d3.selectAll('circle').style("fill-opacity",0.3);
t = d3.select(this).node().__data__;
d3.select(this).style("fill-opacity",1);
d3.select('#showme').selectAll('svg').remove();
d3.select('#showme1').selectAll('svg').remove();
d3.select('#info').selectAll('text').remove();
d3.csv("u.csv", function(error, data)
{
if (error) throw error;
data=data.filter(function(d){return d.uid==t.uid});
data.forEach(
function(d)
{
d3.select('#info').append('text').html("<p >Name: "+d.name+"<br> Age: "+d.age+"<br> Reputation: "+d.rep+"<br> Personal Summary: "+d.aa+"<br> Website: "+d.web+"</p>").style("font-size","16px");
}
);
}
);
loadJS2('http://d3js.org/d3.v4.js', bar, document.getElementById('showme'),t.uid);
loadJS2('//d3js.org/d3.v4.0.0-alpha.4.min.js', piechart, document.getElementById('showme1'),t.uid);
}
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle").style("stroke",function(d){return color(d.group);}).style("stroke-width","3.5px")
.attr("r", function(d){return d.reputation/1000+40;})
.attr("fill",function(d){return color(d.group);}).style("fill-opacity","0.5");
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.on("click",onclicks);
node.append("title").text(function(d) { return 'Reputation: '+d.reputation; }).attr("dx", 10)
.attr("dy", ".25em").style("font-size", "18px");
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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)); });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
};
loadJS("https://d3js.org/d3.v4.min.js", mainscript, document.getElementById('main'));
</script>