blob: 5d75861a7ab3351515d521ab5530aab458bcb0a6 (
plain)
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
|
#!/bin/sh
# (C) 2018 by Eugeniy Mikhailov, <evgmik@gmail.com>
# vim:set ft=tcl: \
exec tclsh "$0" "$@"
package require math::statistics
proc data2txtPlot { data } {
# fixme: use buil-in min and max to get rid math::statistics dependence
set maxVal [::math::statistics::max $data ]
set minVal [::math::statistics::min $data ]
set diffVal [expr {$maxVal-$minVal}]
set chars {▁ ▂ ▃ ▄ ▅ ▆ ▇ █}; # 8 bars
if { $diffVal == 0 } {
set diffVal 1
set chars {▄}; # if all values are the same use mid bar
}
set barsNum [llength $chars];
foreach x $data {
# funky 1.1 are needed to convert it to floats
set cnt [expr { round((1.0*$x-$minVal)/(1.1*$diffVal)*(1.1*($barsNum-1.0))) }]
# safety check on counter value
set cnt [expr {min($cnt, ($barsNum-1))}]
set cnt [expr {max($cnt, 0)}]
set bar [lrange $chars $cnt $cnt]
lappend strPlot $bar
}
return [join $strPlot ""]
}
proc binInHistogram { x limits } {
# calculates which bin in histogram is occupied by data with value x
# limits of the histogram they are used the same way as in ::math::statistics::histogram
set cnt 0
foreach lim $limits {
if { $x < $lim } {
break;
}
incr cnt
}
return $cnt;
}
proc markNthBin { i } {
#set mark {█}
#set mark {╽}
set mark {╿}
set mark {↑}
set space {▁}
#set space {─}
set out {}
set cnt 0
set i [expr {max(0,$i)}]
while { $cnt < $i } {
lappend out $space
incr cnt
}
lappend out $mark
return [join $out ""]
}
# usage examples
#
#puts [ data2txtPlot {1 2 3 4 5 6 7 8} ]
#puts [ data2txtPlot {1 2 3 4 5 6 7 8 7 6 5 4 3 2 1} ]
#puts [ binInHistogram -10 {10 20 30}]
#puts [ binInHistogram 15 {10 20 30}]
#puts [ binInHistogram 25 {10 20 30}]
#puts [ binInHistogram 35 {10 20 30}]
#puts [ binInHistogram 30 {10 20 30}]
#puts [ binInHistogram 10 {10 20 30}]
#puts [ markNthBin 0 ]
#puts [ markNthBin 1 ]
#puts [ markNthBin 2 ]
#puts [ markNthBin 10 ]
# vim: ts=2 sw=2 foldmethod=indent:
|