-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunnel_chart.go
174 lines (159 loc) · 4.35 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
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(),
}
}
// FunnelChartOption defines the options for rendering a funnel chart. Render the chart using Painter.FunnelChart.
type FunnelChartOption struct {
// Theme specifies the colors used for the chart.
Theme ColorPalette
// Padding specifies the padding of funnel chart.
Padding Box
// Deprecated: Font is deprecated, instead the font needs to be set on the SeriesLabel, or other specific elements.
Font *truetype.Font
// SeriesList provides the data population for the chart, typically constructed using NewSeriesListFunnel.
SeriesList FunnelSeriesList
// Title are options for rendering the title.
Title TitleOption
// Legend are options for the data legend.
Legend LegendOption
// ValueFormatter defines how float values should be rendered to strings, notably for series labels.
ValueFormatter ValueFormatter
}
func (f *funnelChart) renderChart(result *defaultRenderResult) (Box, error) {
opt := f.opt
seriesCount := len(opt.SeriesList)
if seriesCount == 0 {
return BoxZero, errors.New("empty series list")
}
seriesPainter := result.seriesPainter
max := opt.SeriesList[0].Value
var min float64
theme := opt.Theme
gap := 2
height := seriesPainter.Height()
width := seriesPainter.Width()
h := (height - gap*(seriesCount-1)) / seriesCount
var y int
widthList := make([]int, seriesCount)
textList := make([]string, seriesCount)
seriesNames := opt.SeriesList.names()
offset := max - min
for index, item := range opt.SeriesList {
// if the maximum and minimum are consistent it's 100%
widthPercent := 100.0
if offset != 0 {
widthPercent = (item.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 = item.Value / max
}
if !flagIs(false, item.Label.Show) {
valueFormatter := item.Label.ValueFormatter
if valueFormatter == nil {
valueFormatter = opt.ValueFormatter
}
if valueFormatter != nil && item.Label.FormatTemplate == "" {
textList[index] = valueFormatter(item.Value)
} else {
textList[index] = labelFormatFunnel(seriesNames, item.Label.FormatTemplate, item.Label.ValueFormatter,
index, item.Value, percent)
}
}
}
for index, w := range widthList {
var nextWidth int
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(index))
text := textList[index]
fontStyle := fillFontStyleDefaults(opt.SeriesList[index].Label.FontStyle,
defaultLabelFontSize, theme.GetLabelTextColor(), 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)
}
if opt.Legend.Symbol == "" {
opt.Legend.Symbol = SymbolSquare
}
renderResult, err := defaultRender(p, defaultRenderOption{
theme: opt.Theme,
padding: opt.Padding,
seriesList: opt.SeriesList,
xAxis: &XAxisOption{
Show: Ptr(false),
},
yAxis: []YAxisOption{
{
Show: Ptr(false),
},
},
title: opt.Title,
legend: &f.opt.Legend,
})
if err != nil {
return BoxZero, err
}
return f.renderChart(renderResult)
}