home.js
7.72 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
/*
HOME CHARTS
*/
//Adding this code by @jshanley to create a filter to look for the last child
d3.selection.prototype.first = function() {
return d3.select(this[0]);
};
d3.selection.prototype.last = function() {
var last = this.size() - 1;
return d3.select(this[last]);
};
var charts = {
build_resource: function(url){
$.get(url, function(dataset){
var width = 600;
var height = 300;
var padding = 30;
var radius = Math.min(width, height) / 2 - padding;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var new_div = d3.select(".carousel-inner").append("div").attr("class","item");
var svg = new_div.append("svg").attr("width", width).attr("height", height)
.style("margin","auto")
.style("display","block")
.append('g')
.attr('transform', 'translate(' + (width / 2) +
',' + (height / 2 + padding ) + ')');
var donutInner = 20;
var arc = d3.arc()
.innerRadius(radius - donutInner)
.outerRadius(radius);
svg.append("text")
.attr("text-anchor", "middle")
.attr("x",0 )
.attr("y", -height/2 )
.style("font-size", "30px")
.text("Recursos mais utilizados")
.attr("fill", "#003333")
.style("font-weight", "bold")
.style("font-style", "italic");
var pie = d3.pie()
.value(function(d) { return d[1]; })
.sort(null);
var path = svg.selectAll('path')
.data(pie(dataset))
.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d, i) {
return color(i);
});
var labelArc = d3.arc()
.outerRadius(radius - donutInner + 30)
.innerRadius(radius + 20 );
svg.selectAll("text.pie-tooltip")
.data(pie(dataset))
.enter()
.append("text")
.attr("id", function(d){
return d.data[0];
})
.attr("class","pie-tooltip")
.attr('fill',"#172121")
.attr("transform", function(d) {
c = labelArc.centroid(d);
return "translate(" + (c[0]*1.0 - 20) +"," + c[1]*0.8 + ")";
})
.attr("dy", ".25em")
.text(function(d) { return d.data[0]; });
}) // end of the get method
},
build_bubble_user: function(url){
$.get(url, function(dataset){
var width = 600;
var height = 300;
function min(){
min = 100000000000;
for(var i = 0; i < dataset.length; i++){
if (dataset[i]['count'] < min){
min = dataset[i]['count'];
}
}
return min
}
function max(){
max = 0;
for(var i = 0; i < dataset.length; i++){
if (dataset[i]['count'] > max){
max = dataset[i]['count'];
}
}
return max
}
var color = d3.scaleOrdinal(d3.schemeCategory20);
//adding new div to carousel-inner div
var new_div = d3.select(".carousel-inner").append("div").attr("class","item");
var radiusScale = d3.scaleSqrt().domain([min(), max()]).range([10,50]);
var svg = new_div.append("svg").attr("width", width).attr("height", height)
.style("margin","auto")
.style("display","block")
.append('g')
.attr("transform", "translate(0,0)")
.attr("width", width)
.attr("height", height);
//adding svg title
svg.append("text")
.attr("text-anchor", "middle")
.attr("x", width/2 )
.attr("y", 30)
.style("font-size", "30px")
.text("Usuários mais ativos no Amadeus")
.attr("fill", "#003333")
.style("font-weight", "bold")
.style("font-style", "italic");
var simulation = d3.forceSimulation()
.force("x", d3.forceX(width/2).strength(0.05))
.force("y", d3.forceY(height/2).strength(0.05))
.force("collide", d3.forceCollide(function(d){
return radiusScale(d['count']);
}));
//First I create as many groups as datapoints so
//it can hold all other objects (circle, texts, images)
var groups = svg.selectAll('.users-item')
.data(dataset)
.enter()
.append("g")
.attr("class",".user-dot");
//Create circles to be drawn
var circles = groups
.append('circle')
.attr("r", function(d){
return radiusScale(d['count']);
})
.attr("fill", function(d){
return 'url('+'#'+'user_'+d['user_id']+')';
});
//Add texts to show user names
groups.append("text")
.text(function(d){
return d['user'];
}).attr("fill", "#FFFFFF")
.attr("id", function(d){
return "user_tooltip_"+d['user_id'];
}).style("display", "none");
groups.on("mouseover", function(d){
$("#"+"user_tooltip_"+d['user_id']).show();
});
groups.on("mouseout", function(d){
$("#"+"user_tooltip_"+d['user_id']).hide();
});
var defs = groups.append('svg:defs');
defs.append("svg:pattern")
.attr("id", function(d){
return "user_"+d['user_id'];
})
.attr("width", function(d){
return radiusScale(d['count']);
})
.attr("height", function(d){
return radiusScale(d['count']);
})
.append("svg:image")
.attr("xlink:href", function(d){
return d['image'];
})
.attr("width",function(d){
return radiusScale(d['count'])*2;
})
.attr("height", function(d){
return radiusScale(d['count'])*2;
})
.attr("x", 0)
.attr("y", 0);
//simulation
simulation.nodes(dataset)
.on('tick', ticked); //so all data points are attached to it
function ticked(){
groups.attr("transform", function(d){
return "translate(" + d.x + "," + d.y + ")";
})
}
});
},
most_accessed_subjects: function(url){
$.get(url, function(dataset){
var w = 400;
var h = 300;
var new_div = d3.select(".carousel-inner").append("div").attr("class","item");
var padding = 20;
var xScale = d3.scaleLinear().
domain([0 , d3.max(dataset, function(d){ return d[0]; })])
.range([padding, w - padding]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([h - padding, padding]);
svg = d3.select("body").append("svg").attr("height", h).attr("width", w);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i){
return i * (w / dataset.length - barPadding );
})
.attr("y", function(d){
return h - d*2;
})
.attr("width", 20)
.attr("height", function(d){
return d*2;
})
.attr("fill", function(d){
if (d <= average(dataset)){
return "#BDBDBD";
}else{
return "red";
}
});
});
}
}
charts.build_resource('/topics/count_resources/');
charts.build_bubble_user('/users/get_users_log/');