Skip to content

Commit e8e4758

Browse files
committed
More configuration function API updates
Changes included: * Removed `OutputFormatOptionFunc`, instead use `SVGOutputOptionFunc()` or `PNGOutputOptionFunc()` * Dimensions are now always set together, `WidthOptionFunc` and `HeightOptionFunc` are now set with `DimensionsOptionFunc`. Similar `SetDefaultWidth` and `SetDefaultHeight` were replaced with `SetDefaultChartDimensions`. * `BoxOptionFunc` was removed, we don't currently have a clear example of where this is useful.
1 parent e4915ef commit e8e4758

File tree

6 files changed

+27
-47
lines changed

6 files changed

+27
-47
lines changed

chart_option.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
)
99

1010
type ChartOption struct {
11-
// OutputFormat specifies the output type of chart, "svg" or "png", default value is "png"
11+
// OutputFormat specifies the output type of chart, "svg" or "png", default value is "png".
1212
OutputFormat string
1313
// Width is the width of chart, default width is 600.
1414
Width int
15-
// Height is the height of chart, default height is 400
15+
// Height is the height of chart, default height is 400.
1616
Height int
1717
// Theme specifies the colors used for the chart. Built in themes can be loaded using GetTheme with
1818
// "light", "dark", "vivid-light", "vivid-dark", "ant" or "grafana".
1919
Theme ColorPalette
20-
// Padding specifies the padding for chart, default padding is [20, 10, 10, 10]
20+
// Padding specifies the padding for chart, default padding is [20, 10, 10, 10].
2121
Padding Box
2222
// XAxis are options for the x-axis.
2323
XAxis XAxisOption
@@ -33,7 +33,7 @@ type ChartOption struct {
3333
Box Box
3434
// SeriesList provides the data series.
3535
SeriesList SeriesList
36-
// RadarIndicators are radar indicator list for radar charts
36+
// RadarIndicators are radar indicator list for radar charts.
3737
RadarIndicators []RadarIndicator
3838
// SymbolShow set this to *false or *true (using False() or True()) to force if the symbols should be shown or hidden.
3939
SymbolShow *bool
@@ -57,18 +57,18 @@ type ChartOption struct {
5757
// OptionFunc option function
5858
type OptionFunc func(opt *ChartOption)
5959

60-
// SVGOutputOption set svg type of chart's output.
61-
func SVGOutputOption() OptionFunc {
62-
return OutputFormatOptionFunc(ChartOutputSVG)
60+
// SVGOutputOptionFunc set svg type of chart's output.
61+
func SVGOutputOptionFunc() OptionFunc {
62+
return outputFormatOptionFunc(ChartOutputSVG)
6363
}
6464

65-
// PNGOutputOption set png type of chart's output.
66-
func PNGOutputOption() OptionFunc {
67-
return OutputFormatOptionFunc(ChartOutputPNG)
65+
// PNGOutputOptionFunc set png type of chart's output.
66+
func PNGOutputOptionFunc() OptionFunc {
67+
return outputFormatOptionFunc(ChartOutputPNG)
6868
}
6969

70-
// OutputFormatOptionFunc set type of chart's output.
71-
func OutputFormatOptionFunc(t string) OptionFunc {
70+
// outputFormatOptionFunc set type of chart's output.
71+
func outputFormatOptionFunc(t string) OptionFunc {
7272
return func(opt *ChartOption) {
7373
opt.OutputFormat = t
7474
}
@@ -162,16 +162,10 @@ func YAxisDataOptionFunc(data []string) OptionFunc {
162162
}
163163
}
164164

165-
// WidthOptionFunc set width of chart
166-
func WidthOptionFunc(width int) OptionFunc {
165+
// DimensionsOptionFunc sets the width and height dimensions of the chart.
166+
func DimensionsOptionFunc(width, height int) OptionFunc {
167167
return func(opt *ChartOption) {
168168
opt.Width = width
169-
}
170-
}
171-
172-
// HeightOptionFunc set height of chart
173-
func HeightOptionFunc(height int) OptionFunc {
174-
return func(opt *ChartOption) {
175169
opt.Height = height
176170
}
177171
}
@@ -183,13 +177,6 @@ func PaddingOptionFunc(padding Box) OptionFunc {
183177
}
184178
}
185179

186-
// BoxOptionFunc set box of chart
187-
func BoxOptionFunc(box Box) OptionFunc {
188-
return func(opt *ChartOption) {
189-
opt.Box = box
190-
}
191-
}
192-
193180
// PieSeriesShowLabel set pie series show label
194181
func PieSeriesShowLabel() OptionFunc {
195182
return func(opt *ChartOption) {

chart_option_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ func TestChartOption(t *testing.T) {
1111
t.Parallel()
1212

1313
fns := []OptionFunc{
14-
SVGOutputOption(),
14+
SVGOutputOptionFunc(),
1515
FontOptionFunc(GetDefaultFont()),
1616
ThemeNameOptionFunc(ThemeVividDark),
1717
TitleTextOptionFunc("title"),
1818
LegendLabelsOptionFunc([]string{"label"}),
1919
XAxisDataOptionFunc([]string{"xaxis"}),
2020
YAxisDataOptionFunc([]string{"yaxis"}),
21-
WidthOptionFunc(800),
22-
HeightOptionFunc(600),
21+
DimensionsOptionFunc(800, 600),
2322
PaddingOptionFunc(Box{
2423
Left: 10,
2524
Top: 10,
@@ -142,7 +141,7 @@ func TestLineRender(t *testing.T) {
142141
}
143142
p, err := LineRender(
144143
values,
145-
SVGOutputOption(),
144+
SVGOutputOptionFunc(),
146145
TitleTextOptionFunc("Line"),
147146
XAxisDataOptionFunc([]string{
148147
"Mon",
@@ -202,7 +201,7 @@ func TestBarRender(t *testing.T) {
202201
}
203202
p, err := BarRender(
204203
values,
205-
SVGOutputOption(),
204+
SVGOutputOptionFunc(),
206205
XAxisDataOptionFunc([]string{
207206
"Jan",
208207
"Feb",
@@ -265,7 +264,7 @@ func TestHorizontalBarRender(t *testing.T) {
265264
}
266265
p, err := HorizontalBarRender(
267266
values,
268-
SVGOutputOption(),
267+
SVGOutputOptionFunc(),
269268
TitleTextOptionFunc("World Population"),
270269
PaddingOptionFunc(Box{
271270
Top: 20,
@@ -304,7 +303,7 @@ func TestPieRender(t *testing.T) {
304303
}
305304
p, err := PieRender(
306305
values,
307-
SVGOutputOption(),
306+
SVGOutputOptionFunc(),
308307
TitleOptionFunc(TitleOption{
309308
Text: "Rainfall vs Evaporation",
310309
Subtext: "Fake Data",
@@ -358,7 +357,7 @@ func TestRadarRender(t *testing.T) {
358357
}
359358
p, err := RadarRender(
360359
values,
361-
SVGOutputOption(),
360+
SVGOutputOptionFunc(),
362361
TitleTextOptionFunc("Basic Radar Chart"),
363362
LegendLabelsOptionFunc([]string{
364363
"Allocated Budget",
@@ -398,7 +397,7 @@ func TestFunnelRender(t *testing.T) {
398397
}
399398
p, err := FunnelRender(
400399
values,
401-
SVGOutputOption(),
400+
SVGOutputOptionFunc(),
402401
TitleTextOptionFunc("Funnel"),
403402
LegendLabelsOptionFunc([]string{
404403
"Show",

charts.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ const defaultYAxisLabelCountLow = 3
1818
var defaultChartWidth = 600
1919
var defaultChartHeight = 400
2020

21-
// SetDefaultWidth sets default width of chart
22-
func SetDefaultWidth(width int) {
21+
// SetDefaultChartDimensions sets default width and height of charts if not otherwise specified in their configuration.
22+
func SetDefaultChartDimensions(width, height int) {
2323
if width > 0 {
2424
defaultChartWidth = width
2525
}
26-
}
27-
28-
// SetDefaultHeight sets default height of chart
29-
func SetDefaultHeight(height int) {
3026
if height > 0 {
3127
defaultChartHeight = height
3228
}

examples/line_chart-2/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ func main() {
3636
p, err := charts.LineRender(
3737
values,
3838
charts.ThemeNameOptionFunc(charts.ThemeVividLight), // custom color theme
39-
charts.WidthOptionFunc(800),
40-
charts.HeightOptionFunc(600),
39+
charts.DimensionsOptionFunc(800, 600),
4140
charts.TitleOptionFunc(charts.TitleOption{
4241
Text: "Line Chart Demo",
4342
Offset: charts.OffsetCenter,

examples/table-1/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func writeFile(buf []byte, filename string) error {
2525
}
2626

2727
func main() {
28-
charts.SetDefaultWidth(810)
28+
charts.SetDefaultChartDimensions(810, 0) // 0 for height will leave it unchanged
2929
header := []string{
3030
"Name",
3131
"Age",

examples/web-1/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ func handler(w http.ResponseWriter, req *http.Request, chartOptions []charts.Cha
7373
theme := query.Get("theme")
7474
width, _ := strconv.Atoi(query.Get("width"))
7575
height, _ := strconv.Atoi(query.Get("height"))
76-
charts.SetDefaultWidth(width)
77-
charts.SetDefaultWidth(height)
76+
charts.SetDefaultChartDimensions(width, height)
7877
bytesList := make([][]byte, 0)
7978
for _, opt := range chartOptions {
8079
opt.Theme = charts.GetTheme(theme)

0 commit comments

Comments
 (0)