-
Notifications
You must be signed in to change notification settings - Fork 71
/
errbars.go
261 lines (216 loc) · 6.6 KB
/
errbars.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
// Copyright (c) 2024, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plots
import (
"cogentcore.org/core/math32"
"cogentcore.org/core/plot"
"cogentcore.org/core/styles/units"
)
//////////////////////////////////////////////////
// XErrorer
// XErrorer provides an interface for a list of Low, High error bar values.
// This is used in addition to an XYer interface, if implemented.
type XErrorer interface {
// XError returns Low, High error values for X data.
XError(i int) (low, high float32)
}
// Errors is a slice of low and high error values.
type Errors []struct{ Low, High float32 }
// XErrors implements the XErrorer interface.
type XErrors Errors
func (xe XErrors) XError(i int) (low, high float32) {
return xe[i].Low, xe[i].High
}
// YErrorer provides an interface for YError method.
// This is used in addition to an XYer interface, if implemented.
type YErrorer interface {
// YError returns two error values for Y data.
YError(i int) (float32, float32)
}
// YErrors implements the YErrorer interface.
type YErrors Errors
func (ye YErrors) YError(i int) (float32, float32) {
return ye[i].Low, ye[i].High
}
// YErrorBars implements the plot.Plotter, plot.DataRanger,
// and plot.GlyphBoxer interfaces, drawing vertical error
// bars, denoting error in Y values.
type YErrorBars struct {
// XYs is a copy of the points for this line.
plot.XYs
// YErrors is a copy of the Y errors for each point.
YErrors
// PXYs is the actual pixel plotting coordinates for each XY value,
// representing the high, center value of the error bar.
PXYs plot.XYs
// LineStyle is the style used to draw the error bars.
LineStyle plot.LineStyle
// CapWidth is the width of the caps drawn at the top of each error bar.
CapWidth units.Value
}
func (eb *YErrorBars) Defaults() {
eb.LineStyle.Defaults()
eb.CapWidth.Dp(10)
}
// NewYErrorBars returns a new YErrorBars plotter, or an error on failure.
// The error values from the YErrorer interface are interpreted as relative
// to the corresponding Y value. The errors for a given Y value are computed
// by taking the absolute value of the error returned by the YErrorer
// and subtracting the first and adding the second to the Y value.
func NewYErrorBars(yerrs interface {
plot.XYer
YErrorer
}) (*YErrorBars, error) {
errors := make(YErrors, yerrs.Len())
for i := range errors {
errors[i].Low, errors[i].High = yerrs.YError(i)
if err := plot.CheckFloats(errors[i].Low, errors[i].High); err != nil {
return nil, err
}
}
xys, err := plot.CopyXYs(yerrs)
if err != nil {
return nil, err
}
eb := &YErrorBars{
XYs: xys,
YErrors: errors,
}
eb.Defaults()
return eb, nil
}
func (e *YErrorBars) XYData() (data plot.XYer, pixels plot.XYer) {
data = e.XYs
pixels = e.PXYs
return
}
// Plot implements the Plotter interface, drawing labels.
func (e *YErrorBars) Plot(plt *plot.Plot) {
pc := plt.Paint
uc := &pc.UnitContext
e.CapWidth.ToDots(uc)
cw := 0.5 * e.CapWidth.Dots
nv := len(e.YErrors)
e.PXYs = make(plot.XYs, nv)
e.LineStyle.SetStroke(plt)
for i, err := range e.YErrors {
x := plt.PX(e.XYs[i].X)
ylow := plt.PY(e.XYs[i].Y - math32.Abs(err.Low))
yhigh := plt.PY(e.XYs[i].Y + math32.Abs(err.High))
e.PXYs[i].X = x
e.PXYs[i].Y = yhigh
pc.MoveTo(x, ylow)
pc.LineTo(x, yhigh)
pc.MoveTo(x-cw, ylow)
pc.LineTo(x+cw, ylow)
pc.MoveTo(x-cw, yhigh)
pc.LineTo(x+cw, yhigh)
pc.Stroke()
}
}
// DataRange implements the plot.DataRanger interface.
func (e *YErrorBars) DataRange() (xmin, xmax, ymin, ymax float32) {
xmin, xmax = plot.Range(plot.XValues{e})
ymin = math32.Inf(1)
ymax = math32.Inf(-1)
for i, err := range e.YErrors {
y := e.XYs[i].Y
ylow := y - math32.Abs(err.Low)
yhigh := y + math32.Abs(err.High)
ymin = math32.Min(math32.Min(math32.Min(ymin, y), ylow), yhigh)
ymax = math32.Max(math32.Max(math32.Max(ymax, y), ylow), yhigh)
}
return
}
// XErrorBars implements the plot.Plotter, plot.DataRanger,
// and plot.GlyphBoxer interfaces, drawing horizontal error
// bars, denoting error in Y values.
type XErrorBars struct {
// XYs is a copy of the points for this line.
plot.XYs
// XErrors is a copy of the X errors for each point.
XErrors
// PXYs is the actual pixel plotting coordinates for each XY value,
// representing the high, center value of the error bar.
PXYs plot.XYs
// LineStyle is the style used to draw the error bars.
LineStyle plot.LineStyle
// CapWidth is the width of the caps drawn at the top
// of each error bar.
CapWidth units.Value
}
// Returns a new XErrorBars plotter, or an error on failure. The error values
// from the XErrorer interface are interpreted as relative to the corresponding
// X value. The errors for a given X value are computed by taking the absolute
// value of the error returned by the XErrorer and subtracting the first and
// adding the second to the X value.
func NewXErrorBars(xerrs interface {
plot.XYer
XErrorer
}) (*XErrorBars, error) {
errors := make(XErrors, xerrs.Len())
for i := range errors {
errors[i].Low, errors[i].High = xerrs.XError(i)
if err := plot.CheckFloats(errors[i].Low, errors[i].High); err != nil {
return nil, err
}
}
xys, err := plot.CopyXYs(xerrs)
if err != nil {
return nil, err
}
eb := &XErrorBars{
XYs: xys,
XErrors: errors,
}
eb.Defaults()
return eb, nil
}
func (eb *XErrorBars) Defaults() {
eb.LineStyle.Defaults()
eb.CapWidth.Dp(10)
}
func (e *XErrorBars) XYData() (data plot.XYer, pixels plot.XYer) {
data = e.XYs
pixels = e.PXYs
return
}
// Plot implements the Plotter interface, drawing labels.
func (e *XErrorBars) Plot(plt *plot.Plot) {
pc := plt.Paint
uc := &pc.UnitContext
e.CapWidth.ToDots(uc)
cw := 0.5 * e.CapWidth.Dots
nv := len(e.XErrors)
e.PXYs = make(plot.XYs, nv)
e.LineStyle.SetStroke(plt)
for i, err := range e.XErrors {
y := plt.PY(e.XYs[i].Y)
xlow := plt.PX(e.XYs[i].X - math32.Abs(err.Low))
xhigh := plt.PX(e.XYs[i].X + math32.Abs(err.High))
e.PXYs[i].X = xhigh
e.PXYs[i].Y = y
pc.MoveTo(xlow, y)
pc.LineTo(xhigh, y)
pc.MoveTo(xlow, y-cw)
pc.LineTo(xlow, y+cw)
pc.MoveTo(xhigh, y-cw)
pc.LineTo(xhigh, y+cw)
pc.Stroke()
}
}
// DataRange implements the plot.DataRanger interface.
func (e *XErrorBars) DataRange() (xmin, xmax, ymin, ymax float32) {
ymin, ymax = plot.Range(plot.YValues{e})
xmin = math32.Inf(1)
xmax = math32.Inf(-1)
for i, err := range e.XErrors {
x := e.XYs[i].X
xlow := x - math32.Abs(err.Low)
xhigh := x + math32.Abs(err.High)
xmin = math32.Min(math32.Min(math32.Min(xmin, x), xlow), xhigh)
xmax = math32.Max(math32.Max(math32.Max(xmax, x), xlow), xhigh)
}
return
}