-
Notifications
You must be signed in to change notification settings - Fork 2
/
density.go
126 lines (105 loc) · 2.35 KB
/
density.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
package plot
import (
"math"
"sort"
)
// Density implements density plot using cubic-pulse kernel.
type Density struct {
Style
Label string
Kernel Length
Normalized bool
Data []float64 // sorted
}
// NewDensity creates a density plot from the given values.
func NewDensity(label string, values []float64) *Density {
data := append(values[:0:0], values...)
sort.Float64s(data)
return &Density{
Kernel: math.NaN(),
Label: label,
Normalized: true,
Data: data,
}
}
// Stats calculates statistics of values.
func (line *Density) Stats() Stats {
min, avg, max := math.NaN(), math.NaN(), math.NaN()
n := len(line.Data)
if n > 0 {
min = line.Data[0]
avg = line.Data[n/2]
max = line.Data[n-1]
}
return Stats{
Min: Point{min, 0},
Center: Point{avg, 0.5},
Max: Point{max, 1},
}
}
// Draw draws the element to canvas.
func (line *Density) Draw(plot *Plot, canvas Canvas) {
x, y := plot.X, plot.Y
size := canvas.Bounds().Size()
xmin, xmax := x.ToCanvas(x.Min, 0, size.X), x.ToCanvas(x.Max, 0, size.X)
if xmin > xmax {
xmin, xmax = xmax, xmin
}
kernel := line.Kernel
if math.IsNaN(kernel) {
// default to 4px wide kernel
kernel = 4 * (x.Max - x.Min) / size.X
}
invkernel := 1 / kernel
points := []Point{}
if line.Fill != nil {
points = append(points, Point{xmin, 0})
}
index := 0
previousLow := math.Inf(-1)
maxy := 0.0
for screenX := 0.0; screenX < size.X; screenX += 0.5 {
center := x.FromCanvas(screenX, 0, size.X)
low, high := center-kernel, center+kernel
if low < previousLow {
index = sort.SearchFloat64s(line.Data, low)
} else {
for ; index < len(line.Data); index++ {
if line.Data[index] >= low {
break
}
}
}
previousLow = low
sample := 0.0
for _, value := range line.Data[index:] {
if value > high {
break
}
sample += cubicPulse(center, kernel, invkernel, value)
}
maxy = math.Max(maxy, sample)
points = append(points, Point{
X: screenX,
Y: sample,
})
}
if line.Fill != nil {
points = append(points,
Point{xmax, 0},
Point{xmin, 0},
)
}
scale := kernel / float64(len(line.Data))
if line.Normalized {
scale = 1 / maxy
}
for i := range points {
points[i].Y = y.ToCanvas(points[i].Y*scale, 0, size.Y)
}
if !line.Style.IsZero() {
canvas.Poly(points, &line.Style)
} else {
canvas.Poly(points, &plot.Theme.Line)
}
}