-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhorizontal_bar_chart.go
161 lines (148 loc) · 4.73 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
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
// 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")
}
margin, barMargin, barHeight := calculateBarMarginsAndSize(seriesCount, height, opt.BarHeight, opt.BarMargin)
seriesNames := seriesList.Names()
min, max := seriesList.GetMinMax(0)
xRange := newRange(p, getPreferredValueFormatter(opt.XAxis.ValueFormatter, opt.ValueFormatter),
seriesPainter.Width(), len(seriesList[0].Data), min, max, 1.0, 1.0)
var rendererList []renderer
for index := range seriesList {
series := seriesList[index]
seriesColor := opt.Theme.GetSeriesColor(series.index)
divideValues := yRange.AutoDivide()
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
}
// display position switch
j = yRange.divideCount - j - 1
y := divideValues[j]
y += margin
if index != 0 {
y += index * (barHeight + barMargin)
}
w := xRange.getHeight(item)
fillColor := seriesColor
right := w
seriesPainter.FilledRect(0, y, right, y+barHeight, fillColor, fillColor, 0.0)
// if the label does not need to be displayed, return
if labelPainter == nil {
continue
}
fontStyle := series.Label.FontStyle
if fontStyle.FontColor.IsZero() {
if isLightColor(fillColor) {
fontStyle.FontColor = defaultLightFontColor
} else {
fontStyle.FontColor = defaultDarkFontColor
}
}
labelValue := labelValue{
vertical: false, // label beside bar
index: index,
value: item,
x: right,
y: y + (barHeight >> 1),
offset: series.Label.Offset,
fontStyle: fontStyle,
}
if series.Label.Position == PositionLeft {
labelValue.x = 0
}
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: &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)
}