forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tick.go
305 lines (251 loc) · 8.66 KB
/
tick.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
300
301
302
303
304
305
package chart
import (
"fmt"
"math"
"strings"
"github.com/beevee/go-chart/util"
)
// TicksProvider is a type that provides ticks.
type TicksProvider interface {
GetTicks(r Renderer, defaults Style, vf ValueFormatter) []Tick
}
// Tick represents a label on an axis.
type Tick struct {
Value float64
Label string
}
// Ticks is an array of ticks.
type Ticks []Tick
// Len returns the length of the ticks set.
func (t Ticks) Len() int {
return len(t)
}
// Swap swaps two elements.
func (t Ticks) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
// Less returns if i's value is less than j's value.
func (t Ticks) Less(i, j int) bool {
return t[i].Value < t[j].Value
}
// String returns a string representation of the set of ticks.
func (t Ticks) String() string {
var values []string
for i, tick := range t {
values = append(values, fmt.Sprintf("[%d: %s]", i, tick.Label))
}
return strings.Join(values, ", ")
}
// GenerateContinuousTicks generates a set of ticks.
func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) []Tick {
if vf == nil {
vf = FloatValueFormatter
}
var ticks []Tick
min, max := ra.GetMin(), ra.GetMax()
if ra.IsDescending() {
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
} else {
ticks = append(ticks, Tick{
Value: min,
Label: vf(min),
})
}
minLabel := vf(min)
style.GetTextOptions().WriteToRenderer(r)
labelBox := r.MeasureText(minLabel)
var tickSize float64
if isVertical {
tickSize = float64(labelBox.Height() + DefaultMinimumTickVerticalSpacing)
} else {
tickSize = float64(labelBox.Width() + DefaultMinimumTickHorizontalSpacing)
}
domain := float64(ra.GetDomain())
domainRemainder := domain - (tickSize * 2)
intermediateTickCount := int(math.Floor(float64(domainRemainder) / float64(tickSize)))
rangeDelta := math.Abs(max - min)
tickStep := rangeDelta / float64(intermediateTickCount)
roundTo := util.Math.GetRoundToForDelta(rangeDelta) / 10
intermediateTickCount = util.Math.MinInt(intermediateTickCount, DefaultTickCountSanityCheck)
for x := 1; x < intermediateTickCount; x++ {
var tickValue float64
if ra.IsDescending() {
tickValue = max - util.Math.RoundUp(tickStep*float64(x), roundTo)
} else {
tickValue = min + util.Math.RoundUp(tickStep*float64(x), roundTo)
}
ticks = append(ticks, Tick{
Value: tickValue,
Label: vf(tickValue),
})
}
if ra.IsDescending() {
ticks = append(ticks, Tick{
Value: min,
Label: vf(min),
})
} else {
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
}
return ticks
}
// GeneratePrettyContinuousTicks generates a set of ticks at visually pleasing intervals.
// Based on http://vis.stanford.edu/files/2010-TickLabels-InfoVis.pdf by Justin Talbot et. al.
func GeneratePrettyContinuousTicks(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) []Tick {
if vf == nil {
vf = FloatValueFormatter
}
prettyStepsPriorityList := []float64{1, 5, 2, 2.5, 4, 3}
paramWeights := map[string]float64{
"simplicity": 0.2,
"coverage": 0.25,
"density": 0.5,
"legibility": 0.05,
}
rangeMin, rangeMax := ra.GetMin(), ra.GetMax()
if rangeMin >= rangeMax || ra.GetDomain() == 0 {
return []Tick{}
}
renderedLabelExample := vf(rangeMin)
style.GetTextOptions().WriteToRenderer(r)
renderedLabelSizePx := r.MeasureText(renderedLabelExample)
var actualLabelSizePx, desiredPaddedLabelSizePx float64
if isVertical {
actualLabelSizePx = math.Max(float64(renderedLabelSizePx.Height()), 1)
desiredPaddedLabelSizePx = actualLabelSizePx + DefaultMinimumTickVerticalSpacing
} else {
actualLabelSizePx = math.Max(float64(renderedLabelSizePx.Width()), 1)
desiredPaddedLabelSizePx = actualLabelSizePx + DefaultMinimumTickHorizontalSpacing
}
availableSpacePx := float64(ra.GetDomain())
desiredTicksCount := math.Min(
math.Max(math.Floor(availableSpacePx/desiredPaddedLabelSizePx), 2), // less than 2 leads to incorrect density calculation
DefaultTickCountSanityCheck)
prettyStepsCount := float64(len(prettyStepsPriorityList))
var bestTickMin, bestTickMax, bestTickStep float64
bestScore := -2.0
stepsToSkip := 1.0
OUTER:
for {
for prettyStepIndex, prettyStep := range prettyStepsPriorityList {
simplicityMax := calculateSimplicityMax(float64(prettyStepIndex), prettyStepsCount, stepsToSkip)
if paramWeights["simplicity"]*simplicityMax+
paramWeights["coverage"]+
paramWeights["density"]+
paramWeights["legibility"] < bestScore {
break OUTER
}
ticksCount := 2.0
for {
densityMax := calculateDensityMax(ticksCount, desiredTicksCount)
if paramWeights["simplicity"]*simplicityMax+
paramWeights["coverage"]+
paramWeights["density"]*densityMax+
paramWeights["legibility"] < bestScore {
break
}
delta := (rangeMax - rangeMin) / (ticksCount + 1) / stepsToSkip / prettyStep
stepSizeMultiplierLog := math.Ceil(math.Log10(delta))
for {
tickStep := stepsToSkip * prettyStep * math.Pow(10, stepSizeMultiplierLog)
coverageMax := calculateCoverageMax(rangeMin, rangeMax, tickStep*(ticksCount-1))
if paramWeights["simplicity"]*simplicityMax+
paramWeights["coverage"]*coverageMax+
paramWeights["density"]*densityMax+
paramWeights["legibility"] < bestScore {
break
}
minStart := math.Floor(rangeMax/tickStep)*stepsToSkip - (ticksCount-1)*stepsToSkip
maxStart := math.Ceil(rangeMin/tickStep) * stepsToSkip
if minStart > maxStart {
stepSizeMultiplierLog += 1
continue
}
for start := minStart; start <= maxStart; start++ {
tickMin := start * (tickStep / stepsToSkip)
tickMax := tickMin + tickStep*(ticksCount-1)
coverage := calculateCoverage(rangeMin, rangeMax, tickMin, tickMax)
simplicity := calculateSimplicity(prettyStepsCount, float64(prettyStepIndex), stepsToSkip, tickMin, tickMax, tickStep)
density := calculateDensity(ticksCount, desiredTicksCount, rangeMin, rangeMax, tickMin, tickMax)
legibility := 1.0 // format is out of our control (provided by ValueFormatter)
// font size is out of our control (provided by Style)
// orientation is out of our control
if actualLabelSizePx*ticksCount > availableSpacePx {
legibility = math.Inf(-1) // overlap is unacceptable
}
score := paramWeights["simplicity"]*simplicity +
paramWeights["coverage"]*coverage +
paramWeights["density"]*density +
paramWeights["legibility"]*legibility
// original algorithm allows ticks outside value range, but it breaks rendering in this library
if score > bestScore && tickMin >= rangeMin && tickMax <= rangeMax {
bestTickMin = tickMin
bestTickMax = tickMax
bestTickStep = tickStep
bestScore = score
}
}
stepSizeMultiplierLog++
}
ticksCount++
}
}
stepsToSkip++
}
var ticks []Tick
if bestTickStep == 0 {
return ticks
}
if ra.IsDescending() {
for tickValue := bestTickMax; tickValue > bestTickMin-bestTickStep/2; tickValue -= bestTickStep {
ticks = append(ticks, Tick{
Value: tickValue,
Label: vf(tickValue),
})
}
} else {
for tickValue := bestTickMin; tickValue < bestTickMax+bestTickStep/2; tickValue += bestTickStep {
ticks = append(ticks, Tick{
Value: tickValue,
Label: vf(tickValue),
})
}
}
return ticks
}
func calculateSimplicity(prettyStepsCount, prettyStepIndex, stepsToSkip, tickMin, tickMax, tickStep float64) float64 {
var hasZeroTick float64
if tickMin <= 0 && tickMax >= 0 && math.Mod(tickMin, tickStep) < 10e-10 {
hasZeroTick = 1
}
return 1 - prettyStepIndex/(prettyStepsCount-1) - stepsToSkip + hasZeroTick
}
func calculateSimplicityMax(prettyStepIndex, prettyStepsCount, stepsToSkip float64) float64 {
return 2 - prettyStepIndex/(prettyStepsCount-1) - stepsToSkip
}
func calculateCoverage(rangeMin, rangeMax, tickMin, tickMax float64) float64 {
return 1 - 0.5*(math.Pow(rangeMax-tickMax, 2)+math.Pow(rangeMin-tickMin, 2))/math.Pow(0.1*(rangeMax-rangeMin), 2)
}
func calculateCoverageMax(rangeMin, rangeMax, span float64) float64 {
if span <= rangeMax-rangeMin {
return 1
}
return 1 - math.Pow((rangeMax-rangeMin)/2, 2)/math.Pow(0.1*(rangeMax-rangeMin), 2)
}
func calculateDensity(ticksCount, desiredTicksCount, rangeMin, rangeMax, tickMin, tickMax float64) float64 {
ticksDensity := (ticksCount - 1) / (tickMax - tickMin)
desiredTicksDensity := (desiredTicksCount - 1) / (math.Max(tickMax, rangeMax) - math.Min(tickMin, rangeMin))
return 2 - math.Max(ticksDensity/desiredTicksDensity, desiredTicksDensity/ticksDensity)
}
func calculateDensityMax(ticksCount, desiredTicksCount float64) float64 {
if ticksCount >= desiredTicksCount {
return 2 - (ticksCount-1)/(desiredTicksCount-1)
}
return 1
}