-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaxes.go
More file actions
170 lines (145 loc) · 3.7 KB
/
Copy pathaxes.go
File metadata and controls
170 lines (145 loc) · 3.7 KB
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
package margaid
import (
"fmt"
"github.com/erkkah/margaid/svg"
)
// Axis is the type for all axis constants
type Axis int
// Axis constants
const (
XAxis Axis = iota + 'x'
YAxis
X2Axis
Y2Axis
X1Axis = XAxis
Y1Axis = YAxis
)
// Axis draws tick marks and labels using the specified ticker
func (m *Margaid) Axis(series *Series, axis Axis, ticker Ticker, grid bool, title string) {
var xOffset = m.inset
var yOffset = m.inset
var axisLength float64
var crossLength float64
var xMult float64
var yMult float64
var tickSign float64 = 1
var vAlignment svg.VAlignment
var hAlignment svg.HAlignment
var axisLabelRotation = 0.0
var axisLabelAlignment svg.VAlignment
var axisLabelSign float64 = 1
max := m.ranges[axis].max
xAttributes := func() {
axisLength = m.width - 2*m.inset
crossLength = m.height - 2*m.inset
xMult = 1
hAlignment = svg.HAlignMiddle
}
yAttributes := func() {
yOffset = m.height - m.inset
axisLength = m.height - 2*m.inset
crossLength = m.width - 2*m.inset
yMult = 1
vAlignment = svg.VAlignCentral
axisLabelRotation = -90.0
}
switch axis {
case X1Axis:
xAttributes()
yOffset = m.height - m.inset
tickSign = -1
vAlignment = svg.VAlignTop
axisLabelAlignment = svg.VAlignBottom
axisLabelSign = -1
case X2Axis:
xAttributes()
vAlignment = svg.VAlignBottom
axisLabelAlignment = svg.VAlignTop
case Y1Axis:
yAttributes()
tickSign = -1
hAlignment = svg.HAlignEnd
axisLabelAlignment = svg.VAlignTop
case Y2Axis:
yAttributes()
xOffset = m.width - m.inset
hAlignment = svg.HAlignStart
axisLabelAlignment = svg.VAlignBottom
axisLabelSign = -1
}
steps := axisLength / tickDistance
start := ticker.start(axis, series, int(steps))
m.g.Transform(
svg.Translation(xOffset, yOffset),
svg.Scaling(1, -1),
).
StrokeWidth("2px").
Stroke("black")
var tick float64
var hasMore = true
for tick = start; tick <= max && hasMore; tick, hasMore = ticker.next(tick) {
value, err := m.project(tick, axis)
if err == nil {
m.g.Polyline([]struct{ X, Y float64 }{
{value * xMult, value * yMult},
{value*xMult + tickSign*tickSize*(yMult), value*yMult + tickSign*(xMult)*tickSize},
}...)
}
}
m.g.Transform(
svg.Translation(xOffset, yOffset),
svg.Scaling(1, 1),
).
Font(m.labelFamily, fmt.Sprintf("%dpx", m.labelSize)).
FontStyle(svg.StyleNormal, svg.WeightNormal).
Alignment(hAlignment, vAlignment).
Fill("black")
lastLabel := -m.inset
textOffset := float64(tickSize + textSpacing)
hasMore = true
for tick = start; tick <= max && hasMore; tick, hasMore = ticker.next(tick) {
value, err := m.project(tick, axis)
if err == nil {
if value-lastLabel > float64(m.labelSize) {
m.g.Text(
value*xMult+(tickSign)*textOffset*(yMult),
-value*yMult+(-tickSign)*textOffset*(xMult),
ticker.label(tick))
lastLabel = value
}
}
}
if title != "" {
m.g.Transform(
svg.Translation(xOffset, yOffset),
svg.Scaling(1, 1),
svg.Rotation(axisLabelRotation, 0, 0),
).
Font(m.labelFamily, fmt.Sprintf("%dpx", m.labelSize)).
FontStyle(svg.StyleNormal, svg.WeightBold).
Alignment(svg.HAlignMiddle, axisLabelAlignment).
Fill("black")
x := axisLength / 2
y := float64(tickSize * axisLabelSign)
m.g.Text(
x, y, title,
)
}
if grid {
m.g.Transform(
svg.Translation(xOffset, yOffset),
svg.Scaling(1, -1),
).
StrokeWidth("0.5px").Stroke("gray")
hasMore = true
for tick = start; tick <= max && hasMore; tick, hasMore = ticker.next(tick) {
value, err := m.project(tick, axis)
if err == nil {
m.g.Polyline([]struct{ X, Y float64 }{
{value * xMult, value * yMult},
{value*xMult - tickSign*crossLength*(yMult), value*yMult - tickSign*(xMult)*crossLength},
}...)
}
}
}
}