forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiechart.go
More file actions
290 lines (261 loc) · 6.52 KB
/
Copy pathpiechart.go
File metadata and controls
290 lines (261 loc) · 6.52 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
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
284
285
286
287
288
289
290
package termui
import (
"container/list"
"image"
"math"
)
const (
piechartOffsetUp = -.5 * math.Pi // the northward angle
sectorFactor = 7.0 // circle resolution: precision vs. performance
fullCircle = 2.0 * math.Pi // the full circle angle
xStretch = 2.0 // horizontal adjustment
solidBlock = '░'
)
var (
defaultColors = []Attribute{
ColorRed,
ColorGreen,
ColorYellow,
ColorBlue,
ColorMagenta,
ColorCyan,
ColorWhite,
}
)
// PieChartLabel callback, i is the current data index, v the current value
type PieChartLabel func(i int, v float64) string
type PieChart struct {
Block
Data []float64 // list of data items
Colors []Attribute // colors to by cycled through (see defaultColors)
BorderColor Attribute // color of the pie-border
Label PieChartLabel // callback function for labels
Offset float64 // which angle to start drawing at? (see piechartOffsetUp)
}
// NewPieChart Creates a new pie chart with reasonable defaults and no labels
func NewPieChart() *PieChart {
return &PieChart{
Block: *NewBlock(),
Colors: defaultColors,
Offset: piechartOffsetUp,
BorderColor: ColorDefault,
}
}
// computes the color for a given data index
func (pc *PieChart) colorFor(i int) Attribute {
return pc.Colors[i%len(pc.Colors)]
}
// Buffer creates the buffer for the pie chart
func (pc *PieChart) Buffer() Buffer {
buf := pc.Block.Buffer()
w, h := pc.innerArea.Dx(), pc.innerArea.Dy()
center := image.Point{X: w / 2, Y: h / 2}
// radius for the border
r := float64(w/2/xStretch) - 1.0
if h < w/xStretch {
r = float64(h/2) - 1.0
}
// make border
borderCircle := &circle{Point: center, radius: r}
drawBorder := func() {
borderCircle.draw(&buf, Cell{Ch: solidBlock, Fg: pc.BorderColor, Bg: ColorDefault})
}
drawBorder()
if len(pc.Data) == 0 { // nothing to draw?
return buf
}
// compute slice sizes
sum := sum(pc.Data)
sliceSizes := make([]float64, len(pc.Data))
for i, v := range pc.Data {
sliceSizes[i] = v / sum * fullCircle
}
// draw slice borders
phi := pc.Offset
for i, v := range sliceSizes {
p := borderCircle.at(phi)
l := line{P1: center, P2: p}
l.draw(&buf, &Cell{Ch: solidBlock, Fg: pc.colorFor(i), Bg: ColorDefault})
phi += v
}
// fill slices
middleCircle := circle{Point: center, radius: r / 2.0}
_, sectorSize := borderCircle.sectors()
phi = pc.Offset
for i, v := range sliceSizes {
if v > sectorSize { // do not render if slice is too small
cell := Cell{Ch: solidBlock, Fg: pc.colorFor(i), Bg: ColorDefault}
halfSlice := phi + v/2.0
fill(borderCircle.inner(halfSlice), &cell, &buf)
fill(middleCircle.at(halfSlice), &cell, &buf)
for f := phi; f < phi+v; f += sectorSize {
line{P1: center, P2: borderCircle.inner(f)}.draw(&buf, &cell)
}
}
phi += v
}
// labels
if pc.Label != nil {
drawLabel := func(p image.Point, label string, fg, bg Attribute) {
offset := p.Add(image.Point{X: -int(round(float64(len(label)) / 2.0)), Y: 0})
for i, v := range []rune(label) {
buf.Set(offset.X+i, offset.Y, Cell{Ch: v, Fg: fg, Bg: bg})
}
}
phi = pc.Offset
for i, v := range sliceSizes {
labelAt := middleCircle.at(phi + v/2.0)
if len(pc.Data) == 1 {
labelAt = center
}
drawLabel(labelAt, pc.Label(i, pc.Data[i]), pc.colorFor(i), ColorDefault)
phi += v
}
}
drawBorder()
return buf
}
// fills empty cells from position
func fill(p image.Point, c *Cell, buf *Buffer) {
empty := func(x, y int) bool {
return buf.At(x, y).Ch == ' '
}
if !empty(p.X, p.Y) {
return
}
q := list.New()
q.PushBack(p)
buf.Set(p.X, p.Y, *c)
for q.Front() != nil {
p := q.Remove(q.Front()).(image.Point)
w, e, row := p.X, p.X, p.Y
for empty(w-1, row) {
w--
}
for empty(e+1, row) {
e++
}
for x := w; x <= e; x++ {
buf.Set(x, row, *c)
if empty(x, row-1) {
q.PushBack(image.Point{X: x, Y: row - 1})
}
if empty(x, row+1) {
q.PushBack(image.Point{X: x, Y: row + 1})
}
}
}
}
type circle struct {
image.Point
radius float64
}
// computes the point at a given angle phi
func (c circle) at(phi float64) image.Point {
x := c.X + int(round(xStretch*c.radius*math.Cos(phi)))
y := c.Y + int(round(c.radius*math.Sin(phi)))
return image.Point{X: x, Y: y}
}
// computes the "inner" point at a given angle phi
func (c circle) inner(phi float64) image.Point {
p := image.Point{X: 0, Y: 0}
outer := c.at(phi)
if c.X < outer.X {
p.X = -1
} else if c.X > outer.X {
p.X = 1
}
if c.Y < outer.Y {
p.Y = -1
} else if c.Y > outer.Y {
p.Y = 1
}
return outer.Add(p)
}
// computes the perimeter of a circle
func (c circle) perimeter() float64 {
return 2.0 * math.Pi * c.radius
}
// computes the number of sectors and the size of each sector
func (c circle) sectors() (sectors float64, sectorSize float64) {
sectors = c.perimeter() * sectorFactor
sectorSize = fullCircle / sectors
return
}
// draws the circle
func (c circle) draw(buf *Buffer, cell Cell) {
sectors, sectorSize := c.sectors()
for i := 0; i < int(round(sectors)); i++ {
phi := float64(i) * sectorSize
point := c.at(float64(phi))
buf.Set(point.X, point.Y, cell)
}
}
// a line between two points
type line struct {
P1, P2 image.Point
}
// draws the line
func (l line) draw(buf *Buffer, cell *Cell) {
isLeftOf := func(p1, p2 image.Point) bool {
return p1.X <= p2.X
}
isTopOf := func(p1, p2 image.Point) bool {
return p1.Y <= p2.Y
}
p1, p2 := l.P1, l.P2
buf.Set(l.P2.X, l.P2.Y, Cell{Ch: '*', Fg: cell.Fg, Bg: cell.Bg})
width, height := l.size()
if width > height { // paint left to right
if !isLeftOf(p1, p2) {
p1, p2 = p2, p1
}
flip := 1.0
if !isTopOf(p1, p2) {
flip = -1.0
}
for x := p1.X; x <= p2.X; x++ {
ratio := float64(height) / float64(width)
factor := float64(x - p1.X)
y := ratio * factor * flip
buf.Set(x, int(round(y))+p1.Y, *cell)
}
} else { // paint top to bottom
if !isTopOf(p1, p2) {
p1, p2 = p2, p1
}
flip := 1.0
if !isLeftOf(p1, p2) {
flip = -1.0
}
for y := p1.Y; y <= p2.Y; y++ {
ratio := float64(width) / float64(height)
factor := float64(y - p1.Y)
x := ratio * factor * flip
buf.Set(int(round(x))+p1.X, y, *cell)
}
}
}
// width and height of a line
func (l line) size() (w, h int) {
return abs(l.P2.X - l.P1.X), abs(l.P2.Y - l.P1.Y)
}
// rounds a value
func round(x float64) float64 {
return math.Floor(x + 0.5)
}
// fold a sum
func sum(data []float64) float64 {
sum := 0.0
for _, v := range data {
sum += v
}
return sum
}
// math.Abs for ints
func abs(x int) int {
if x >= 0 {
return x
}
return -x
}