-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxAndWhiskers.html
194 lines (178 loc) · 5.82 KB
/
boxAndWhiskers.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
<!DOCTYPE html>
<html>
<head>
<!-- Code generated by Grok3 -->
<title>Box and Whisker Chart with HTML</title>
<style>
.container {
display: flex;
gap: 20px;
padding: 20px;
}
.chart-container {
position: relative;
width: 200px;
height: 300px;
border: 1px solid #ccc;
}
.chart {
position: relative;
width: 100px;
margin: 40px auto;
height: calc(100% - 80px);
}
.whisker {
position: absolute;
width: 2px;
background: #1E90FF;
left: 50%;
transform: translateX(-50%);
}
.box {
position: absolute;
width: 100%;
background: rgba(50, 205, 50, 0.3);
border: 2px solid #000;
box-sizing: border-box;
}
.median {
position: absolute;
width: 100%;
height: 2px;
background: #FF4500;
}
.outlier {
position: absolute;
width: 8px;
height: 8px;
background: #800080;
border-radius: 50%;
left: 50%;
transform: translateX(-50%);
}
.label {
position: absolute;
font: 12px Arial;
left: 110px;
}
</style>
</head>
<body>
<div class="container">
<div>
<textarea id="dataInput" rows="10" cols="30" placeholder="Enter numbers (one per line)">
25
30
35
40
45
50
55
60
65
70
15
80
</textarea>
<br>
<button onclick="drawChart()">Draw Chart</button>
</div>
<div class="chart-container">
<div id="chart" class="chart"></div>
</div>
</div>
<script>
function drawChart() {
// Get and process input data
const textarea = document.getElementById('dataInput');
const values = textarea.value.trim().split('\n')
.map(num => parseFloat(num))
.filter(num => !isNaN(num))
.sort((a, b) => a - b);
if (values.length === 0) {
alert('Please enter some valid numbers');
return;
}
// Calculate quartiles
const q1 = quartile(values, 0.25);
const q2 = quartile(values, 0.5); // median
const q3 = quartile(values, 0.75);
const iqr = q3 - q1;
const lowerBound = q1 - 1.5 * iqr;
const upperBound = q3 + 1.5 * iqr;
const min = Math.max(values[0], lowerBound);
const max = Math.min(values[values.length - 1], upperBound);
// Identify outliers
const outliers = values.filter(v => v < lowerBound || v > upperBound);
// Get chart container
const chart = document.getElementById('chart');
chart.innerHTML = '';
// Scale data
const dataMin = Math.min(lowerBound, ...values);
const dataMax = Math.max(upperBound, ...values);
const chartHeight = chart.offsetHeight;
const scaleY = chartHeight / (dataMax - dataMin);
function toY(value) {
return chartHeight - (value - dataMin) * scaleY;
}
// Upper whisker
const upperWhisker = document.createElement('div');
upperWhisker.className = 'whisker';
upperWhisker.style.top = `${toY(max)}px`;
upperWhisker.style.height = `${toY(q3) - toY(max)}px`;
chart.appendChild(upperWhisker);
// Lower whisker
const lowerWhisker = document.createElement('div');
lowerWhisker.className = 'whisker';
lowerWhisker.style.top = `${toY(q1)}px`;
lowerWhisker.style.height = `${toY(min) - toY(q1)}px`;
chart.appendChild(lowerWhisker);
// Box
const box = document.createElement('div');
box.className = 'box';
box.style.top = `${toY(q3)}px`;
box.style.height = `${toY(q1) - toY(q3)}px`;
chart.appendChild(box);
// Median
const median = document.createElement('div');
median.className = 'median';
median.style.top = `${toY(q2)}px`;
chart.appendChild(median);
// Outliers
outliers.forEach(value => {
const outlier = document.createElement('div');
outlier.className = 'outlier';
outlier.style.top = `${toY(value) - 4}px`; // Offset by half height
chart.appendChild(outlier);
});
// Labels
const labels = [
{value: max, y: toY(max)},
{value: q3, y: toY(q3)},
{value: q2, y: toY(q2)},
{value: q1, y: toY(q1)},
{value: min, y: toY(min)},
...outliers.map(value => ({value, y: toY(value)}))
];
labels.forEach(({value, y}) => {
const label = document.createElement('span');
label.className = 'label';
label.textContent = Math.round(value);
label.style.top = `${y - 6}px`; // Center vertically with text
chart.appendChild(label);
});
}
function quartile(sortedArray, q) {
const pos = (sortedArray.length - 1) * q;
const base = Math.floor(pos);
const rest = pos - base;
if (sortedArray[base + 1] !== undefined) {
return sortedArray[base] + rest * (sortedArray[base + 1] - sortedArray[base]);
}
return sortedArray[base];
}
// Draw initial chart
drawChart();
</script>
</body>
</html>