-
Notifications
You must be signed in to change notification settings - Fork 4
/
progress_bar_linear.go
281 lines (219 loc) · 7.39 KB
/
progress_bar_linear.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package unichart
import (
"io"
"math"
"github.com/unidoc/unichart/mathutil"
"github.com/unidoc/unichart/render"
)
// LinearProgressBar is a component that will render progress bar component.
type LinearProgressBar struct {
// BackgroundStyle is the style for the background bar.
BackgroundStyle render.Style
// ForegroundStyle is the style for the foreground bar.
ForegroundStyle render.Style
// LabelStyle is the style for the label that will displayed on the progress bar.
LabelStyle render.Style
// ColorPalette is the color pallete that could be used to add colors in this progress bar
ColorPalette render.ColorPalette
// RoundedEdgeStart is a flag to enable rounded edge at the start of the bar.
RoundedEdgeStart bool
// RoundedEdgeEnd us a flag to enable rounded edge at the end of the bar.
RoundedEdgeEnd bool
// CustomTopInfo defines a user provided function to draw a custom info above the progress bar.
// The callback function should return the height occupied by the top info, which would be used
// to shift down the progress bar position.
CustomTopInfo func(r render.Renderer, x int) int
// CustomBottomInfo defines a user provided function to draw a custom info under the progress bar.
// The callback function should return the height occupied by the bottom info, which would be used
// to calculate this progress bar total height.
CustomBottomInfo func(r render.Renderer, x int) int
height int
width int
dpi float64
barYPos int
bottomInfoHeight int
// progress is the progress bar values which should be between 0.0 - 1.0.
progress float64
// label is the text that will be displayed on the progress bar.
label string
}
// SetProgress set the progress that will represented by this chart.
// Expected value should be float number between 0.0 - 1.0.
func (lp *LinearProgressBar) SetProgress(progress float64) {
lp.progress = math.Max(0.0, math.Min(progress, 1.0))
}
// GetProgress returns the progress represented by this chart.
// Returned value should be a float number between 0.0 - 1.0.
func (lp *LinearProgressBar) GetProgress() float64 {
return lp.progress
}
// DPI returns the DPI of the progress bar.
func (lp *LinearProgressBar) DPI() float64 {
if lp.dpi == 0 {
return defaultDPI
}
return lp.dpi
}
// SetDPI sets the DPI for the progrss bar.
func (lp *LinearProgressBar) SetDPI(dpi float64) {
lp.dpi = dpi
}
// Width returns the chart width or the default value.
func (lp *LinearProgressBar) Width() int {
if lp.width == 0 {
return defaultChartWidth
}
return lp.width
}
// SetWidth sets the chart width.
func (lp *LinearProgressBar) SetWidth(width int) {
lp.width = width
}
// Height returns the chart height or the default value.
func (lp *LinearProgressBar) Height() int {
if lp.height == 0 {
return defaultChartHeight
}
return lp.height + lp.barYPos + lp.bottomInfoHeight
}
// SetHeight sets the chart height.
func (lp *LinearProgressBar) SetHeight(height int) {
lp.height = height
}
// SetLabel sets the labe that would be rendered on the progress bar.
func (lp *LinearProgressBar) SetLabel(label string) {
lp.label = label
}
// GetLabel would returns label that rendered on the progress bar.
func (lp *LinearProgressBar) GetLabel() string {
return lp.label
}
func (lp *LinearProgressBar) getBackgroundStyle() render.Style {
return lp.BackgroundStyle.InheritFrom(lp.styleDefaultsBackground())
}
func (lp *LinearProgressBar) styleDefaultsBackground() render.Style {
return render.Style{
FillColor: lp.GetColorPalette().BackgroundColor(),
StrokeColor: lp.GetColorPalette().BackgroundStrokeColor(),
StrokeWidth: render.DefaultStrokeWidth,
}
}
func (lp *LinearProgressBar) getForegroundStyle() render.Style {
return lp.ForegroundStyle.InheritFrom(lp.styleDefaultsForeground())
}
func (lp *LinearProgressBar) styleDefaultsForeground() render.Style {
return render.Style{
FillColor: lp.GetColorPalette().BackgroundColor(),
StrokeColor: lp.GetColorPalette().BackgroundStrokeColor(),
StrokeWidth: render.DefaultStrokeWidth,
}
}
func (lp *LinearProgressBar) getLabelStyle() render.Style {
return lp.LabelStyle.InheritFrom(lp.styleDefaultsLabel())
}
func (lp *LinearProgressBar) styleDefaultsLabel() render.Style {
return render.Style{
FontSize: render.DefaultFontSize,
FontColor: lp.getForegroundStyle().FillColor,
TextHorizontalAlign: render.TextHorizontalAlignLeft,
TextVerticalAlign: render.TextVerticalAlignMiddle,
}
}
// GetColorPalette returns the color palette for the chart.
func (lp *LinearProgressBar) GetColorPalette() render.ColorPalette {
if lp.ColorPalette != nil {
return lp.ColorPalette
}
return render.AlternateColorPalette
}
func (lp *LinearProgressBar) roundedEdgeRadius() float64 {
return float64(lp.height) / 2
}
func (lp *LinearProgressBar) drawBar(r render.Renderer, width int, style render.Style) {
roundStartRadius := 0.0
if lp.RoundedEdgeStart {
roundStartRadius = lp.roundedEdgeRadius()
}
roundEndRadius := 0.0
if lp.RoundedEdgeEnd {
roundEndRadius = lp.roundedEdgeRadius()
}
h := lp.height
r.MoveTo(int(roundStartRadius), lp.barYPos)
r.LineTo(width-int(roundEndRadius), lp.barYPos)
if lp.RoundedEdgeEnd {
r.ArcTo(width-int(roundEndRadius), lp.barYPos+h/2, roundEndRadius, roundEndRadius, mathutil.DegreesToRadians(-90), mathutil.DegreesToRadians(180))
} else {
r.LineTo(width, lp.barYPos+h)
}
r.MoveTo(width-int(roundEndRadius), lp.barYPos+h)
r.LineTo(int(roundStartRadius), lp.barYPos+h)
if lp.RoundedEdgeStart {
r.ArcTo(int(roundStartRadius), lp.barYPos+h/2, roundStartRadius, roundStartRadius, mathutil.DegreesToRadians(90), mathutil.DegreesToRadians(180))
} else {
r.LineTo(0, lp.barYPos)
}
r.SetFillColor(style.FillColor)
strokeColor := style.StrokeColor
strokeWidth := style.StrokeWidth
if style.StrokeWidth == 0 {
strokeColor = style.FillColor
strokeWidth = lp.getBackgroundStyle().StrokeWidth
}
r.SetStrokeColor(strokeColor)
r.SetStrokeWidth(strokeWidth)
r.FillStroke()
}
func (lp *LinearProgressBar) drawBackground(r render.Renderer) {
bgStyle := lp.getBackgroundStyle()
if bgStyle.Hidden {
return
}
lp.drawBar(r, lp.Width(), bgStyle)
}
func (lp *LinearProgressBar) drawForeground(r render.Renderer) {
fgStyle := lp.getForegroundStyle()
if fgStyle.Hidden {
return
}
w := float64(lp.Width()) * lp.progress
lp.drawBar(r, int(w), fgStyle)
}
func (lp *LinearProgressBar) drawLabel(r render.Renderer) {
labelStyle := lp.getLabelStyle()
if labelStyle.Hidden || lp.label == "" {
return
}
x := float64(lp.Width()) * lp.progress
y := lp.barYPos
render.Text.DrawWithin(r, lp.label, render.NewBox(y, int(x)+10, lp.Width(), y+lp.height), labelStyle)
}
func (lp *LinearProgressBar) drawTopInfo(r render.Renderer) {
x := float64(lp.Width()) * lp.progress
if lp.CustomTopInfo != nil {
lp.barYPos = lp.CustomTopInfo(r, int(x))
return
}
}
func (lp *LinearProgressBar) drawBottomInfo(r render.Renderer) {
x := float64(lp.Width()) * lp.progress
if lp.CustomBottomInfo != nil {
lp.bottomInfoHeight = lp.CustomBottomInfo(r, int(x))
return
}
}
// Render renders the progrss bar with the given renderer to the given io.Writer.
func (lp *LinearProgressBar) Render(rp render.RendererProvider, w io.Writer) error {
r, err := rp(lp.Width(), lp.height)
if err != nil {
return err
}
r.SetDPI(lp.DPI())
lp.barYPos = 0
lp.drawTopInfo(r)
lp.drawBackground(r)
lp.drawForeground(r)
lp.drawLabel(r)
lp.drawBottomInfo(r)
return r.Save(w)
}