-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunnel_chart.go
163 lines (148 loc) · 3.63 KB
/
funnel_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
package charts
import (
"errors"
"github.com/golang/freetype/truetype"
)
type funnelChart struct {
p *Painter
opt *FunnelChartOption
}
// newFunnelChart returns a funnel chart renderer
func newFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart {
return &funnelChart{
p: p,
opt: &opt,
}
}
// NewFunnelChartOptionWithData returns an initialized FunnelChartOption with the SeriesList set for the provided data slice.
func NewFunnelChartOptionWithData(data []float64) FunnelChartOption {
return FunnelChartOption{
SeriesList: NewSeriesListFunnel(data),
Padding: defaultPadding,
Theme: GetDefaultTheme(),
Font: GetDefaultFont(),
}
}
type FunnelChartOption struct {
// Theme specifies the colors used for the chart.
Theme ColorPalette
// Padding specifies the padding of funnel 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
}
func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
opt := f.opt
seriesPainter := result.seriesPainter
max := seriesList[0].Data[0]
min := float64(0)
theme := opt.Theme
gap := 2
height := seriesPainter.Height()
width := seriesPainter.Width()
count := len(seriesList)
if count == 0 {
return BoxZero, errors.New("empty series list")
}
h := (height - gap*(count-1)) / count
y := 0
widthList := make([]int, len(seriesList))
textList := make([]string, len(seriesList))
seriesNames := seriesList.Names()
offset := max - min
for index, item := range seriesList {
value := item.Data[0]
// if the maximum and minimum are consistent it's 100%
widthPercent := 100.0
if offset != 0 {
widthPercent = (value - min) / offset
}
w := int(widthPercent * float64(width))
widthList[index] = w
// if the maximum value is 0, the proportion is 100%
percent := 1.0
if max != 0 {
percent = value / max
}
textList[index] = labelFormatFunnel(seriesNames, item.Label.Formatter, index, value, percent)
}
for index, w := range widthList {
series := seriesList[index]
nextWidth := 0
if index+1 < len(widthList) {
nextWidth = widthList[index+1]
}
topStartX := (width - w) >> 1
topEndX := topStartX + w
bottomStartX := (width - nextWidth) >> 1
bottomEndX := bottomStartX + nextWidth
points := []Point{
{
X: topStartX,
Y: y,
},
{
X: topEndX,
Y: y,
},
{
X: bottomEndX,
Y: y + h,
},
{
X: bottomStartX,
Y: y + h,
},
{
X: topStartX,
Y: y,
},
}
seriesPainter.FillArea(points, theme.GetSeriesColor(series.index))
text := textList[index]
fontStyle := FontStyle{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
}
textBox := seriesPainter.MeasureText(text, 0, fontStyle)
textX := width>>1 - textBox.Width()>>1
textY := y + h>>1
seriesPainter.Text(text, textX, textY, 0, fontStyle)
y += h + gap
}
return f.p.box, nil
}
func (f *funnelChart) Render() (Box, error) {
p := f.p
opt := f.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: &f.opt.Legend,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeFunnel)
return f.render(renderResult, seriesList)
}