-
Notifications
You must be signed in to change notification settings - Fork 0
/
quartile_percentile75.html
171 lines (132 loc) · 5.62 KB
/
quartile_percentile75.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
<script type="text/javascript" src="https://code.jquery.com/jquery.min.js"></script>
<style>
#quartiles{border:1px solid black;}
</style>
<script>
var coordXOnScale = null;
// Other variables
if(lowerBound == null){
var lowerBound = 2;
}
if(upperBound == null){
var upperBound = 3;
}
if(Median == null){
var Median = parseFloat(lowerBound) + (parseFloat(upperBound)-parseFloat(lowerBound))/2
}
if(Quart1 == null){
var Quart1 = parseFloat(lowerBound) + (parseFloat(Median)-parseFloat(lowerBound))/2
}
$(function(){
// Canvas variables
var canvas=document.getElementById("quartiles");
var ctx=canvas.getContext("2d");
var $canvas=$("#quartiles"); //dollar sign = document.getElementByID
// Helper functions
// convert scale values to canvas coordinates
function convertToPts(VAL){
var unitX = 0.8*window.innerWidth/100;
var res = (VAL-parseFloat(lowerBound))*(80*unitX)/(parseFloat(upperBound)-parseFloat(lowerBound))+10*unitX;
return(res);
}
// draw background to which the graphic reverts after something new is clicked
function drawBG(){
// canvas height and width
ctx.canvas.width = 0.8*window.innerWidth;
ctx.canvas.height = 0.3*window.innerHeight;
// canvas size in screen-dependent units (change to actual rescale at some point?)
var unitX = 0.8*window.innerWidth/100;
var unitY = 0.3*window.innerHeight/100;
ctx.clearRect(0,0, 0.8*window.innerWidth, 0.3*window.innerHeight);
// horizontal axis
ctx.beginPath();
ctx.moveTo(10*unitX, 50*unitY);
ctx.lineTo(90*unitX, 50*unitY);
ctx.stroke();
// end points
ctx.beginPath();
ctx.moveTo(10*unitX, 54*unitY);
ctx.lineTo(10*unitX, 46*unitY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(90*unitX, 54*unitY);
ctx.lineTo(90*unitX, 46*unitY);
ctx.stroke();
// lower and upper bound
ctx.font = '20px sans-serif';
ctx.fillText(lowerBound, 10*unitX, 65*unitY);
ctx.fillText(upperBound, 90*unitX, 65*unitY);
// median
ctx.beginPath();
ctx.strokeStyle = '#039dfc';
ctx.lineWidth = '8';
ctx.moveTo(convertToPts(Median), 58*unitY);
ctx.lineTo(convertToPts(Median), 42*unitY);
ctx.stroke();
// add tick mark label median
ctx.textAlign = 'center';
ctx.fillStyle = '#039dfc';
ctx.fillText(Median, convertToPts(Median), 65*unitY);
// first quartile
ctx.beginPath();
ctx.strokeStyle = '#936862';
ctx.lineWidth = '8';
ctx.moveTo(convertToPts(Quart1), 58*unitY);
ctx.lineTo(convertToPts(Quart1), 42*unitY);
ctx.stroke();
// add tick mark label quartile 1
ctx.fillStyle = '#936862';
ctx.fillText(Quart1, convertToPts(Quart1), 65*unitY);
// add grey background
ctx.fillStyle = 'rgba(220,220,220,0.4)';
ctx.fillRect(convertToPts(Median), 0*unitY, 100*unitX, 100*unitY);
}
// draw line with value on scale (this happens on click)
function drawVline(coordX){
var unitY = 0.3*window.innerHeight/100;
var unitX = 0.8*window.innerWidth/100;
coordXOnScale = (coordX-10*unitX)*((parseFloat(upperBound)-parseFloat(lowerBound))/(80*unitX))+parseFloat(lowerBound);
if(coordXOnScale < parseFloat(upperBound) && coordXOnScale > Median){
// add line (tick mark) at selected location
ctx.beginPath();
ctx.strokeStyle = '#936862';
ctx.lineWidth = '8';
ctx.moveTo(coordX, 58*unitY);
ctx.lineTo(coordX, 42*unitY);
ctx.stroke();
// add tick mark label
ctx.textAlign = 'center';
ctx.fillStyle = '#936862';
ctx.fillText(Math.round(coordXOnScale*100)/100, coordX, 65*unitY);
// store chosen value in session so that it can be called on next page
sessionStorage.setItem("Quart1", Math.round(coordXOnScale*100)/100);
}
}
// called when user clicks the mouse
function handleMouseDown(e){
e.preventDefault();
// refresh background
drawBG();
// get the mouse position
var rect = canvas.getBoundingClientRect();
var mouseX = parseInt(e.clientX-rect.left);
var mouseY=parseInt(e.clientY-rect.top);
// paint new anchor on line
drawVline(mouseX);
// send information to form input field
document.getElementById('PE09_01').value = coordXOnScale;
}
// Initialize
drawBG();
// listen for mousedown events
$("#quartiles").mousedown(function(e){handleMouseDown(e);});
// Store lower and upper bound in session
sessionStorage.setItem("lowerBound", lowerBound);
sessionStorage.setItem("upperBound", upperBound);
}); // end $(function(){});
</script>
<p style="text-align: center;">
<canvas id="quartiles"></canvas>
</p>
<!-- Hidden input field to save information and send it to questionnaire software-->
<input type="hidden" id="PE09_01" name="PE09_01" value="">