-
Notifications
You must be signed in to change notification settings - Fork 0
/
piechart.go
152 lines (137 loc) · 3.46 KB
/
piechart.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
/* Cf. https://marian-caikovski.medium.com/drawing-sectors-and-pie-charts-with-svg-paths-b99b5b6bf7bd */
package charts
import (
"fmt"
"io"
"math"
"sort"
)
type PieChart struct {
Dimension
series []string
data []float64
numberFormat string
colorScheme *ColorScheme
showValues bool
isInteractive bool
}
func NewPieChart(
width int,
height int,
series []string,
data []float64,
) *PieChart {
return &PieChart{
Dimension: Dimension{
width: width,
height: height,
},
colorScheme: &DefaultColorScheme,
series: series,
data: data,
showValues: false,
isInteractive: false,
}
}
func (pc *PieChart) SetColorDcheme(colorScheme *ColorScheme) *PieChart {
pc.colorScheme = colorScheme
return pc
}
func (pc *PieChart) SetNumberFormat(numberFormat string) *PieChart {
pc.numberFormat = numberFormat
return pc
}
func (pc *PieChart) SetInteractive(interactive bool) *PieChart {
pc.isInteractive = interactive
return pc
}
func (pc *PieChart) SetShowValue(showValues bool) *PieChart {
pc.showValues = showValues
return pc
}
func (pc *PieChart) RenderSVG(w io.Writer) error {
startSVG(w, pc.width, pc.height, pc.colorScheme)
writeFontStyle(w, pc.isInteractive)
writeBackground(w, pc.width, pc.height, pc.colorScheme)
type pieSlice struct {
value float64
label string
startX, startY float64
endX, endY float64
large int
labelX, labelY float64
}
pieSlices := make([]pieSlice, len(pc.data))
total := 0.0
for i, v := range pc.data {
pieSlices[i] = pieSlice{
value: v,
label: pc.series[i],
}
total += v
}
sort.SliceStable(pieSlices, func(i, j int) bool {
return pieSlices[i].value > pieSlices[j].value
})
sortSeries := make([]string, len(pieSlices))
for i, v := range pieSlices {
sortSeries[i] = v.label
}
// series
legendfHeight := writeBarSeriesLegend(w, pc.width, sortSeries, pc.colorScheme)
centerX := float64(pc.width / 2)
centerY := float64(pc.height-legendfHeight)/2.0 + float64(legendfHeight)
var radius float64
if pc.width > (pc.height - legendfHeight) {
radius = float64(pc.height-legendfHeight) / 2.0
} else {
radius = float64(pc.width) / 2.0
}
curAlpha := math.Pi / 2
for i, v := range pieSlices {
percent := v.value / total
alpha := percent*math.Pi*2 + curAlpha
pieSlices[i].startX = math.Cos(curAlpha) * radius
pieSlices[i].startY = math.Sin(curAlpha) * radius
pieSlices[i].endX = math.Cos(alpha) * radius
pieSlices[i].endY = math.Sin(alpha) * radius
pieSlices[i].labelX = math.Cos((alpha+curAlpha)/2.0) * radius * 0.8
pieSlices[i].labelY = math.Sin((alpha+curAlpha)/2.0) * radius * 0.8
if alpha-curAlpha > math.Pi {
pieSlices[i].large = 1
}
curAlpha = alpha
}
// pie
for i, _ := range pieSlices {
fmt.Fprintf(
w,
"<path d='M %f %f A %f %f 0 %d 1 %f %f L %f %f L %f %f Z' fill='%s' stroke='%s' />",
centerX-pieSlices[i].startX,
centerY-pieSlices[i].startY,
radius, radius,
pieSlices[i].large,
centerX-pieSlices[i].endX,
centerY-pieSlices[i].endY,
centerX,
centerY,
centerX-pieSlices[i].startX,
centerY-pieSlices[i].startY,
pc.colorScheme.ColorPalette(i),
pc.colorScheme.Background,
)
}
if pc.showValues {
for i, _ := range pieSlices {
fmt.Fprintf(
w,
"<text x='%f' y='%f' text-anchor='middle' alignment-baseline='middle' fill='#fff'>%g</text>",
centerX-pieSlices[i].labelX,
centerY-pieSlices[i].labelY,
pieSlices[i].value,
)
}
}
endSVG(w)
return nil
}