-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradar_chart.go
242 lines (223 loc) · 6.13 KB
/
radar_chart.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
package charts
import (
"errors"
"github.com/dustin/go-humanize"
"github.com/golang/freetype/truetype"
"github.com/go-analyze/charts/chartdraw"
)
type radarChart struct {
p *Painter
opt *RadarChartOption
}
type RadarIndicator struct {
// Name specifies a name for the iIndicator.
Name string
// Max is the maximum value of indicator.
Max float64
// Min is the minimum value of indicator.
Min float64
}
// NewRadarChartOptionWithData returns an initialized RadarChartOption with the SeriesList set for the provided data slice.
func NewRadarChartOptionWithData(data [][]float64, names []string, values []float64) RadarChartOption {
return RadarChartOption{
SeriesList: NewSeriesListRadar(data),
RadarIndicators: NewRadarIndicators(names, values),
Padding: defaultPadding,
Theme: GetDefaultTheme(),
Font: GetDefaultFont(),
}
}
type RadarChartOption struct {
// Theme specifies the colors used for the pie chart.
Theme ColorPalette
// Padding specifies the padding of pie chart.
Padding Box
// Font is the font used to render the chart.
Font *truetype.Font
// SeriesList provides the data series.
SeriesList SeriesList
// Title are options for rendering the title.
Title TitleOption
// Legend are options for the data legend.
Legend LegendOption
// RadarIndicators provides the radar indicator list.
RadarIndicators []RadarIndicator
// backgroundIsFilled is set to true if the background is filled.
backgroundIsFilled bool
}
// NewRadarIndicators returns a radar indicator list
func NewRadarIndicators(names []string, values []float64) []RadarIndicator {
if len(names) != len(values) {
return nil
}
indicators := make([]RadarIndicator, len(names))
for index, name := range names {
indicators[index] = RadarIndicator{
Name: name,
Max: values[index],
}
}
return indicators
}
// newRadarChart returns a radar chart renderer.
func newRadarChart(p *Painter, opt RadarChartOption) *radarChart {
return &radarChart{
p: p,
opt: &opt,
}
}
func (r *radarChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
opt := r.opt
indicators := opt.RadarIndicators
sides := len(indicators)
if sides < 3 {
return BoxZero, errors.New("the count of indicator should be >= 3")
}
maxValues := make([]float64, len(indicators))
for _, series := range seriesList {
for index, item := range series.Data {
if index < len(maxValues) && item > maxValues[index] {
maxValues[index] = item
}
}
}
for index, indicator := range indicators {
if indicator.Max <= 0 {
indicators[index].Max = maxValues[index]
}
}
radiusValue := ""
for _, series := range seriesList {
if len(series.Radius) != 0 {
radiusValue = series.Radius
}
}
seriesPainter := result.seriesPainter
theme := opt.Theme
cx := seriesPainter.Width() >> 1
cy := seriesPainter.Height() >> 1
diameter := chartdraw.MinInt(seriesPainter.Width(), seriesPainter.Height())
radius := getRadius(float64(diameter), radiusValue)
divideCount := 5
divideRadius := float64(int(radius / float64(divideCount)))
radius = divideRadius * float64(divideCount)
center := Point{X: cx, Y: cy}
for i := 0; i < divideCount; i++ {
seriesPainter.Polygon(center, divideRadius*float64(i+1), sides, theme.GetAxisSplitLineColor(), 1)
}
points := getPolygonPoints(center, radius, sides)
for _, p := range points {
seriesPainter.moveTo(center.X, center.Y)
seriesPainter.lineTo(p.X, p.Y)
seriesPainter.stroke(theme.GetAxisSplitLineColor(), 1)
}
fontStyle := FontStyle{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
}
offset := 5
// text generation
for index, p := range points {
name := indicators[index].Name
b := seriesPainter.MeasureText(name, 0, fontStyle)
isXCenter := p.X == center.X
isYCenter := p.Y == center.Y
isRight := p.X > center.X
isLeft := p.X < center.X
isTop := p.Y < center.Y
isBottom := p.Y > center.Y
x := p.X
y := p.Y
if isXCenter {
x -= b.Width() >> 1
if isTop {
y -= b.Height()
} else {
y += b.Height()
}
}
if isYCenter {
y += b.Height() >> 1
}
if isTop {
y += offset
}
if isBottom {
y += offset
}
if isRight {
x += offset
}
if isLeft {
x -= b.Width() + offset
}
seriesPainter.Text(name, x, y, 0, fontStyle)
}
// radar chart
angles := getPolygonPointAngles(sides)
maxCount := len(indicators)
for _, series := range seriesList {
linePoints := make([]Point, 0, maxCount)
for j, item := range series.Data {
if j >= maxCount {
continue
}
indicator := indicators[j]
var percent float64
offset := indicator.Max - indicator.Min
if offset > 0 {
percent = (item - indicator.Min) / offset
}
r := percent * radius
p := getPolygonPoint(center, r, angles[j])
linePoints = append(linePoints, p)
}
color := theme.GetSeriesColor(series.index)
dotFillColor := ColorWhite
if theme.IsDark() {
dotFillColor = color
}
linePoints = append(linePoints, linePoints[0])
seriesPainter.LineStroke(linePoints, color, defaultStrokeWidth)
seriesPainter.FillArea(linePoints, color.WithAlpha(20))
dotWith := defaultDotWidth
for index, point := range linePoints {
seriesPainter.Circle(dotWith, point.X, point.Y, dotFillColor, color, defaultStrokeWidth)
if flagIs(true, series.Label.Show) && index < len(series.Data) {
value := humanize.FtoaWithDigits(series.Data[index], 2)
b := seriesPainter.MeasureText(value, 0, fontStyle)
seriesPainter.Text(value, point.X-b.Width()/2, point.Y, 0, fontStyle)
}
}
}
return r.p.box, nil
}
func (r *radarChart) Render() (Box, error) {
p := r.p
opt := r.opt
if opt.Theme == nil {
opt.Theme = getPreferredTheme(p.theme)
}
renderResult, err := defaultRender(p, defaultRenderOption{
theme: opt.Theme,
padding: opt.Padding,
seriesList: opt.SeriesList,
xAxis: &XAxisOption{
Show: False(),
},
yAxis: []YAxisOption{
{
Show: False(),
},
},
title: opt.Title,
legend: &r.opt.Legend,
backgroundIsFilled: opt.backgroundIsFilled,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeRadar)
return r.render(renderResult, seriesList)
}