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)
  * Additional functions expected to be internal only: `NewRange`, `NewLeftYAxis`, `NewRightYAxis`, `NewBottomXAxis`, `NewEChartsSeriesDataValue` (struct is still public if needed)
  * `PainterOption` function has been renamed to `PainterOptionFunc`
  * 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.
  • Loading branch information
jentfoo committed Jan 10, 2025
1 parent 0d67ace commit 40756d5
Show file tree
Hide file tree
Showing 38 changed files with 242 additions and 328 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
22 changes: 11 additions & 11 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 Down
35 changes: 14 additions & 21 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
opt := b.opt
seriesPainter := result.seriesPainter

xRange := NewRange(b.p, seriesPainter.Width(), len(opt.XAxis.Data), 0.0, 0.0, 0.0, 0.0)
xRange := newRange(b.p, seriesPainter.Width(), len(opt.XAxis.Data), 0.0, 0.0, 0.0, 0.0)
x0, x1 := xRange.GetRange(0)
width := int(x1 - x0)
// margin between each block
Expand All @@ -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 Expand Up @@ -156,16 +150,15 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
}
}
}
labelPainter.Add(LabelValue{
Vertical: true, // label is above bar
Index: index,
Value: item.Value,
FontStyle: fontStyle,
X: x + barWidth>>1,
Y: y,
// rotate
Radians: radians,
Offset: series.Label.Offset,
labelPainter.Add(labelValue{
vertical: true, // label is above bar
index: index,
value: item.Value,
fontStyle: fontStyle,
x: x + (barWidth >> 1),
y: y,
radians: radians,
offset: series.Label.Offset,
})
}

Expand Down
39 changes: 13 additions & 26 deletions chart_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ChartOption struct {
BarWidth int
// BarHeight is the height of the bars for horizontal bar charts.
BarHeight int
// Children are child charts to render together.
// Children are Child charts to render together.
Children []ChartOption
parent *Painter
// ValueFormatter to format numeric values into labels.
Expand All @@ -57,18 +57,18 @@ type ChartOption struct {
// OptionFunc option function
type OptionFunc func(opt *ChartOption)

// SVGOutputOption set svg type of chart's output.
func SVGOutputOption() OptionFunc {
return OutputFormatOptionFunc(ChartOutputSVG)
// SVGOutputOptionFunc set svg type of chart's output.
func SVGOutputOptionFunc() OptionFunc {
return outputFormatOptionFunc(ChartOutputSVG)
}

// PNGOutputOption set png type of chart's output.
func PNGOutputOption() OptionFunc {
return OutputFormatOptionFunc(ChartOutputPNG)
// PNGOutputOptionFunc set png type of chart's output.
func PNGOutputOptionFunc() OptionFunc {
return outputFormatOptionFunc(ChartOutputPNG)
}

// OutputFormatOptionFunc set type of chart's output.
func OutputFormatOptionFunc(t string) OptionFunc {
// outputFormatOptionFunc set type of chart's output.
func outputFormatOptionFunc(t string) OptionFunc {
return func(opt *ChartOption) {
opt.OutputFormat = t
}
Expand Down Expand Up @@ -162,16 +162,10 @@ func YAxisDataOptionFunc(data []string) OptionFunc {
}
}

// WidthOptionFunc set width of chart
func WidthOptionFunc(width int) OptionFunc {
// DimensionsOptionFunc sets the width and height dimensions of the chart.
func DimensionsOptionFunc(width, height int) OptionFunc {
return func(opt *ChartOption) {
opt.Width = width
}
}

// HeightOptionFunc set height of chart
func HeightOptionFunc(height int) OptionFunc {
return func(opt *ChartOption) {
opt.Height = height
}
}
Expand All @@ -183,14 +177,7 @@ func PaddingOptionFunc(padding Box) OptionFunc {
}
}

// BoxOptionFunc set box of chart
func BoxOptionFunc(box Box) OptionFunc {
return func(opt *ChartOption) {
opt.Box = box
}
}

// PieSeriesShowLabel set pie series show label
// PieSeriesShowLabel set pie series show label.
func PieSeriesShowLabel() OptionFunc {
return func(opt *ChartOption) {
for index := range opt.SeriesList {
Expand All @@ -199,7 +186,7 @@ func PieSeriesShowLabel() OptionFunc {
}
}

// ChildOptionFunc add child chart
// ChildOptionFunc adds a Child chart on top of the current one. Use Padding and Box for positioning.
func ChildOptionFunc(child ...ChartOption) OptionFunc {
return func(opt *ChartOption) {
if opt.Children == nil {
Expand Down
21 changes: 10 additions & 11 deletions chart_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ func TestChartOption(t *testing.T) {
t.Parallel()

fns := []OptionFunc{
SVGOutputOption(),
SVGOutputOptionFunc(),
FontOptionFunc(GetDefaultFont()),
ThemeNameOptionFunc(ThemeVividDark),
TitleTextOptionFunc("title"),
LegendLabelsOptionFunc([]string{"label"}),
XAxisDataOptionFunc([]string{"xaxis"}),
YAxisDataOptionFunc([]string{"yaxis"}),
WidthOptionFunc(800),
HeightOptionFunc(600),
DimensionsOptionFunc(800, 600),
PaddingOptionFunc(Box{
Left: 10,
Top: 10,
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestLineRender(t *testing.T) {
}
p, err := LineRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
TitleTextOptionFunc("Line"),
XAxisDataOptionFunc([]string{
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
Expand All @@ -128,7 +127,7 @@ func TestBarRender(t *testing.T) {
}
p, err := BarRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
XAxisDataOptionFunc([]string{
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
}),
Expand Down Expand Up @@ -165,7 +164,7 @@ func TestHorizontalBarRender(t *testing.T) {
}
p, err := HorizontalBarRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
TitleTextOptionFunc("World Population"),
PaddingOptionFunc(Box{
Top: 20,
Expand All @@ -192,7 +191,7 @@ func TestPieRender(t *testing.T) {
values := []float64{1048, 735, 580, 484, 300}
p, err := PieRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
TitleOptionFunc(TitleOption{
Text: "Rainfall vs Evaporation",
Subtext: "Fake Data",
Expand All @@ -205,7 +204,7 @@ func TestPieRender(t *testing.T) {
Left: 20,
}),
LegendOptionFunc(LegendOption{
Vertical: true,
Vertical: True(),
Data: []string{
"Search Engine", "Direct", "Email", "Union Ads", "Video Ads",
},
Expand All @@ -228,7 +227,7 @@ func TestRadarRender(t *testing.T) {
}
p, err := RadarRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
TitleTextOptionFunc("Basic Radar Chart"),
LegendLabelsOptionFunc([]string{
"Allocated Budget", "Actual Spending",
Expand Down Expand Up @@ -258,7 +257,7 @@ func TestFunnelRender(t *testing.T) {
}
p, err := FunnelRender(
values,
SVGOutputOption(),
SVGOutputOptionFunc(),
TitleTextOptionFunc("Funnel"),
LegendLabelsOptionFunc([]string{
"Show", "Click", "Visit", "Inquiry", "Order",
Expand All @@ -277,7 +276,7 @@ func TestChildRender(t *testing.T) {
{150, 232, 201, 154, 190, 330, 410},
{320, 332, 301, 334, 390, 330, 320},
},
SVGOutputOption(),
SVGOutputOptionFunc(),
XAxisDataOptionFunc([]string{
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
}),
Expand Down
Loading

0 comments on commit 40756d5

Please sign in to comment.