forked from joliv/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spark.go
68 lines (62 loc) · 1.11 KB
/
spark.go
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
package spark
import (
"bytes"
"math"
)
var steps = []rune("▁▂▃▄▅▆▇█")
// Line generates a sparkline string from a slice of
// float64s.
func Line(nums []float64) string {
if len(nums) == 0 {
return ""
}
indices := normalize(nums)
var sparkline bytes.Buffer
for _, index := range indices {
sparkline.WriteRune(steps[index])
}
return sparkline.String()
}
func normalize(nums []float64) []int {
var indices []int
min := minimum(nums)
for i, _ := range nums {
nums[i] -= min
}
max := maximum(nums)
if max == 0 {
// Protect against division by zero
// This can happen if all values are the same
max = 1
}
for i, _ := range nums {
x := nums[i]
x /= max
x *= 8
if x == 8 {
x = 7
} else {
x = math.Floor(x)
}
indices = append(indices, int(x))
}
return indices
}
func minimum(nums []float64) float64 {
var min float64 = nums[0]
for _, x := range nums {
if math.Min(x, min) == x {
min = x
}
}
return min
}
func maximum(nums []float64) float64 {
var max float64 = nums[0]
for _, x := range nums {
if math.Max(x, max) == x {
max = x
}
}
return max
}