-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathfunnel_chart.go
192 lines (174 loc) · 4.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
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
// MIT License
// Copyright (c) 2022 Tree Xie
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package charts
import (
"github.com/golang/freetype/truetype"
)
type funnelChart struct {
p *Painter
opt *FunnelChartOption
}
// NewFunnelSeriesList returns a series list for funnel
func NewFunnelSeriesList(values []float64) SeriesList {
seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues([]float64{
value,
}, ChartTypeFunnel)
}
return seriesList
}
// NewFunnelChart returns a funnel chart renderer
func NewFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &funnelChart{
p: p,
opt: &opt,
}
}
type FunnelChartOption struct {
// The theme
Theme ColorPalette
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList
// The padding of line chart
Padding Box
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
}
func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
opt := f.opt
seriesPainter := result.seriesPainter
max := seriesList[0].Data[0].Value
min := float64(0)
for _, item := range seriesList {
if item.Max != nil {
max = *item.Max
}
if item.Min != nil {
min = *item.Min
}
}
theme := opt.Theme
gap := 2
height := seriesPainter.Height()
width := seriesPainter.Width()
count := len(seriesList)
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].Value
// 最大最小值一致则为100%
widthPercent := 100.0
if offset != 0 {
widthPercent = (value - min) / offset
}
w := int(widthPercent * float64(width))
widthList[index] = w
// 如果最大值为0,则占比100%
percent := 1.0
if max != 0 {
percent = value / max
}
textList[index] = NewFunnelLabelFormatter(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,
},
}
color := theme.GetSeriesColor(series.index)
seriesPainter.OverrideDrawingStyle(Style{
FillColor: color,
}).FillArea(points)
// 文本
text := textList[index]
seriesPainter.OverrideTextStyle(Style{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
})
textBox := seriesPainter.MeasureText(text)
textX := width>>1 - textBox.Width()>>1
textY := y + h>>1
seriesPainter.Text(text, textX, textY)
y += (h + gap)
}
return f.p.box, nil
}
func (f *funnelChart) Render() (Box, error) {
p := f.p
opt := f.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: XAxisOption{
Show: FalseFlag(),
},
YAxisOptions: []YAxisOption{
{
Show: FalseFlag(),
},
},
TitleOption: opt.Title,
LegendOption: opt.Legend,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeFunnel)
return f.render(renderResult, seriesList)
}