Skip to content

Commit

Permalink
Start of v0.4.0 Config API Changes
Browse files Browse the repository at this point in the history
v0.4.0 will be focused on minimizing and simplifying the public API so that our module is easier to learn.  This will include removing public structs and functions not expected to be utilized by the user, as well as making sure the public API presented is consistent.

Changes included so far:
* `LegendOptions.Vertical` introduced in #20 has been changed from a `bool` to `*bool`.  This makes the field more consistent to other configuration, and provides more future flexibility for dynamic defaults.
* Public `NewXPainter` functions are made private, these are only expected to be used internally.
* Public structs `AxisOption`, `GridPainterOption`, `SeriesLabelPainter`, `SeriesLabelPainterParams`, have been made private or removed.  These are expected to only be used internally.
* `LabelFormatter` has been removed (not to be confused with `ValueFormatter`, which remains)
  • Loading branch information
jentfoo committed Jan 1, 2025
1 parent 86a6839 commit c8b09ec
Show file tree
Hide file tree
Showing 42 changed files with 171 additions and 202 deletions.
6 changes: 3 additions & 3 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (

type axisPainter struct {
p *Painter
opt *AxisOption
opt *axisOption
}

func NewAxisPainter(p *Painter, opt AxisOption) *axisPainter {
func newAxisPainter(p *Painter, opt axisOption) *axisPainter {
return &axisPainter{
p: p,
opt: &opt,
}
}

type AxisOption struct {
type axisOption struct {
// Show specifies if the axis should be rendered, set this to *false (through False()) to hide the axis.
Show *bool
// Theme specifies the colors used for the axis.
Expand Down
24 changes: 12 additions & 12 deletions axis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAxis(t *testing.T) {
{
name: "x-axis_bottom",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
SplitLineShow: true,
}).Render()
Expand All @@ -42,7 +42,7 @@ func TestAxis(t *testing.T) {
{
name: "x-axis_bottom_left",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
BoundaryGap: False(),
}).Render()
Expand All @@ -53,7 +53,7 @@ func TestAxis(t *testing.T) {
{
name: "y-axis_left",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
Position: PositionLeft,
}).Render()
Expand All @@ -64,7 +64,7 @@ func TestAxis(t *testing.T) {
{
name: "y-axis_center",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
Position: PositionLeft,
BoundaryGap: False(),
Expand All @@ -77,7 +77,7 @@ func TestAxis(t *testing.T) {
{
name: "y-axis_right",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
Position: PositionRight,
BoundaryGap: False(),
Expand All @@ -90,7 +90,7 @@ func TestAxis(t *testing.T) {
{
name: "top",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: dayLabels,
Formatter: "{value} --",
Position: PositionTop,
Expand All @@ -102,7 +102,7 @@ func TestAxis(t *testing.T) {
{
name: "reduced_label_count",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: letterLabels,
SplitLineShow: false,
LabelCountAdjustment: -1,
Expand All @@ -114,7 +114,7 @@ func TestAxis(t *testing.T) {
{
name: "custom_unit",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: letterLabels,
SplitLineShow: false,
Unit: 10,
Expand All @@ -126,7 +126,7 @@ func TestAxis(t *testing.T) {
{
name: "custom_font",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: letterLabels,
FontStyle: FontStyle{
FontSize: 40.0,
Expand All @@ -140,7 +140,7 @@ func TestAxis(t *testing.T) {
{
name: "boundary_gap_disable",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: letterLabels,
BoundaryGap: False(),
}).Render()
Expand All @@ -151,7 +151,7 @@ func TestAxis(t *testing.T) {
{
name: "boundary_gap_enable",
render: func(p *Painter) ([]byte, error) {
_, _ = NewAxisPainter(p, AxisOption{
_, _ = newAxisPainter(p, axisOption{
Data: letterLabels,
BoundaryGap: True(),
}).Render()
Expand All @@ -170,7 +170,7 @@ func TestAxis(t *testing.T) {
})
for i, tt := range tests {
t.Run(strconv.Itoa(i)+"-"+tt.name, func(t *testing.T) {
p, err := NewPainter(PainterOptions{
p, err := newPainter(PainterOptions{
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
Expand Down
14 changes: 4 additions & 10 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
theme := opt.Theme
seriesNames := seriesList.Names()

markPointPainter := NewMarkPointPainter(seriesPainter)
markLinePainter := NewMarkLinePainter(seriesPainter)
markPointPainter := newMarkPointPainter(seriesPainter)
markLinePainter := newMarkLinePainter(seriesPainter)
rendererList := []Renderer{
markPointPainter,
markLinePainter,
Expand All @@ -87,15 +87,9 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B

divideValues := xRange.AutoDivide()
points := make([]Point, len(series.Data))
var labelPainter *SeriesLabelPainter
var labelPainter *seriesLabelPainter
if series.Label.Show {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, series.Label, opt.Theme, opt.Font)
rendererList = append(rendererList, labelPainter)
}

Expand Down
6 changes: 3 additions & 3 deletions bar_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,20 @@ func TestBarChart(t *testing.T) {
}
if tt.defaultTheme {
t.Run(strconv.Itoa(i)+"-"+tt.name, func(t *testing.T) {
p, err := NewPainter(painterOptions)
p, err := newPainter(painterOptions)
require.NoError(t, err)

validateBarChartRender(t, p, tt.makeOptions(), tt.result)
})
} else {
t.Run(strconv.Itoa(i)+"-"+tt.name+"-painter", func(t *testing.T) {
p, err := NewPainter(painterOptions, PainterThemeOption(GetTheme(ThemeVividDark)))
p, err := newPainter(painterOptions, PainterThemeOption(GetTheme(ThemeVividDark)))
require.NoError(t, err)

validateBarChartRender(t, p, tt.makeOptions(), tt.result)
})
t.Run(strconv.Itoa(i)+"-"+tt.name+"-options", func(t *testing.T) {
p, err := NewPainter(painterOptions)
p, err := newPainter(painterOptions)
require.NoError(t, err)
opt := tt.makeOptions()
opt.Theme = GetTheme(ThemeVividDark)
Expand Down
5 changes: 3 additions & 2 deletions chart_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func PieSeriesShowLabel() OptionFunc {
}
}

// TODO - add testing and examples
// ChildOptionFunc add child chart
func ChildOptionFunc(child ...ChartOption) OptionFunc {
return func(opt *ChartOption) {
Expand Down Expand Up @@ -391,7 +392,7 @@ func TableOptionRender(opt TableChartOption) (*Painter, error) {
opt.Width = defaultChartWidth
}

p, err := NewPainter(PainterOptions{
p, err := newPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: opt.Width,
Height: 100, // is only used to calculate the height of the table
Expand All @@ -405,7 +406,7 @@ func TableOptionRender(opt TableChartOption) (*Painter, error) {
return nil, err
}

p, err = NewPainter(PainterOptions{
p, err = newPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: info.width,
Height: info.height,
Expand Down
2 changes: 1 addition & 1 deletion chart_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestPieRender(t *testing.T) {
Left: 20,
}),
LegendOptionFunc(LegendOption{
Vertical: true,
Vertical: True(),
Data: []string{
"Search Engine",
"Direct",
Expand Down
8 changes: 4 additions & 4 deletions charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e

const legendTitlePadding = 15
legendTopSpacing := 0
legendResult, err := NewLegendPainter(p, opt.Legend).Render()
legendResult, err := newLegendPainter(p, opt.Legend).Render()
if err != nil {
return nil, err
}
if !legendResult.IsZero() && !opt.Legend.Vertical && !flagIs(true, opt.Legend.OverlayChart) {
if !legendResult.IsZero() && !flagIs(true, opt.Legend.Vertical) && !flagIs(true, opt.Legend.OverlayChart) {
legendHeight := legendResult.Height()
if legendResult.Bottom < p.Height()/2 {
// horizontal legend at the top, set the spacing based on the height
Expand All @@ -131,7 +131,7 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
}
}

titleBox, err := NewTitlePainter(p, opt.Title).Render()
titleBox, err := newTitlePainter(p, opt.Title).Render()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -313,7 +313,7 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {

isChild := opt.parent != nil
if !isChild {
p, err := NewPainter(PainterOptions{
p, err := newPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: opt.Width,
Height: opt.Height,
Expand Down
2 changes: 1 addition & 1 deletion echarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (eo *EChartsOption) ToOption() ChartOption {
Top: string(eo.Legend.Top),
},
Align: eo.Legend.Align,
Vertical: strings.EqualFold(eo.Legend.Orient, "vertical"),
Vertical: BoolPointer(strings.EqualFold(eo.Legend.Orient, "vertical")),
},
RadarIndicators: eo.Radar.Indicator,
Width: eo.Width,
Expand Down
2 changes: 1 addition & 1 deletion examples/line_chart-2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
charts.LegendOptionFunc(charts.LegendOption{
Data: []string{"Critical", "High", "Medium", "Low"},
// Legend Vertical, on the right, and with smaller font to give more space for data
Vertical: true,
Vertical: charts.True(),
Offset: charts.OffsetRight,
Align: charts.AlignRight,
FontStyle: charts.FontStyle{
Expand Down
2 changes: 1 addition & 1 deletion examples/pie_chart-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
"Union Ads",
"Video Ads",
},
Vertical: true,
Vertical: charts.True(),
Offset: charts.OffsetStr{
Left: "80%",
Top: charts.PositionBottom,
Expand Down
2 changes: 1 addition & 1 deletion examples/web-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
},
},
Legend: charts.LegendOption{
Vertical: true,
Vertical: charts.True(),
Data: []string{
"Search Engine",
"Direct",
Expand Down
2 changes: 1 addition & 1 deletion font_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestGetPreferredFont(t *testing.T) {
func TestCustomFontSizeRender(t *testing.T) {
t.Parallel()

p, err := NewPainter(PainterOptions{
p, err := newPainter(PainterOptions{
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
Expand Down
2 changes: 1 addition & 1 deletion funnel_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
if max != 0 {
percent = value / max
}
textList[index] = NewFunnelLabelFormatter(seriesNames, item.Label.Formatter)(index, value, percent)
textList[index] = labelFormatFunnel(seriesNames, item.Label.Formatter, index, value, percent)
}

for index, w := range widthList {
Expand Down
6 changes: 3 additions & 3 deletions funnel_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ func TestFunnelChart(t *testing.T) {
}
if tt.defaultTheme {
t.Run(strconv.Itoa(i)+"-"+tt.name, func(t *testing.T) {
p, err := NewPainter(painterOptions)
p, err := newPainter(painterOptions)
require.NoError(t, err)

validateFunnelChartRender(t, p, tt.makeOptions(), tt.result)
})
} else {
t.Run(strconv.Itoa(i)+"-"+tt.name+"-painter", func(t *testing.T) {
p, err := NewPainter(painterOptions, PainterThemeOption(GetTheme(ThemeVividDark)))
p, err := newPainter(painterOptions, PainterThemeOption(GetTheme(ThemeVividDark)))
require.NoError(t, err)

validateFunnelChartRender(t, p, tt.makeOptions(), tt.result)
})
t.Run(strconv.Itoa(i)+"-"+tt.name+"-options", func(t *testing.T) {
p, err := NewPainter(painterOptions)
p, err := newPainter(painterOptions)
require.NoError(t, err)
opt := tt.makeOptions()
opt.Theme = GetTheme(ThemeVividDark)
Expand Down
8 changes: 4 additions & 4 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

type gridPainter struct {
p *Painter
opt *GridPainterOption
opt *gridPainterOption
}

type GridPainterOption struct {
type gridPainterOption struct {
// StrokeWidth is the grid line width.
StrokeWidth float64
// StrokeColor is the grid line color.
Expand All @@ -30,8 +30,8 @@ type GridPainterOption struct {
IgnoreLastColumn bool
}

// NewGridPainter returns new a grid renderer
func NewGridPainter(p *Painter, opt GridPainterOption) *gridPainter {
// newGridPainter returns new a grid renderer
func newGridPainter(p *Painter, opt gridPainterOption) *gridPainter {
return &gridPainter{
p: p,
opt: &opt,
Expand Down
6 changes: 3 additions & 3 deletions grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGrid(t *testing.T) {
}{
{
render: func(p *Painter) ([]byte, error) {
_, err := NewGridPainter(p, GridPainterOption{
_, err := newGridPainter(p, gridPainterOption{
StrokeColor: drawing.ColorBlack,
Columns: 6,
Rows: 6,
Expand All @@ -36,7 +36,7 @@ func TestGrid(t *testing.T) {
},
{
render: func(p *Painter) ([]byte, error) {
_, err := NewGridPainter(p, GridPainterOption{
_, err := newGridPainter(p, gridPainterOption{
StrokeColor: drawing.ColorBlack,
ColumnSpans: []int{2, 5, 3},
Rows: 6,
Expand All @@ -51,7 +51,7 @@ func TestGrid(t *testing.T) {
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
p, err := NewPainter(PainterOptions{
p, err := newPainter(PainterOptions{
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
Expand Down
10 changes: 2 additions & 8 deletions horizontal_bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,9 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
seriesColor := theme.GetSeriesColor(series.index)
divideValues := yRange.AutoDivide()

var labelPainter *SeriesLabelPainter
var labelPainter *seriesLabelPainter
if series.Label.Show {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, series.Label, opt.Theme, opt.Font)
rendererList = append(rendererList, labelPainter)
}
for j, item := range series.Data {
Expand Down
Loading

0 comments on commit c8b09ec

Please sign in to comment.