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)
  • Loading branch information
jentfoo committed Jan 2, 2025
1 parent fd93500 commit caa5e3e
Show file tree
Hide file tree
Showing 44 changed files with 195 additions and 231 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
16 changes: 5 additions & 11 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
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
14 changes: 7 additions & 7 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 @@ -263,9 +263,9 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
}))
var yAxis *axisPainter
if index == 0 {
yAxis = NewLeftYAxis(child, yAxisOption)
yAxis = newLeftYAxis(child, yAxisOption)
} else {
yAxis = NewRightYAxis(child, yAxisOption)
yAxis = newRightYAxis(child, yAxisOption)
}
if yAxisBox, err := yAxis.Render(); err != nil {
return nil, err
Expand All @@ -276,7 +276,7 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
}
}

xAxis := NewBottomXAxis(p.Child(PainterPaddingOption(Box{
xAxis := newBottomXAxis(p.Child(PainterPaddingOption(Box{
Left: rangeWidthLeft,
Right: rangeWidthRight,
IsSet: true,
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
16 changes: 6 additions & 10 deletions echarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ func (value *EChartsSeriesDataValue) UnmarshalJSON(data []byte) error {
data = convertToArray(data)
return json.Unmarshal(data, &value.values)
}

func (value *EChartsSeriesDataValue) First() float64 {
if len(value.values) == 0 {
return 0
}
return value.values[0]
}
func NewEChartsSeriesDataValue(values ...float64) EChartsSeriesDataValue {
return EChartsSeriesDataValue{
values: values,
}
}

type EChartsSeriesData struct {
Value EChartsSeriesDataValue `json:"value"`
Expand Down Expand Up @@ -417,7 +413,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 Expand Up @@ -482,17 +478,17 @@ func renderEcharts(options, outputType string) ([]byte, error) {
}
opt := o.ToOption()
opt.OutputFormat = outputType
if d, err := Render(opt); err != nil {
if p, err := Render(opt); err != nil {
return nil, err
} else {
return d.Bytes()
return p.Bytes()
}
}

func RenderEChartsToPNG(options string) ([]byte, error) {
return renderEcharts(options, "png")
return renderEcharts(options, ChartOutputPNG)
}

func RenderEChartsToSVG(options string) ([]byte, error) {
return renderEcharts(options, "svg")
return renderEcharts(options, ChartOutputSVG)
}
2 changes: 1 addition & 1 deletion echarts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestEChartsSeriesDataValue(t *testing.T) {
assert.Equal(t, EChartsSeriesDataValue{
values: []float64{1, 2},
}, es)
assert.Equal(t, NewEChartsSeriesDataValue(1, 2), es)
assert.Equal(t, EChartsSeriesDataValue{values: []float64{1, 2}}, es)
assert.Equal(t, 1.0, es.First())
}

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
Loading

0 comments on commit caa5e3e

Please sign in to comment.