-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhorizontal_bar_chart.go
207 lines (187 loc) · 6.54 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
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
package charts
import (
"errors"
"github.com/golang/freetype/truetype"
)
type horizontalBarChart struct {
p *Painter
opt *HorizontalBarChartOption
}
// NewHorizontalBarChartOptionWithData returns an initialized HorizontalBarChartOption with the SeriesList set for the provided data slice.
func NewHorizontalBarChartOptionWithData(data [][]float64) HorizontalBarChartOption {
sl := NewSeriesListHorizontalBar(data)
return HorizontalBarChartOption{
SeriesList: sl,
Padding: defaultPadding,
Theme: GetDefaultTheme(),
Font: GetDefaultFont(),
YAxis: make([]YAxisOption, sl.getYAxisCount()),
ValueFormatter: defaultValueFormatter,
}
}
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
// StackSeries if set to *true a single bar with the colored series stacked together will be rendered.
// This feature will result in some options being ignored, including BarMargin and SeriesLabelPosition.
// MarkLine is also interpreted differently, only the first Series will have the MarkLine rendered (as it's the
// base bar, other bars are influenced by prior values).
StackSeries *bool
// SeriesLabelPosition specifies the position of the label for the series. Currently supported values are
// "left" or "right".
SeriesLabelPosition string
// 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. Height may be reduced to ensure all series fit on the chart.
BarHeight int
// BarMargin specifies the margin between bars grouped together. BarHeight takes priority over the margin.
BarMargin *float64
// ValueFormatter defines how float values should be rendered to strings, notably for numeric axis labels.
ValueFormatter ValueFormatter
}
// 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)
seriesCount := len(seriesList)
if seriesCount == 0 {
return BoxZero, errors.New("empty series list")
}
stackedSeries := flagIs(true, opt.StackSeries)
min, max, sumMax := seriesList.getMinMaxSumMax(0, stackedSeries)
// If stacking, keep track of accumulated widths for each data index (after the “reverse” logic).
var accumulatedWidths []int
var margin, barMargin, barHeight int
if stackedSeries {
// If stacking, max should be the highest sum
max = sumMax
accumulatedWidths = make([]int, yRange.divideCount)
margin, _, barHeight = calculateBarMarginsAndSize(1, height, opt.BarHeight, nil)
} else {
margin, barMargin, barHeight = calculateBarMarginsAndSize(seriesCount, height, opt.BarHeight, opt.BarMargin)
}
seriesNames := seriesList.Names()
// xRange is used to convert data values into horizontal bar widths
xRange := newRange(p, getPreferredValueFormatter(opt.XAxis.ValueFormatter, opt.ValueFormatter),
seriesPainter.Width(), len(seriesList[0].Data), min, max, 1.0, 1.0)
divideValues := yRange.AutoDivide()
var rendererList []renderer
for index := range seriesList {
series := seriesList[index]
seriesColor := opt.Theme.GetSeriesColor(series.index)
var labelPainter *seriesLabelPainter
if flagIs(true, series.Label.Show) {
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, series.Label, opt.Theme, opt.Font)
rendererList = append(rendererList, labelPainter)
}
for j, item := range series.Data {
if j >= yRange.divideCount {
continue
}
// Reverse the category index for drawing from top to bottom
reversedJ := yRange.divideCount - j - 1
// Compute the top of this bar “row”
y := divideValues[reversedJ] + margin
// Determine the width (horizontal length) of the bar based on the data value
w := xRange.getHeight(item)
var left, right int
if stackedSeries {
// Start where the previous series ended
left = accumulatedWidths[reversedJ]
right = left + w
accumulatedWidths[reversedJ] = right
} else {
// Offset each series in its own lane
if index != 0 {
y += index * (barHeight + barMargin)
}
left = 0
right = w
}
seriesPainter.FilledRect(left, y, right, y+barHeight, seriesColor, seriesColor, 0.0)
if labelPainter != nil {
fontStyle := series.Label.FontStyle
labelX := right
labelY := y + (barHeight >> 1)
labelLeft := (opt.SeriesLabelPosition == PositionLeft || series.Label.Position == PositionLeft) && !stackedSeries
if labelLeft {
labelX = 0
}
if fontStyle.FontColor.IsZero() {
var testColor Color
if labelLeft {
testColor = seriesColor
} else if stackedSeries && index+1 < seriesCount {
testColor = opt.Theme.GetSeriesColor(index + 1)
}
if !testColor.IsZero() {
if isLightColor(testColor) {
fontStyle.FontColor = defaultLightFontColor
} else {
fontStyle.FontColor = defaultDarkFontColor
}
}
}
labelPainter.Add(labelValue{
vertical: false, // horizontal label
index: index,
value: item,
x: labelX,
y: labelY,
offset: series.Label.Offset,
fontStyle: fontStyle,
})
}
}
}
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,
stackSeries: flagIs(true, opt.StackSeries),
xAxis: &h.opt.XAxis,
yAxis: opt.YAxis,
title: opt.Title,
legend: &h.opt.Legend,
valueFormatter: opt.ValueFormatter,
axisReversed: true,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeHorizontalBar)
return h.render(renderResult, seriesList)
}