-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplots.go
More file actions
228 lines (186 loc) · 4.65 KB
/
Copy pathplots.go
File metadata and controls
228 lines (186 loc) · 4.65 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
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
package margaid
import (
"fmt"
"math"
"strings"
"github.com/erkkah/margaid/svg"
)
type plotOptions struct {
xAxis Axis
yAxis Axis
marker string
strokeWidth float32
}
// Using is the base type for plotting options
type Using func(*plotOptions)
func getPlotOptions(using []Using) plotOptions {
options := plotOptions{
xAxis: XAxis,
yAxis: YAxis,
strokeWidth: 3,
}
for _, u := range using {
u(&options)
}
return options
}
// UsingAxes selects the x and y axis for plotting
func UsingAxes(x, y Axis) Using {
return func(o *plotOptions) {
o.xAxis = x
o.yAxis = y
}
}
// UsingMarker selects a marker for highlighting plotted values.
// See svg.Marker for valid marker types.
func UsingMarker(marker string) Using {
return func(o *plotOptions) {
o.marker = marker
}
}
func UsingStrokeWidth(width float32) Using {
return func(o *plotOptions) {
o.strokeWidth = width
}
}
// Line draws a series using straight lines
func (m *Margaid) Line(series *Series, using ...Using) {
options := getPlotOptions(using)
points, err := m.getProjectedValues(series, options.xAxis, options.yAxis)
if err != nil {
m.error(err.Error())
return
}
id := m.addPlot(series.title)
color := m.getPlotColor(id)
m.g.
StrokeWidth(fmt.Sprintf("%vpx", options.strokeWidth)).
Fill("none").
Stroke(color).
Marker(options.marker).
Transform(
svg.Translation(m.inset, m.height-m.inset),
svg.Scaling(1, -1),
).
Polyline(points...).
Marker("").
Transform()
}
// Smooth draws one series as a smooth curve
func (m *Margaid) Smooth(series *Series, using ...Using) {
options := getPlotOptions(using)
points, err := m.getProjectedValues(series, options.xAxis, options.yAxis)
if err != nil {
m.error(err.Error())
return
}
id := m.addPlot(series.title)
color := m.getPlotColor(id)
m.g.
StrokeWidth(fmt.Sprintf("%vpx", options.strokeWidth)).
Fill("none").
Stroke(color).
Marker(options.marker).
Transform(
svg.Translation(m.inset, m.height-m.inset),
svg.Scaling(1, -1),
)
var path strings.Builder
path.WriteString(fmt.Sprintf("M%e,%e ", points[0].X, points[0].Y))
catmull := catmullRom2bezier(points)
for _, p := range catmull {
path.WriteString(fmt.Sprintf("C%e,%e %e,%e %e,%e ",
p[0].X, p[0].Y,
p[1].X, p[1].Y,
p[2].X, p[2].Y,
))
}
m.g.Path(path.String()).Marker("").Transform()
}
// Bar draws bars for the specified group of series.
func (m *Margaid) Bar(series []*Series, using ...Using) {
if len(series) == 0 {
return
}
options := getPlotOptions(using)
maxSize := 0
for _, s := range series {
if s.Size() > maxSize {
maxSize = s.Size()
}
}
plotWidth := (m.width - 2*m.inset)
barWidth := plotWidth / float64(maxSize)
barWidth /= 1.5
barWidth = math.Min(barWidth, tickDistance)
barWidth /= float64(len(series))
barOffset := -(barWidth / 2) * float64(len(series)-1)
for i, s := range series {
points, err := m.getProjectedValues(s, options.xAxis, options.yAxis)
if err != nil {
m.error(err.Error())
return
}
id := m.addPlot(s.title)
color := m.getPlotColor(id)
m.g.
StrokeWidth("1px").
Color(color).
Transform(
svg.Translation(m.inset, m.height-m.inset),
svg.Scaling(1, -1),
)
for _, p := range points {
m.g.Rect(barOffset+float64(i)*barWidth+p.X-barWidth/2, 0, barWidth, p.Y)
}
}
m.g.Transform()
}
// BezierPoint is one Bezier curve control point
type BezierPoint [3]struct{ X, Y float64 }
// Pulled from:
// https://advancedweb.hu/plotting-charts-with-svg/
func catmullRom2bezier(points []struct{ X, Y float64 }) []BezierPoint {
var result = []BezierPoint{}
for i := range points[0 : len(points)-1] {
p := []struct{ X, Y float64 }{}
idx := int(math.Max(float64(i-1), 0))
p = append(p, struct{ X, Y float64 }{
X: points[idx].X,
Y: points[idx].Y,
})
p = append(p, struct{ X, Y float64 }{
X: points[i].X,
Y: points[i].Y,
})
p = append(p, struct{ X, Y float64 }{
X: points[i+1].X,
Y: points[i+1].Y,
})
idx = int(math.Min(float64(i+2), float64(len(points)-1)))
p = append(p, struct{ X, Y float64 }{
X: points[idx].X,
Y: points[idx].Y,
})
// Catmull-Rom to Cubic Bezier conversion matrix
// 0 1 0 0
// -1/6 1 1/6 0
// 0 1/6 1 -1/6
// 0 0 1 0
bp := BezierPoint{}
bp[0] = struct{ X, Y float64 }{
X: ((-p[0].X + 6*p[1].X + p[2].X) / 6),
Y: ((-p[0].Y + 6*p[1].Y + p[2].Y) / 6),
}
bp[1] = struct{ X, Y float64 }{
X: ((p[1].X + 6*p[2].X - p[3].X) / 6),
Y: ((p[1].Y + 6*p[2].Y - p[3].Y) / 6),
}
bp[2] = struct{ X, Y float64 }{
X: p[2].X,
Y: p[2].Y,
}
result = append(result, bp)
}
return result
}