-
Notifications
You must be signed in to change notification settings - Fork 0
/
barCharts.html
210 lines (168 loc) · 7.5 KB
/
barCharts.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
<html>
<head>
<style>
text{
cursor:pointer;
tool-tip: "Click on the label for drillDown";
}
text:hover{
tool-tip: "Click on the label for drillDown";
}
</style>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var data;
var options;
var chart;
var length;
var old_view;
var globaluser;
var userList= new Array();
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawVisualization);
google.charts.setOnLoadCallback(drawChart2);
var handler = function(e) {
var parts = e.targetID.split('#');
//alert(e);
if (parts.indexOf('label') >= 0) {
var idx = parts[parts.indexOf('label') + 1];
var olduser=globaluser;
globaluser=data.getValue(parseInt(idx), 0);
if(idx<userList.length){
// $("#piechart").css("display","block");
if(olduser===globaluser || olduser===undefined)
$("#piechart").toggle();
drawChart2();
}
}
};
//try writing handler on text
function getData(fileName){
var dataResult;
$.ajax({
url: fileName,
async: false,
type: 'GET',
dataType: 'json',
// added data type
success: function(res) {
dataResult = res;
}
});
return dataResult
}
function drawVisualization() {
data=getData("http://127.0.0.1:8000/AllActions.txt");
var all_items = ['Search', 'Favorite','Upvote','Downvote','Comment','Post'];
//For filter use delete
var all_itemsSize=0;
for(var item in all_items)
all_itemsSize++;
result=[];
result.push(["username"].concat(all_items));
for(userid in data) {
let user = data[userid];
let new_user_data = [user["userName"]]
userList.push(user["userName"]);
for (index in all_items) {
let item = all_items[index];
if (item in user.actions) {
new_user_data.push(+user.actions[item])
}else{
new_user_data.push(0)
}
}
length = result.length - 1;
result.push(new_user_data);
}
data = google.visualization.arrayToDataTable(result);
options = {
title : 'Actions of different Users',
vAxis: {title: 'Units'},
hAxis: {title: 'Users'},
seriesType: 'bars',
};
var view = new google.visualization.DataView(data);
view.setColumns([0,1,2,3,4,5,6]);
chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'select', function () {
highlightBar(chart, options, view);
});
google.visualization.events.addListener(chart, 'click', handler);
chart.draw(view, options);
}
function highlightBar(chart, options, view) {
//view = new google.visualization.DataView(data);
old_view = view;
var selection = chart.getSelection();
console.log(selection);
var column = selection[0].column;
var row = selection[0].row;
if (selection.length) {
if(row==null){
console.log(row);
console.log(column);
view.hideColumns([view.getTableColumnIndex(column)]);
console.log("Removed column "+ column);
chart.draw(view, options);
} else {
view.hideRows([view.getTableRowIndex(row)]);
chart.draw(view, options);
}
}
}
// this one is for drawing the chart only if user clicks on the label
function drawChart2() {
var dataResult=getData("http://127.0.0.1:8000/AllActions.txt");
let all_items = {'Search':0, 'Favorite':0,'Upvote':0,'Downvote':0,'Comment':0,'Post':0 }
for(key in dataResult) {
let value = dataResult[key];
if(value['userName']===globaluser){
value=value.actions;
for(ele in value) {
all_items[ele]=all_items[ele]+value[ele];
}
break;
}
}
for(ele in all_items)
console.log(ele+all_items[ele]);
var data = google.visualization.arrayToDataTable([
['Actions', 'Percent'],
['Post', all_items['Post']],
['Favorite', all_items['Favorite']],
['Comment', all_items['Comment']],
['Upvote', all_items['Upvote']],
['Downvote', all_items['Downvote']],
['Search', all_items['Search']]
]);
var options = {
title: 'Individual Activities Chart for '+globaluser
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
// Will accure at document load
$("#ResetColumn").on("click",function(){
old_view.setColumns([0,1,2,3,4,5,6]);
chart.draw(old_view, options);
});
$("#ResetRow").on("click",function(){
old_view.setRows(0, length);
chart.draw(old_view, options);
});
$("#disappear").on("click",function(){
$("#piechart").style.display='none';
});
});
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
<button id="ResetColumn"> Reset Filters on Columns</button>
<button id="ResetRow"> Reset Filters on Rows</button>
<div id="piechart" style="width: 600px; height: 500px;float:left;display:none;"><h4 id="disappear">Click me to hide the chart<h4></h4>
</body>
</html>