-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.icn
124 lines (82 loc) · 2.53 KB
/
box.icn
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
link numbers
# Box Plot
# ----------------------------------------------------------------
record boxData(min, max, mean, q1, median, q3, range)
# ----------------------------------------------------------------
procedure drawBoxPlot(w, bData)
# Position & dimensions
width := 700 ; height := 100
leftX := 50 ; rightX := leftX + width
topY := 50 ; botY := topY + height
midY := topY + height / 2
leftBox := (xs := calXs(leftX, width, bData))[1]
rightBox := xs[2] ; medianX := xs[3]
# Left
DrawLine(w, leftX, topY, leftX, botY)
DrawLine(w, leftX, midY, leftBox, midY)
# Right
DrawLine(w, rightBox, midY, rightX, midY)
DrawLine(w, rightX, topY, rightX, botY)
# Median & Box
DrawLine(w, medianX, topY, medianX, botY)
DrawRectangle(w, leftBox, topY, rightBox - leftBox, height)
end
procedure calPosX(left, width, bData, s)
return left + (bData[s] - bData.min) * width / bData.range
end
procedure calXs(leftX, width, bData)
return {
xs := list(3); ks := ["q1", "q3", "median"]
every i := 1 to 3 do
xs[i] := calPosX(leftX, width, bData, ks[i]); xs
}
end
procedure showData(bData)
write("Min: ", bData.min, n := "\n",
"Max: ", bData.max, n,
"Mean: ", bData.mean, n,
"Q1: ", bData.q1, n,
"Median: ", bData.median, n,
"Q3: ", bData.q3, n,
"Range: ", bData.range)
end
# ----------------------------------------------------------------
procedure median(sortedData)
return(percentile(sortedData, 50))
end
procedure percentile(sortedData, n)
return if 0 = (m := n * (*sortedData + 1)) % (h := 100) then
sortedData[m / h] else
amean(sortedData[floor(d := div(m, h))],
sortedData[ceil(d)])
end
# ----------------------------------------------------------------
procedure calculate(d)
return {
(bData := boxData()).range :=
(bData.max := (d := sort(d))[*d]) -
(bData.min := d[1])
bData.q1 := percentile(d, 25)
bData.median := median(d)
bData.q3 := percentile(d, 75)
bData.mean := mean(d); bData
}
end
procedure mean(L)
return div({ s := 0; every s +:= !L; s }, *L)
end
# ----------------------------------------------------------------
procedure main()
w := open("Box Plot", "g")
# Create data
data := list(numData := 100)
every i := 1 to numData do data[i] := ?1e3
# Perform calculations and display box data
showData(bData := calculate(data))
# Draw box plot
drawBoxPlot(w, bData)
WSync()
delay(5e3)
close(w)
end
# ----------------------------------------------------------------