This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheelVis.html
192 lines (164 loc) · 6.08 KB
/
wheelVis.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
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div width='100%'>
<object id='wheel-svg' width='100%' type='image/svg+xml' data='./customGEW.svg'></object>
</div>
<div width='100%'>
<svg id='summary' width='100%' viewBox="-200 -200 400 400"></svg>
</div>
<script>
// the letters
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'];
// clockwise round wheel
var orderedLetters = ['a', 'b', 'e', 'f', 'g', 'h', 'p', 'o', 'n', 's',
't', 'q', 'r', 'm', 'l', 'k', 'j', 'i', 'd', 'c'];
// the labels, as map to letters above
var labels = ['shock', 'interest', 'anger', 'hate', 'amusement',
'pride', 'joy', 'contentment', 'contempt', 'disgust', 'fear',
'confusion', 'disappointment', 'relief', 'admiration', 'love',
'guilt', 'regret', 'compassion', 'sadness'];
// retrieve a GET parameter
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
// calculate a single score for each emotion
function sumForEmotion(){
var sums = [];
for(var i=0; i< orderedLetters.length; i++){
var emo = labels[letters.indexOf(orderedLetters[i])];
var label = orderedLetters[i];
var sum = 0;
for(var j = 1; j <= 4; j++){
sum += j*results['circle' + j + '-' + label];
}
sums.push(sum);
}
return sums;
}
function buildEmptySummary(){
var svg = document.getElementById('summary');
var wheel = document.getElementById("wheel-svg");
svgDoc = wheel.contentDocument;
var xmlns = "http://www.w3.org/2000/svg";
for(var i=0; i< orderedLetters.length; i++){
var segment = document.createElementNS(xmlns,'path');
segment.setAttribute('d', '');
segment.id = 'summary-' + i;
var cId = 'circle1-' + orderedLetters[i];
var c = svgDoc.getElementById('circle1-' + orderedLetters[i]);
var color = c.getAttribute('style');
segment.setAttribute('style', color);
svg.appendChild(segment);
}
}
// draw a simple diagram showing summary of results in a way that could
// be displayed on a map
function svgSummary(sums){
var cX = 0, cY = 0, theta = (2*Math.PI)/letters.length;
var pathStart = "M " + cX + "," + cY + " L ";
var pathEnd = " L " + cX + "," + cY + " Z";
var max = Math.max.apply(null, sums);
var rScale = 200/max; // Array.max(sums);
for(var i=0; i< orderedLetters.length; i++){
if(sums[i] > 0){
var r = rScale * sums[i];
var a = polarToCartesian(r, i*theta);
var b = polarToCartesian(r, (i+1)*theta)
// create svg segment path
var path = pathStart + (a[0]+cX) + " " + (a[1]+cY);
path += " A " + r + " " + r + " 0 0 1 " + (b[0]+cX) + " " + (b[1]+cY);
path += pathEnd;
// set the right portion to have this segment path
var el = document.getElementById('summary-' + i);
el.setAttribute('d', path);
}
}
}
function polarToCartesian(R, theta){
var theta = theta-(Math.PI/2);
var x = R * Math.cos(theta);
var y= R * Math.sin(theta);
return [x,y];
}
// initiate results
var results = {};
for(var i = 0; i < letters.length; i++){
for(var j = 1; j <= 4; j++){
var circle_id = "circle" + j + "-" + letters[i];
results[circle_id] = 0;
}
}
var max = 0;
// new set of results in
function logResults(data){
var selection = data.value;
console.log('gps: ' + data.location);
for(var i = 0; i < selection.length; i++){
var circle = selection[i];
if(results[circle] == null){
results[circle] = 0;
}
else{
results[circle] += 1;
if(results[circle] > max){ max = results[circle]; }
}
animate(circle);
}
setOpacities();
var sums = sumForEmotion();
svgSummary(sums);
}
// animate a circle to show it has been clicked
function animate(circle){
var svg = document.getElementById("wheel-svg");
svgDoc = svg.contentDocument;
var c = svgDoc.getElementById(circle);
var currentRadius = c.getAttribute('r');
var currentStroke = c.style.strokeWidth;
// var currentStyle = c.getAttribute('style');
c.setAttribute('r', currentRadius * 1.3);
c.style.strokeWidth = '5';
window.setTimeout(function () {
c.setAttribute('r', currentRadius);
c.style.strokeWidth = currentStroke;
// c.setAttribute('style', currentStyle);
}, 1000);
}
function setOpacities(){
var svg = document.getElementById("wheel-svg");
svgDoc = svg.contentDocument;
for(k in results){
var opacity = results[k]/max;
var c = svgDoc.getElementById(k);
c.style.opacity = opacity;
}
}
// nodejs initiation
var urlarr = window.location.href.split("/");
var server = urlarr[0] + "//" + urlarr[2]
var socket = io.connect(server);
console.log('connected');
var nodejs = true;
var session = get('s');
var pid = get('p');
socket.emit('joinSession', {"session": session });
socket.on('wheelUpdate', function(data){
if(data.pid === pid || pid == null){
logResults(data);
}
});
var svgElement = document.getElementById("wheel-svg");
svgElement.addEventListener('load', function()
{
buildEmptySummary();
});
// setTimeout(buildEmptySummary, 3000); // double check it's worked...
</script>
</body>
</html>