-
Notifications
You must be signed in to change notification settings - Fork 4
/
progress_bar_circular.go
213 lines (169 loc) · 5.86 KB
/
progress_bar_circular.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
package unichart
import (
"io"
"math"
"github.com/unidoc/unichart/mathutil"
"github.com/unidoc/unichart/render"
)
// CircularProgressBar is a component that will render circular progress bar component.
type CircularProgressBar 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 in the center of 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
// Reversed is a flag where if the value is true then the progress bar would rendered counter clockwise.
Reversed bool
// size is the size of the progress bar in width and height.
size int
dpi float64
// progress is the progress bar values which should be between 0.0 - 1.0.
progress float64
// label is the text to be displayed in the center of the chart.
label string
}
// SetProgress set the progress that will represented by this chart.
// Expected value should be float number between 0.0 - 1.0.
func (cp *CircularProgressBar) SetProgress(progress float64) {
cp.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 (cp *CircularProgressBar) GetProgress() float64 {
return cp.progress
}
// DPI returns the DPI of the progress bar.
func (cp *CircularProgressBar) DPI() float64 {
if cp.dpi == 0 {
return defaultDPI
}
return cp.dpi
}
// SetDPI sets the DPI for the progress bar.
func (cp *CircularProgressBar) SetDPI(dpi float64) {
cp.dpi = dpi
}
// Size returns the chart size or the default value.
func (cp *CircularProgressBar) Size() int {
if cp.size == 0 {
return defaultChartWidth
}
return cp.size
}
// SetSize sets the chart size.
func (cp *CircularProgressBar) SetSize(size int) {
cp.size = size
}
// SetLabel sets the label that would be displayed in the center of the progress bar.
func (cp *CircularProgressBar) SetLabel(label string) {
cp.label = label
}
// GetLabel returns the label displayed in the center of the progress bar.
func (cp *CircularProgressBar) GetLabel() string {
return cp.label
}
// Width returns the chart width.
func (cp *CircularProgressBar) Width() int {
return cp.Size()
}
// SetWidth method is exists to fuifill the requirements of render.ChartRenderable interface.
// To set width or height of this circular progress bar, use SetSize instead.
func (cp *CircularProgressBar) SetWidth(width int) {
}
// Height returns the chart height.
func (cp *CircularProgressBar) Height() int {
return cp.Size()
}
// SetHeight method is exists to fuifill the requirements of render.ChartRenderable interface.
// To set width or height of this circular progress bar, use SetSize instead.
func (cp *CircularProgressBar) SetHeight(height int) {
}
func (cp *CircularProgressBar) getBackgroundStyle() render.Style {
return cp.BackgroundStyle.InheritFrom(cp.styleDefaultsBackground())
}
func (cp *CircularProgressBar) styleDefaultsBackground() render.Style {
return render.Style{
FillColor: cp.GetColorPalette().BackgroundColor(),
StrokeColor: cp.GetColorPalette().BackgroundStrokeColor(),
StrokeWidth: render.DefaultStrokeWidth,
}
}
func (cp *CircularProgressBar) getForegroundStyle() render.Style {
return cp.ForegroundStyle.InheritFrom(cp.styleDefaultsForeground())
}
func (cp *CircularProgressBar) styleDefaultsForeground() render.Style {
return render.Style{
FillColor: cp.GetColorPalette().BackgroundColor(),
StrokeColor: cp.GetColorPalette().BackgroundStrokeColor(),
StrokeWidth: render.DefaultStrokeWidth,
}
}
func (cp *CircularProgressBar) getLabelStyle() render.Style {
return cp.LabelStyle.InheritFrom(cp.styleDefaultsLabel())
}
func (cp *CircularProgressBar) styleDefaultsLabel() render.Style {
return render.Style{
FontSize: render.DefaultFontSize,
FontColor: cp.getForegroundStyle().StrokeColor,
TextHorizontalAlign: render.TextHorizontalAlignCenter,
TextVerticalAlign: render.TextVerticalAlignMiddle,
}
}
// GetColorPalette returns the color palette for the chart.
func (cp *CircularProgressBar) GetColorPalette() render.ColorPalette {
if cp.ColorPalette != nil {
return cp.ColorPalette
}
return render.AlternateColorPalette
}
func (cp *CircularProgressBar) drawBackground(r render.Renderer) {
bgStyle := cp.getBackgroundStyle()
if bgStyle.Hidden {
return
}
radius := cp.Size() / 2.0
r.Circle(float64(radius), radius, radius)
r.SetFillColor(bgStyle.FillColor)
r.SetStrokeColor(bgStyle.StrokeColor)
r.SetStrokeWidth(bgStyle.StrokeWidth)
r.FillStroke()
}
func (cp *CircularProgressBar) drawForeground(r render.Renderer) {
fgStyle := cp.getForegroundStyle()
if fgStyle.Hidden {
return
}
radius := float64(cp.Size()) / 2.0
progressDeg := cp.progress * 360.0
if cp.Reversed {
progressDeg = -progressDeg
}
r.MoveTo(int(radius), 0)
r.ArcTo(int(radius), int(radius), radius, radius, mathutil.DegreesToRadians(-90), mathutil.DegreesToRadians(progressDeg))
r.SetStrokeColor(fgStyle.StrokeColor)
r.SetStrokeWidth(fgStyle.StrokeWidth)
r.Stroke()
}
func (cp *CircularProgressBar) drawLabel(r render.Renderer) {
labelStyle := cp.getLabelStyle()
if labelStyle.Hidden || cp.label == "" {
return
}
fgStrokeWidth := int(labelStyle.StrokeWidth)
render.Text.DrawWithin(r, cp.label, render.NewBox(fgStrokeWidth, fgStrokeWidth, cp.Size()-fgStrokeWidth, cp.Size()-fgStrokeWidth), labelStyle)
}
// Render renders the progress bar with the given renderer to the given io.Writer.
func (cp *CircularProgressBar) Render(rp render.RendererProvider, w io.Writer) error {
r, err := rp(cp.Size(), cp.Size())
if err != nil {
return err
}
r.SetDPI(cp.DPI())
cp.drawBackground(r)
cp.drawForeground(r)
cp.drawLabel(r)
return r.Save(w)
}