-
Notifications
You must be signed in to change notification settings - Fork 1
/
horizontal_bar_chart.go
171 lines (159 loc) · 4.32 KB
/
horizontal_bar_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
package charts
import (
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
)
type horizontalBarChart struct {
p *Painter
opt *HorizontalBarChartOption
}
type HorizontalBarChartOption struct {
// Theme specifies the colors used for the chart.
Theme ColorPalette
// Padding specifies the padding of bar chart.
Padding Box
// Font is the font used to render the chart.
Font *truetype.Font
// SeriesList provides the data series.
SeriesList SeriesList
// XAxis are options for the x-axis.
XAxis XAxisOption
// YAxis are options for the y-axis (at most two).
YAxis []YAxisOption
// Title are options for rendering the title.
Title TitleOption
// Legend are options for the data legend.
Legend LegendOption
// BarHeight specifies the height of each horizontal bar.
BarHeight int
}
// NewHorizontalBarChart returns a horizontal bar chart renderer
func NewHorizontalBarChart(p *Painter, opt HorizontalBarChartOption) *horizontalBarChart {
return &horizontalBarChart{
p: p,
opt: &opt,
}
}
func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
p := h.p
opt := h.opt
seriesPainter := result.seriesPainter
yRange := result.axisRanges[0]
y0, y1 := yRange.GetRange(0)
height := int(y1 - y0)
// margin between each block
margin := 10
// margin between each bar
barMargin := 5
if height < 20 {
margin = 2
barMargin = 2
} else if height < 50 {
margin = 5
barMargin = 3
}
seriesCount := len(seriesList)
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount
if opt.BarHeight > 0 && opt.BarHeight < barHeight {
barHeight = opt.BarHeight
margin = (height - seriesCount*barHeight - barMargin*(seriesCount-1)) / 2
}
theme := opt.Theme
min, max := seriesList.GetMinMax(0)
xRange := NewRange(p, seriesPainter.Width(), len(seriesList[0].Data), min, max, 1.0, 1.0)
seriesNames := seriesList.Names()
rendererList := []Renderer{}
for index := range seriesList {
series := seriesList[index]
seriesColor := theme.GetSeriesColor(series.index)
divideValues := yRange.AutoDivide()
var labelPainter *SeriesLabelPainter
if series.Label.Show {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
rendererList = append(rendererList, labelPainter)
}
for j, item := range series.Data {
if j >= yRange.divideCount {
continue
}
// display position switch
j = yRange.divideCount - j - 1
y := divideValues[j]
y += margin
if index != 0 {
y += index * (barHeight + barMargin)
}
w := xRange.getHeight(item.Value)
fillColor := seriesColor
if !item.Style.FillColor.IsZero() {
fillColor = item.Style.FillColor
}
right := w
seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
}).Rect(chart.Box{
Top: y,
Left: 0,
Right: right,
Bottom: y + barHeight,
})
// if the label does not need to be displayed, return
if labelPainter == nil {
continue
}
labelValue := LabelValue{
Orient: OrientHorizontal,
Index: index,
Value: item.Value,
X: right,
Y: y + barHeight>>1,
Offset: series.Label.Offset,
FontColor: series.Label.Color,
FontSize: series.Label.FontSize,
}
if series.Label.Position == PositionLeft {
labelValue.X = 0
if labelValue.FontColor.IsZero() {
if isLightColor(fillColor) {
labelValue.FontColor = defaultLightFontColor
} else {
labelValue.FontColor = defaultDarkFontColor
}
}
}
labelPainter.Add(labelValue)
}
}
if err := doRender(rendererList...); err != nil {
return BoxZero, err
}
return p.box, nil
}
func (h *horizontalBarChart) Render() (Box, error) {
p := h.p
opt := h.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: opt.XAxis,
YAxis: opt.YAxis,
Title: opt.Title,
Legend: opt.Legend,
axisReversed: true,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeHorizontalBar)
return h.render(renderResult, seriesList)
}