-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.go
299 lines (243 loc) · 6.16 KB
/
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package chart
import (
"cmp"
"errors"
"fmt"
"io"
"math"
"regexp"
"slices"
"strconv"
"strings"
"sync"
)
var (
dataSepRE = regexp.MustCompile(`[\s,;:|#]`) // Matches common separator symbols in tabular data.
floatRE = regexp.MustCompile(`[\d\.]`) // Matches integer and float values.
)
// SortOption represents a sort option for a [Chart].
type SortOption int
// SortDirection represents a sorting direction for a [Chart].
type SortDirection int
const (
SortNone SortOption = iota // No sorting.
SortByLabel // Sort by label alphabetically.
SortByLabelNumeric // Sort by label numerically.
SortByValue // Sort by value.
)
const (
OrderNone SortDirection = iota // No ordering.
OrderAsc // Ascending order.
OrderDesc // Descending order.
)
// Default option values.
const (
DefaultSort = SortNone
DefaultSortDirection = OrderNone
DefaultPrecision = 2
)
// Renderer renders a chart to a writer.
type Renderer interface {
// Render renders the given chart and writes it to the writer.
Render(*Chart, io.Writer) (int, error)
}
// orderedMap wraps a map of labels and data to record the order of insertion.
type orderedMap struct {
m map[string]float64
k []string
mu sync.RWMutex
}
func newOrderedMap() *orderedMap {
return &orderedMap{m: make(map[string]float64)}
}
func (m *orderedMap) set(key string, val float64) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.m[key]; ok {
m.m[key] = val
return
}
m.k = append(m.k, key)
m.m[key] = val
}
func (m *orderedMap) get(key string) (float64, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
if val, ok := m.m[key]; ok {
return val, ok
}
return 0, false
}
func (m *orderedMap) keys() []string {
m.mu.RLock()
defer m.mu.RUnlock()
cp := make([]string, len(m.k))
copy(cp, m.k)
return cp
}
func (m *orderedMap) values() []float64 {
m.mu.RLock()
defer m.mu.RUnlock()
vals := make([]float64, 0, len(m.k))
for _, k := range m.k {
vals = append(vals, m.m[k])
}
return vals
}
// Chart represents a simple bar chart.
type Chart struct {
data *orderedMap
sort SortOption
sortDir SortDirection
p float64
}
// New creates a new [Chart] configured with given options.
func New(opts ...ChartOption) (*Chart, error) {
c := &Chart{
data: newOrderedMap(),
sort: DefaultSort,
sortDir: DefaultSortDirection,
p: math.Pow(10, DefaultPrecision),
}
for i, opt := range opts {
if err := opt(c); err != nil {
return nil, fmt.Errorf("applying option #%d: %w", i+1, err)
}
}
return c, nil
}
// Set sets the value for a label.
func (c *Chart) Set(label string, value float64) *Chart {
c.data.set(label, value)
return c
}
// Add adds the number to a label's value.
// If label is not registered, it is added to the chart.
func (c *Chart) Add(label string, value float64) *Chart {
if val, ok := c.data.get(label); ok {
value += val
}
return c.Set(label, value)
}
// Labels returns chart labels sorted and ordered according to configuration.
func (c *Chart) Labels() []string {
labels := c.data.keys()
switch c.sort {
case SortByLabel:
slices.SortStableFunc(labels, cmp.Compare)
case SortByLabelNumeric:
slices.SortStableFunc(labels, func(i, j string) int {
return cmp.Compare(stringToInt(i), stringToInt(j))
})
case SortByValue:
slices.SortStableFunc(labels, func(i, j string) int {
iVal, _ := c.data.get(i)
jVal, _ := c.data.get(j)
return cmp.Compare(iVal, jVal)
})
}
if c.sortDir == OrderDesc {
slices.Reverse(labels)
}
return labels
}
// Value returns the value for a label.
// Returns an error if label does not exist.
func (c *Chart) Value(label string) (float64, error) {
if val, ok := c.data.get(label); ok {
return math.Round(val*c.p) / c.p, nil
}
return 0, errors.New("unknown label")
}
// MaxValue returns the highest chart value.
func (c *Chart) MaxValue() float64 {
vals := c.data.values()
if len(vals) == 0 {
return 0
}
maxVal := 0.0
for _, val := range vals {
if val > maxVal {
maxVal = val
}
}
return math.Round(maxVal*c.p) / c.p
}
// MaxLabel returns the longest chart label.
func (c *Chart) MaxLabel() string {
labels := c.data.keys()
if len(labels) == 0 {
return ""
}
maxLabel := ""
for _, label := range labels {
if len(label) > len(maxLabel) {
maxLabel = label
}
}
return maxLabel
}
// ChartOption configures a [Chart].
type ChartOption func(*Chart) error
// WithSorting configures a [Chart] with bar sorting options.
func WithSorting(sort SortOption, dir SortDirection) ChartOption {
return func(c *Chart) error {
c.sort = sort
c.sortDir = dir
return nil
}
}
// WithPrecision configures a [Chart] with a precision for values.
func WithPrecision(p int) ChartOption {
return func(c *Chart) error {
if p < 0 {
p = 0
}
c.p = math.Pow(10, float64(p))
return nil
}
}
// ParseLine parses a data line into its float64 value and label string.
//
// The line is expected to have the following structure:
//
// <numeric value> <label>
//
// The function tolerates any kind of whitespace between the value and label, as
// well as currency symbols and punctuation.
func ParseLine(line string) (float64, string, error) {
sepIdx := dataSepRE.FindStringIndex(line)
if sepIdx == nil {
return 0, "", errors.New("missing data separator")
}
value := strings.TrimSpace(line[0:sepIdx[0]])
label := strings.TrimSpace(line[sepIdx[1]:])
if label == "" {
return 0, "", errors.New("missing label")
}
value = strings.TrimSuffix(strings.Join(floatRE.FindAllString(value, -1), ""), ".")
if value == "" {
return 0, "", errors.New("missing value")
}
count, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, "", fmt.Errorf("parsing %q as float64: %w", value, err)
}
return count, label, nil
}
// stringToInt strips all non-numeric characters from a string and converts it
// to an integer. Returns 0 if conversion fails.
func stringToInt(s string) int {
if num, err := strconv.Atoi(s); err == nil {
return num
}
numStr := strings.Join(floatRE.FindAllString(s, -1), "")
if numStr == "" {
return 0
}
num, err := strconv.Atoi(numStr)
if err != nil {
return 0
}
return num
}