forked from gonum/plot
/
quartile.go
283 lines (245 loc) · 7.3 KB
/
quartile.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
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plotter
import (
"image/color"
"github.com/elamre/plot"
"github.com/elamre/plot/vg"
"github.com/elamre/plot/vg/draw"
)
var (
// DefaultQuartMedianStyle is a fat dot.
DefaultQuartMedianStyle = draw.GlyphStyle{
Color: color.Black,
Radius: vg.Points(1.5),
Shape: draw.CircleGlyph{},
}
// DefaultQuartWhiskerStyle is a hairline.
DefaultQuartWhiskerStyle = draw.LineStyle{
Color: color.Black,
Width: vg.Points(0.5),
Dashes: []vg.Length{},
DashOffs: 0,
}
)
// QuartPlot implements the Plotter interface, drawing
// a plot to represent the distribution of values.
//
// This style of the plot appears in Tufte's "The Visual
// Display of Quantitative Information".
type QuartPlot struct {
fiveStatPlot
// Offset is added to the x location of each plot.
// When the Offset is zero, the plot is drawn
// centered at its x location.
Offset vg.Length
// MedianStyle is the line style for the median point.
MedianStyle draw.GlyphStyle
// WhiskerStyle is the line style used to draw the
// whiskers.
WhiskerStyle draw.LineStyle
// Horizontal dictates whether the QuartPlot should be in the vertical
// (default) or horizontal direction.
Horizontal bool
}
// NewQuartPlot returns a new QuartPlot that represents
// the distribution of the given values.
//
// An error is returned if the plot is created with
// no values.
//
// The fence values are 1.5x the interquartile before
// the first quartile and after the third quartile. Any
// value that is outside of the fences are drawn as
// Outside points. The adjacent values (to which the
// whiskers stretch) are the minimum and maximum
// values that are not outside the fences.
func NewQuartPlot(loc float64, values Valuer) (*QuartPlot, error) {
b := new(QuartPlot)
var err error
if b.fiveStatPlot, err = newFiveStat(0, loc, values); err != nil {
return nil, err
}
b.MedianStyle = DefaultQuartMedianStyle
b.WhiskerStyle = DefaultQuartWhiskerStyle
return b, err
}
// Plot draws the QuartPlot on Canvas c and Plot plt.
func (b *QuartPlot) Plot(c draw.Canvas, plt *plot.Plot) {
if b.Horizontal {
b := &horizQuartPlot{b}
b.Plot(c, plt)
return
}
trX, trY := plt.Transforms(&c)
x := trX(b.Location)
if !c.ContainsX(x) {
return
}
x += b.Offset
med := vg.Point{X: x, Y: trY(b.Median)}
q1 := trY(b.Quartile1)
q3 := trY(b.Quartile3)
aLow := trY(b.AdjLow)
aHigh := trY(b.AdjHigh)
c.StrokeLine2(b.WhiskerStyle, x, aHigh, x, q3)
if c.ContainsY(med.Y) {
c.DrawGlyphNoClip(b.MedianStyle, med)
}
c.StrokeLine2(b.WhiskerStyle, x, aLow, x, q1)
ostyle := b.MedianStyle
ostyle.Radius = b.MedianStyle.Radius / 2
for _, out := range b.Outside {
y := trY(b.Value(out))
if c.ContainsY(y) {
c.DrawGlyphNoClip(ostyle, vg.Point{X: x, Y: y})
}
}
}
// DataRange returns the minimum and maximum x
// and y values, implementing the plot.DataRanger
// interface.
func (b *QuartPlot) DataRange() (float64, float64, float64, float64) {
if b.Horizontal {
b := &horizQuartPlot{b}
return b.DataRange()
}
return b.Location, b.Location, b.Min, b.Max
}
// GlyphBoxes returns a slice of GlyphBoxes for the plot,
// implementing the plot.GlyphBoxer interface.
func (b *QuartPlot) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
if b.Horizontal {
b := &horizQuartPlot{b}
return b.GlyphBoxes(plt)
}
bs := make([]plot.GlyphBox, len(b.Outside)+1)
ostyle := b.MedianStyle
ostyle.Radius = b.MedianStyle.Radius / 2
for i, out := range b.Outside {
bs[i].X = plt.X.Norm(b.Location)
bs[i].Y = plt.Y.Norm(b.Value(out))
bs[i].Rectangle = ostyle.Rectangle()
bs[i].Rectangle.Min.X += b.Offset
}
bs[len(bs)-1].X = plt.X.Norm(b.Location)
bs[len(bs)-1].Y = plt.Y.Norm(b.Median)
bs[len(bs)-1].Rectangle = b.MedianStyle.Rectangle()
bs[len(bs)-1].Rectangle.Min.X += b.Offset
return bs
}
// OutsideLabels returns a *Labels that will plot
// a label for each of the outside points. The
// labels are assumed to correspond to the
// points used to create the plot.
func (b *QuartPlot) OutsideLabels(labels Labeller) (*Labels, error) {
if b.Horizontal {
b := &horizQuartPlot{b}
return b.OutsideLabels(labels)
}
strs := make([]string, len(b.Outside))
for i, out := range b.Outside {
strs[i] = labels.Label(out)
}
o := quartPlotOutsideLabels{b, strs}
ls, err := NewLabels(o)
if err != nil {
return nil, err
}
ls.XOffset += b.MedianStyle.Radius / 2
ls.YOffset += b.MedianStyle.Radius / 2
return ls, nil
}
type quartPlotOutsideLabels struct {
qp *QuartPlot
labels []string
}
func (o quartPlotOutsideLabels) Len() int {
return len(o.qp.Outside)
}
func (o quartPlotOutsideLabels) XY(i int) (float64, float64) {
return o.qp.Location, o.qp.Value(o.qp.Outside[i])
}
func (o quartPlotOutsideLabels) Label(i int) string {
return o.labels[i]
}
// horizQuartPlot is like a regular QuartPlot, however,
// it draws horizontally instead of Vertically.
type horizQuartPlot struct{ *QuartPlot }
func (b horizQuartPlot) Plot(c draw.Canvas, plt *plot.Plot) {
trX, trY := plt.Transforms(&c)
y := trY(b.Location)
if !c.ContainsY(y) {
return
}
y += b.Offset
med := vg.Point{X: trX(b.Median), Y: y}
q1 := trX(b.Quartile1)
q3 := trX(b.Quartile3)
aLow := trX(b.AdjLow)
aHigh := trX(b.AdjHigh)
c.StrokeLine2(b.WhiskerStyle, aHigh, y, q3, y)
if c.ContainsX(med.X) {
c.DrawGlyphNoClip(b.MedianStyle, med)
}
c.StrokeLine2(b.WhiskerStyle, aLow, y, q1, y)
ostyle := b.MedianStyle
ostyle.Radius = b.MedianStyle.Radius / 2
for _, out := range b.Outside {
x := trX(b.Value(out))
if c.ContainsX(x) {
c.DrawGlyphNoClip(ostyle, vg.Point{X: x, Y: y})
}
}
}
// DataRange returns the minimum and maximum x
// and y values, implementing the plot.DataRanger
// interface.
func (b horizQuartPlot) DataRange() (float64, float64, float64, float64) {
return b.Min, b.Max, b.Location, b.Location
}
// GlyphBoxes returns a slice of GlyphBoxes for the plot,
// implementing the plot.GlyphBoxer interface.
func (b horizQuartPlot) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
bs := make([]plot.GlyphBox, len(b.Outside)+1)
ostyle := b.MedianStyle
ostyle.Radius = b.MedianStyle.Radius / 2
for i, out := range b.Outside {
bs[i].X = plt.X.Norm(b.Value(out))
bs[i].Y = plt.Y.Norm(b.Location)
bs[i].Rectangle = ostyle.Rectangle()
bs[i].Rectangle.Min.Y += b.Offset
}
bs[len(bs)-1].X = plt.X.Norm(b.Median)
bs[len(bs)-1].Y = plt.Y.Norm(b.Location)
bs[len(bs)-1].Rectangle = b.MedianStyle.Rectangle()
bs[len(bs)-1].Rectangle.Min.Y += b.Offset
return bs
}
// OutsideLabels returns a *Labels that will plot
// a label for each of the outside points. The
// labels are assumed to correspond to the
// points used to create the plot.
func (b *horizQuartPlot) OutsideLabels(labels Labeller) (*Labels, error) {
strs := make([]string, len(b.Outside))
for i, out := range b.Outside {
strs[i] = labels.Label(out)
}
o := horizQuartPlotOutsideLabels{
quartPlotOutsideLabels{b.QuartPlot, strs},
}
ls, err := NewLabels(o)
if err != nil {
return nil, err
}
ls.XOffset += b.MedianStyle.Radius / 2
ls.YOffset += b.MedianStyle.Radius / 2
return ls, nil
}
type horizQuartPlotOutsideLabels struct {
quartPlotOutsideLabels
}
func (o horizQuartPlotOutsideLabels) XY(i int) (float64, float64) {
return o.qp.Value(o.qp.Outside[i]), o.qp.Location
}