-
Notifications
You must be signed in to change notification settings - Fork 0
/
spark.go
51 lines (46 loc) · 978 Bytes
/
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
package main
import (
"bytes"
"math"
)
var Steps = []rune(" ▁▂▃▄▅▆▇█")
// Line generates a sparkline string from a slice of
// float32.
// Amended from https://github.com/joliv/spark , licensed under the GPLv3.
func Sparkline(nums []float32, min, max float32) string {
if len(nums) == 0 {
return ""
}
indices := normalize(nums, min, max)
var sparkline bytes.Buffer
for _, index := range indices {
sparkline.WriteRune(Steps[index])
}
return sparkline.String()
}
func normalize(nums []float32, min, max float32) []int {
var indices []int
for i, _ := range nums {
nums[i] -= min
}
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
x = float32(math.Floor(float64(x)))
xi := int(x)
if xi >= len(Steps) {
xi = len(Steps) - 1
}
if xi <= 0 {
xi = 0
}
indices = append(indices, xi)
}
return indices
}