-
Notifications
You must be signed in to change notification settings - Fork 52
/
spark.go
67 lines (61 loc) · 1.12 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
package main
import (
"bytes"
"math"
)
var steps = []rune("▁▂▃▄▅▆▇") // 8th rune "█" omitted to prevent gluing of rows.
func Spark(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
total := float64(len(steps))
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 *= total
if x == total {
x = total - 1
} else {
x = math.Floor(x)
}
indices = append(indices, int(x))
}
return indices
}
func minimum(nums []float64) float64 {
var min = nums[0]
for _, x := range nums {
if math.Min(x, min) == x {
min = x
}
}
return min
}
func maximum(nums []float64) float64 {
var max = nums[0]
for _, x := range nums {
if math.Max(x, max) == x {
max = x
}
}
return max
}