-
Notifications
You must be signed in to change notification settings - Fork 3
/
bar_chart.go
55 lines (41 loc) · 1.13 KB
/
bar_chart.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
package main
import "sort"
type bucket struct {
Category string
Frequency int
}
func categoricalBuckets(vals []string) []bucket {
buckets := make(map[string]int)
for _, v := range vals {
if _, ok := buckets[v]; !ok {
buckets[v] = 0
}
buckets[v]++
}
res := make([]bucket, 0, len(buckets))
for k, v := range buckets {
res = append(res, bucket{Category: k, Frequency: v})
}
return res
}
func sortBuckets(buckets []bucket, orderBy string, desc bool) {
var sorter sort.Interface
switch orderBy {
case "category":
sorter = byCategory(buckets)
default:
sorter = byFrequency(buckets)
}
if desc {
sorter = sort.Reverse(sorter)
}
sort.Sort(sorter)
}
type byCategory []bucket
func (b byCategory) Len() int { return len(b) }
func (b byCategory) Less(i, j int) bool { return b[i].Category < b[j].Category }
func (b byCategory) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
type byFrequency []bucket
func (b byFrequency) Len() int { return len(b) }
func (b byFrequency) Less(i, j int) bool { return b[i].Frequency < b[j].Frequency }
func (b byFrequency) Swap(i, j int) { b[i], b[j] = b[j], b[i] }