-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabels.go
114 lines (95 loc) · 3.05 KB
/
labels.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
package gopie
import (
"math"
)
func createLabels(chart PieChart, pieRect rectangle) []label {
if len(chart.Values) == 0 {
return createEmptyLabels()
}
if len(chart.Values) == 1 {
return createSingleLabel(chart, pieRect)
}
return createMultipleLabels(chart, pieRect)
}
func createEmptyLabels() []label {
return make([]label, 0)
}
func createMultipleLabels(chart PieChart, pieRect rectangle) []label {
labels := make([]label, len(chart.Values))
sum := float64(0)
total := chart.calculateTotalValue()
labelLineInnerRadius := calculateLabelLineInnerRadius(chart, pieRect)
labelLineOuterRadius := calculateLabelLineOuterRadius(chart, pieRect)
textRadius := calculateTextRadius(chart, pieRect)
for index, value := range chart.Values {
labelLineAngle := ((sum + value.Value/2) / total) * twoPi
labels[index] = label{
Text: createText(index, textRadius, labelLineAngle, chart),
Line: createLine(index, labelLineInnerRadius, labelLineOuterRadius, labelLineAngle, chart),
}
sum += value.Value
}
return labels
}
func createSingleLabel(chart PieChart, pieRect rectangle) []label {
labelLineInnerRadius := calculateLabelLineInnerRadius(chart, pieRect)
labelLineOuterRadius := calculateLabelLineOuterRadius(chart, pieRect)
textRadius := calculateTextRadius(chart, pieRect)
labelLineAngle := math.Pi / 2
return []label{
label{
Text: createText(0, textRadius, labelLineAngle, chart),
Line: createLine(0, labelLineInnerRadius, labelLineOuterRadius, labelLineAngle, chart),
},
}
}
func createText(index int, textRadius, angle float64, chart PieChart) text {
centerX, centerY := chart.getCenter()
textX, textY := toDecartTranslate(angle, textRadius, centerX, centerY)
return text{
Text: chart.Values[index].Label,
FontAnchor: calculateLabelTextAnchor(angle),
FontFamily: chart.getFontFamily(),
FontSize: chart.getFontSize(),
X: textX,
Y: textY,
}
}
func calculateLabelTextAnchor(angle float64) string {
mod := math.Mod(angle, math.Pi*2)
switch {
case mod == 0:
return "middle"
case mod == math.Pi:
return "middle"
case mod > math.Pi:
return "end"
default:
return "start"
}
}
func createLine(index int, innerRadius, outerRadius, angle float64, chart PieChart) line {
centerX, centerY := chart.getCenter()
startX, startY := toDecartTranslate(angle, innerRadius, centerX, centerY)
endX, endY := toDecartTranslate(angle, outerRadius, centerX, centerY)
return line{
Style: createLabelLineStyle(chart, index),
X1: startX,
Y1: startY,
X2: endX,
Y2: endY,
}
}
func calculateLabelLineInnerRadius(c PieChart, r rectangle) float64 {
outerRadius := r.calculateIncircleRadius()
strokeWidth := c.getStrokeWidth()
return outerRadius - strokeWidth
}
func calculateLabelLineOuterRadius(c PieChart, r rectangle) float64 {
innerRadius := calculateLabelLineInnerRadius(c, r)
return innerRadius + c.getLabelLineFullLength()
}
func calculateTextRadius(c PieChart, r rectangle) float64 {
labelLineOuterRadius := calculateLabelLineOuterRadius(c, r)
return labelLineOuterRadius + c.getLabelPadding()
}