Skip to content

Commit 1958b50

Browse files
committed
Minor cleanup changes
These include some minor changes that were originally in the v0.4.0 branch, but can be provided without any impact in functionality. Backporting to keep the API change PR as clean as possible.
1 parent 2e15c54 commit 1958b50

File tree

7 files changed

+33
-79
lines changed

7 files changed

+33
-79
lines changed

axis.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ func (a *axisPainter) Render() (Box, error) {
6868
return BoxZero, nil
6969
}
7070
top := a.p
71-
theme := opt.Theme
72-
if theme == nil {
73-
theme = top.theme
74-
}
71+
theme := getPreferredTheme(opt.Theme, top.theme)
7572

7673
strokeWidth := opt.StrokeWidth
7774
if strokeWidth == 0 {
@@ -155,6 +152,7 @@ func (a *axisPainter) Render() (Box, error) {
155152
case PositionTop:
156153
labelPaddingTop = 0
157154
x1 = p.Width()
155+
// TODO - should this reference opt.FontStyle or fontStyle with defaults set
158156
y0 = labelMargin + int(opt.FontStyle.FontSize)
159157
ticksPaddingTop = int(opt.FontStyle.FontSize)
160158
y1 = y0

bar_chart.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
135135
}
136136
// generate marker point by hand
137137
points[j] = Point{
138-
// centered position
139-
X: x + barWidth>>1,
138+
X: x + (barWidth >> 1), // centered position
140139
Y: top,
141140
}
142141
// return if the label does not need to be displayed

chart_option.go

Lines changed: 4 additions & 5 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
@@ -50,7 +50,6 @@ type ChartOption struct {
5050
// Children are child charts to render together.
5151
Children []ChartOption
5252
parent *Painter
53-
// TODO - review how this is set on other Option structs
5453
// ValueFormatter to format numeric values into labels.
5554
ValueFormatter ValueFormatter
5655
}

echarts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,17 @@ func renderEcharts(options, outputType string) ([]byte, error) {
482482
}
483483
opt := o.ToOption()
484484
opt.OutputFormat = outputType
485-
if d, err := Render(opt); err != nil {
485+
if p, err := Render(opt); err != nil {
486486
return nil, err
487487
} else {
488-
return d.Bytes()
488+
return p.Bytes()
489489
}
490490
}
491491

492492
func RenderEChartsToPNG(options string) ([]byte, error) {
493-
return renderEcharts(options, "png")
493+
return renderEcharts(options, ChartOutputPNG)
494494
}
495495

496496
func RenderEChartsToSVG(options string) ([]byte, error) {
497-
return renderEcharts(options, "svg")
497+
return renderEcharts(options, ChartOutputSVG)
498498
}

painter.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
"github.com/go-analyze/charts/chartdraw"
1010
)
1111

12+
// ValueFormatter defines a function that can be used to format numeric values.
1213
type ValueFormatter func(float64) string
1314

1415
var defaultValueFormatter = func(val float64) string {
1516
return FormatValueHumanizeShort(val, 2, false)
1617
}
1718

19+
// Painter is the primary struct for drawing charts/graphs.
1820
type Painter struct {
1921
render chartdraw.Renderer
2022
box Box
21-
parent *Painter
2223
style chartdraw.Style
2324
theme ColorPalette
2425
font *truetype.Font
2526
outputFormat string
2627
valueFormatter ValueFormatter
2728
}
2829

30+
// PainterOptions contains parameters for creating a new Painter.
2931
type PainterOptions struct {
30-
// OutputFormat specifies the output type, "svg" or "png", default value is "png"
32+
// OutputFormat specifies the output type, "svg" or "png", default is "png".
3133
OutputFormat string
3234
// Width is the width of the draw painter.
3335
Width int
3436
// Height is the height of the draw painter.
3537
Height int
36-
// TODO - is this the best place for font configuration?
3738
// Font is the font used for rendering text.
3839
Font *truetype.Font
3940
}
@@ -87,7 +88,7 @@ func PainterPaddingOption(padding Box) PainterOption {
8788
}
8889
}
8990

90-
// PainterBoxOption sets the box of draw painter
91+
// PainterBoxOption sets a specific box for the Painter to draw within.
9192
func PainterBoxOption(box Box) PainterOption {
9293
return func(p *Painter) {
9394
if box.IsZero() {
@@ -114,7 +115,8 @@ func PainterStyleOption(style chartdraw.Style) PainterOption {
114115
}
115116
}
116117

117-
// PainterThemeOption sets the theme of draw painter
118+
// PainterThemeOption sets a color palette theme default for the Painter.
119+
// This theme is used if the specific chart options don't have a theme set.
118120
func PainterThemeOption(theme ColorPalette) PainterOption {
119121
return func(p *Painter) {
120122
if theme == nil {
@@ -184,13 +186,13 @@ func (p *Painter) setOptions(opts ...PainterOption) {
184186

185187
func (p *Painter) Child(opt ...PainterOption) *Painter {
186188
child := &Painter{
187-
valueFormatter: p.valueFormatter,
188189
render: p.render,
189190
box: p.box.Clone(),
190-
parent: p,
191191
style: p.style,
192192
theme: p.theme,
193193
font: p.font,
194+
outputFormat: p.outputFormat,
195+
valueFormatter: p.valueFormatter,
194196
}
195197
child.setOptions(opt...)
196198
return child
@@ -412,14 +414,17 @@ func (p *Painter) Fill() *Painter {
412414
return p
413415
}
414416

417+
// Width returns the drawable width of the painter's box.
415418
func (p *Painter) Width() int {
416419
return p.box.Width()
417420
}
418421

422+
// Height returns the drawable height of the painter's box.
419423
func (p *Painter) Height() int {
420424
return p.box.Height()
421425
}
422426

427+
// MeasureText will provide the rendered size of the text for the provided font style.
423428
func (p *Painter) MeasureText(text string) Box {
424429
return p.render.MeasureText(text)
425430
}
@@ -439,6 +444,9 @@ func (p *Painter) MeasureTextMaxWidthHeight(textList []string) (int, int) {
439444
return maxWidth, maxHeight
440445
}
441446

447+
// LineStroke draws a line in the graph from point to point with the specified stroke color/width.
448+
// Points with values of math.MaxInt32 will be skipped, resulting in a gap.
449+
// Single or isolated points will result in just a dot being drawn at the point.
442450
func (p *Painter) LineStroke(points []Point) *Painter {
443451
var valid []Point
444452
for _, pt := range points {
@@ -457,6 +465,8 @@ func (p *Painter) LineStroke(points []Point) *Painter {
457465
return p.Stroke()
458466
}
459467

468+
// drawStraightPath draws a simple (non-curved) path for the given points.
469+
// If dotForSinglePoint is true, single points are drawn as 2px radius dots.
460470
func (p *Painter) drawStraightPath(points []Point, dotForSinglePoint bool) {
461471
pointCount := len(points)
462472
if pointCount == 0 {
@@ -576,6 +586,8 @@ func (p *Painter) SetBackground(width, height int, color Color, inside ...bool)
576586
p.FillStroke()
577587
return p
578588
}
589+
590+
// MarkLine draws a horizontal line with a small circle and arrow at the right.
579591
func (p *Painter) MarkLine(x, y, width int) *Painter {
580592
arrowWidth := 16
581593
arrowHeight := 10
@@ -590,6 +602,7 @@ func (p *Painter) MarkLine(x, y, width int) *Painter {
590602
return p
591603
}
592604

605+
// Polygon draws a polygon with the specified center, radius, and number of sides.
593606
func (p *Painter) Polygon(center Point, radius float64, sides int) *Painter {
594607
points := getPolygonPoints(center, radius, sides)
595608
for i, item := range points {

util.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"strings"
88

99
"github.com/dustin/go-humanize"
10-
11-
"github.com/go-analyze/charts/chartdraw"
1210
)
1311

1412
// True returns a pointer to a true bool, useful for configuration.
@@ -113,18 +111,6 @@ func sumInt(values []int) int {
113111
return sum
114112
}
115113

116-
// measureTextMaxWidthHeight returns maxWidth and maxHeight of text list
117-
func measureTextMaxWidthHeight(textList []string, p *Painter) (int, int) {
118-
maxWidth := 0
119-
maxHeight := 0
120-
for _, text := range textList {
121-
box := p.MeasureText(text)
122-
maxWidth = chartdraw.MaxInt(maxWidth, box.Width())
123-
maxHeight = chartdraw.MaxInt(maxHeight, box.Height())
124-
}
125-
return maxWidth, maxHeight
126-
}
127-
128114
func reverseStringSlice(stringList []string) {
129115
for i, j := 0, len(stringList)-1; i < j; i, j = i+1, j-1 {
130116
stringList[i], stringList[j] = stringList[j], stringList[i]

util_test.go

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
9-
10-
"github.com/go-analyze/charts/chartdraw"
118
)
129

1310
func TestGetDefaultInt(t *testing.T) {
@@ -126,53 +123,15 @@ func TestGetRadius(t *testing.T) {
126123
assert.Equal(t, 40.0, getRadius(100, ""))
127124
}
128125

129-
func TestMeasureTextMaxWidthHeight(t *testing.T) {
130-
t.Parallel()
131-
132-
p, err := NewPainter(PainterOptions{
133-
Width: 400,
134-
Height: 300,
135-
})
136-
require.NoError(t, err)
137-
style := FontStyle{
138-
FontSize: 10,
139-
}
140-
p.SetStyle(chartdraw.Style{FontStyle: style})
141-
142-
maxWidth, maxHeight := measureTextMaxWidthHeight([]string{
143-
"Mon",
144-
"Tue",
145-
"Wed",
146-
"Thu",
147-
"Fri",
148-
"Sat",
149-
"Sun",
150-
}, p)
151-
assert.Equal(t, 31, maxWidth)
152-
assert.Equal(t, 12, maxHeight)
153-
}
154-
155126
func TestReverseSlice(t *testing.T) {
156127
t.Parallel()
157128

158129
arr := []string{
159-
"Mon",
160-
"Tue",
161-
"Wed",
162-
"Thu",
163-
"Fri",
164-
"Sat",
165-
"Sun",
130+
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
166131
}
167132
reverseStringSlice(arr)
168133
assert.Equal(t, []string{
169-
"Sun",
170-
"Sat",
171-
"Fri",
172-
"Thu",
173-
"Wed",
174-
"Tue",
175-
"Mon",
134+
"Sun", "Sat", "Fri", "Thu", "Wed", "Tue", "Mon",
176135
}, arr)
177136

178137
numbers := []int{1, 3, 5, 7, 9}

0 commit comments

Comments
 (0)