forked from mum4k/termdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
266 lines (231 loc) · 7.85 KB
/
label.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
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package axes
// label.go contains code that calculates the positions of labels on the axes.
import (
"fmt"
"image"
"github.com/mum4k/termdash/align"
"github.com/mum4k/termdash/internal/alignfor"
)
// LabelOrientation represents the orientation of text labels.
type LabelOrientation int
// String implements fmt.Stringer()
func (lo LabelOrientation) String() string {
if n, ok := labelOrientationNames[lo]; ok {
return n
}
return "LabelOrientationUnknown"
}
// labelOrientationNames maps LabelOrientation values to human readable names.
var labelOrientationNames = map[LabelOrientation]string{
LabelOrientationHorizontal: "LabelOrientationHorizontal",
LabelOrientationVertical: "LabelOrientationVertical",
}
const (
// LabelOrientationHorizontal is the default label orientation where text
// flows horizontally.
LabelOrientationHorizontal LabelOrientation = iota
// LabelOrientationVertical is an orientation where text flows vertically.
LabelOrientationVertical
)
// Label is one value label on an axis.
type Label struct {
// Value if the value to be displayed.
Value *Value
// Position of the label within the canvas.
Pos image.Point
}
// yLabels returns labels that should be placed next to the Y axis.
// The labelWidth is the width of the area from the left-most side of the
// canvas until the Y axis (not including the Y axis). This is the area where
// the labels will be placed and aligned.
// Labels are returned in an increasing value order.
// Label value is not trimmed to the provided labelWidth, the label width is
// only used to align the labels. Alignment is done with the assumption that
// longer labels will be trimmed.
func yLabels(scale *YScale, labelWidth int) ([]*Label, error) {
if min := 2; scale.GraphHeight < min {
return nil, fmt.Errorf("cannot place labels on a canvas with height %d, minimum is %d", scale.GraphHeight, min)
}
if min := 0; labelWidth < min {
return nil, fmt.Errorf("cannot place labels in label area width %d, minimum is %d", labelWidth, min)
}
var labels []*Label
const labelSpacing = 4
seen := map[string]bool{}
for y := scale.GraphHeight - 1; y >= 0; y -= labelSpacing {
label, err := rowLabel(scale, y, labelWidth)
if err != nil {
return nil, err
}
if !seen[label.Value.Text()] {
labels = append(labels, label)
seen[label.Value.Text()] = true
}
}
// If we have data, place at least two labels, first and last.
haveData := scale.Min.Rounded != 0 || scale.Max.Rounded != 0
if len(labels) < 2 && haveData {
const maxRow = 0
label, err := rowLabel(scale, maxRow, labelWidth)
if err != nil {
return nil, err
}
labels = append(labels, label)
}
return labels, nil
}
// rowLabelArea determines the area available for labels on the specified row.
// The row is the Y coordinate of the row, Y coordinates grow down.
func rowLabelArea(row int, labelWidth int) image.Rectangle {
return image.Rect(0, row, labelWidth, row+1)
}
// rowLabel returns label for the specified row.
func rowLabel(scale *YScale, y int, labelWidth int) (*Label, error) {
v, err := scale.CellLabel(y)
if err != nil {
return nil, fmt.Errorf("unable to determine label value for row %d: %v", y, err)
}
ar := rowLabelArea(y, labelWidth)
pos, err := alignfor.Text(ar, v.Text(), align.HorizontalRight, align.VerticalMiddle)
if err != nil {
return nil, fmt.Errorf("unable to align the label value: %v", err)
}
return &Label{
Value: v,
Pos: pos,
}, nil
}
// xSpace represents an available space among the X axis.
type xSpace struct {
// min is the current relative coordinate.
// These are zero based, i.e. not adjusted to axisStart.
cur int
// max is the maximum relative coordinate.
// These are zero based, i.e. not adjusted to axisStart.
// The xSpace instance contains points 0 <= x < max
max int
// graphZero is the (0, 0) point on the graph.
graphZero image.Point
}
// newXSpace returns a new xSpace instance initialized for the provided width.
func newXSpace(graphZero image.Point, graphWidth int) *xSpace {
return &xSpace{
cur: 0,
max: graphWidth,
graphZero: graphZero,
}
}
// Implements fmt.Stringer.
func (xs *xSpace) String() string {
return fmt.Sprintf("xSpace(size:%d)-cur:%v-max:%v", xs.Remaining(), image.Point{xs.cur, xs.graphZero.Y}, image.Point{xs.max, xs.graphZero.Y})
}
// Remaining returns the remaining size on the X axis.
func (xs *xSpace) Remaining() int {
return xs.max - xs.cur
}
// Relative returns the relative coordinate within the space, these are zero
// based.
func (xs *xSpace) Relative() image.Point {
return image.Point{xs.cur, xs.graphZero.Y + 1}
}
// LabelPos returns the absolute coordinate on the canvas where a label should
// be placed. The is the coordinate that represents the current relative
// coordinate of the space.
func (xs *xSpace) LabelPos() image.Point {
return image.Point{xs.cur + xs.graphZero.X, xs.graphZero.Y + 2} // First down is the axis, second the label.
}
// Sub subtracts the specified size from the beginning of the available
// space.
func (xs *xSpace) Sub(size int) error {
if xs.Remaining() < size {
return fmt.Errorf("unable to subtract %d from the start, not enough size in %v", size, xs)
}
xs.cur += size
return nil
}
// xLabels returns labels that should be placed under the X axis.
// The graphZero is the (0, 0) point of the graph area on the canvas.
// Labels are returned in an increasing value order.
// Returned labels shouldn't be trimmed, their count is adjusted so that they
// fit under the width of the axis.
// The customLabels map value positions in the series to the desired custom
// label. These are preferred if present.
func xLabels(scale *XScale, graphZero image.Point, customLabels map[int]string, lo LabelOrientation) ([]*Label, error) {
space := newXSpace(graphZero, scale.GraphWidth)
const minSpacing = 3
var res []*Label
next := int(scale.Min.Value)
for haveLabels := 0; haveLabels <= int(scale.Max.Value); haveLabels = len(res) {
label, err := colLabel(scale, space, customLabels, lo)
if err != nil {
return nil, err
}
if label == nil {
break
}
res = append(res, label)
next++
if next > int(scale.Max.Value) {
break
}
nextCell, err := scale.ValueToCell(next)
if err != nil {
return nil, err
}
skip := nextCell - space.Relative().X
if skip < minSpacing {
skip = minSpacing
}
if space.Remaining() <= skip {
break
}
if err := space.Sub(skip); err != nil {
return nil, err
}
}
return res, nil
}
// colLabel returns a label placed at the beginning of the space.
// The space is adjusted according to how much space was taken by the label.
// Returns nil, nil if the label doesn't fit in the space.
func colLabel(scale *XScale, space *xSpace, customLabels map[int]string, lo LabelOrientation) (*Label, error) {
pos := space.Relative()
label, err := scale.CellLabel(pos.X)
if err != nil {
return nil, fmt.Errorf("unable to determine label value for column %d: %v", pos.X, err)
}
if custom, ok := customLabels[int(label.Value)]; ok {
label = NewTextValue(custom)
}
var labelLen int
switch lo {
case LabelOrientationHorizontal:
labelLen = len(label.Text())
case LabelOrientationVertical:
labelLen = 1
}
if labelLen > space.Remaining() {
return nil, nil
}
abs := space.LabelPos()
if err := space.Sub(labelLen); err != nil {
return nil, err
}
return &Label{
Value: label,
Pos: abs,
}, nil
}