Skip to content

Commit

Permalink
Config API updates and improved go docs
Browse files Browse the repository at this point in the history
These changes are designed to improve consistency and simplicity of chart configuration.

General Changes

* Public config struct fields have had their docs updated and improved.
* `Type` has been renamed to `OutputFormat`, this better describes the format that you want the chart rendered to.
* Heavier theme use - color specifications that were already possible to derive from the theme have been removed, instead the theme is now just always used. As a future change we will allow easier theme modifications to more easily allow one off customization.
* `TrueFlag()` and `FlaseFlag()` have been renamed to just `True()` and `False()`
* `NewFloatPoint(float64)` has been renamed to `FloatPointer`
* Fields with `Option` in their name have had the `Option` removed. This makes names more concise, and for `YAxisOptions` renaming to `YAxis` made it more consistent when compared to `XAxis`.

Axis-Specific Changes

* YAxisOptions field renamed to `YAxis` as mentioned above.
* `FirstAxis` has been renamed to the more descriptive `DataStartIndex`
* `StrokeColor` and `SplitLineColor` has been removed (as mentioned above, theme is now used). You can use `WithAxisColor(Color)` to easily change an existing theme to a different axis color. We plan to improve this API further.
* YAxis `Color` has been renamed to the more descriptive `AxisColor`. Depending on how easy we can make theme adjustments this field may be deprecated. It is preferred to modify the theme using `WithAxisColor(Color)`.

Chart-Specific Changes

* Line charts `Opacity` has been renamed to the more descriptive `FillOpacity` (avoid confusion with other shading opacity)
* For Grid and Table charts `Column` and `Row` were made plural into `Columns` and `Rows`
* For Table charts `Padding` was renamed to the more descriptive `CellPadding` (avoid confusion with chart padding).
  • Loading branch information
jentfoo committed Feb 11, 2024
1 parent ff70656 commit 6b74fa0
Show file tree
Hide file tree
Showing 46 changed files with 749 additions and 867 deletions.
16 changes: 8 additions & 8 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ type Point struct {
}

const (
ChartTypeLine = "line"
ChartTypeBar = "bar"
ChartTypePie = "pie"
ChartTypeRadar = "radar"
ChartTypeFunnel = "funnel"
// horizontal bar
ChartTypeLine = "line"
ChartTypeBar = "bar"
ChartTypePie = "pie"
ChartTypeRadar = "radar"
ChartTypeFunnel = "funnel"
ChartTypeHorizontalBar = "horizontalBar"
)

const (
ChartOutputSVG = "svg"
ChartOutputPNG = "png"
ChartOutputSVG = "svg"
ChartOutputPNG = "png"
chartDefaultOutputFormat = ChartOutputPNG
)

const (
Expand Down
93 changes: 32 additions & 61 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,38 @@ func NewAxisPainter(p *Painter, opt AxisOption) *axisPainter {
}

type AxisOption struct {
// The theme of chart
// 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.
Theme ColorPalette
// Formatter for y axis text value
Formatter string
// The label of axis
// Data provides labels for the axis.
Data []string
// The boundary gap on both sides of a coordinate axis.
// Nil or *true means the center part of two axis ticks
BoundaryGap *bool
// The flag for show axis, set this to *false will hide axis
Show *bool
// The position of axis, it can be 'left', 'top', 'right' or 'bottom'
// DataStartIndex specifies what index the Data values should start from.
DataStartIndex int
// Formatter for replacing axis text values.
Formatter string
// Position describes the position of axis, it can be 'left', 'top', 'right' or 'bottom'.
Position string
// The line color of axis
StrokeColor Color
// The line width
// BoundaryGap specifies that the chart should have additional space on the left and right, with data points being
// centered between two axis ticks. Enabled by default, specify *false (through False()) to change the spacing.
BoundaryGap *bool
// StrokeWidth is the axis line width.
StrokeWidth float64
// The length of the axis tick
// TickLength is the length of each axis tick.
TickLength int
// The first axis
FirstAxis int
// The margin value of label
// LabelMargin specifies the margin value of each label.
LabelMargin int
// The font size of label
// FontSize specifies the font size of each label.
FontSize float64
// The font of label
// Font is the font used to render each label.
Font *truetype.Font
// The color of label
// FontColor is the color used for text rendered.
FontColor Color
// The flag for show axis split line, set this to true will show axis split line
// SplitLineShow, set this to true will show axis split line.
SplitLineShow bool
// The color of split line
SplitLineColor Color
// The text rotation of label
// TextRotation are the radians for rotating the label.
TextRotation float64
// The offset of label
// LabelOffset is the offset of each label.
LabelOffset Box
// Unit is a suggestion for how large the axis step is, this is a recommendation only. Larger numbers result in fewer labels.
Unit float64
Expand Down Expand Up @@ -91,10 +87,6 @@ func (a *axisPainter) Render() (Box, error) {
if fontSize == 0 {
fontSize = defaultFontSize
}
strokeColor := opt.StrokeColor
if strokeColor.IsZero() {
strokeColor = theme.GetAxisStrokeColor()
}

formatter := opt.Formatter
if len(formatter) != 0 {
Expand All @@ -112,7 +104,7 @@ func (a *axisPainter) Render() (Box, error) {
labelMargin := getDefaultInt(opt.LabelMargin, 5)

style := Style{
StrokeColor: strokeColor,
StrokeColor: theme.GetAxisStrokeColor(),
StrokeWidth: strokeWidth,
Font: font,
FontColor: fontColor,
Expand Down Expand Up @@ -235,17 +227,11 @@ func (a *axisPainter) Render() (Box, error) {
TickSpaces: tickSpaces,
Length: tickLength,
Orient: orient,
First: opt.FirstAxis,
First: opt.DataStartIndex,
})
p.LineStroke([]Point{
{
X: x0,
Y: y0,
},
{
X: x1,
Y: y1,
},
{X: x0, Y: y0},
{X: x1, Y: y1},
})
}

Expand All @@ -254,7 +240,7 @@ func (a *axisPainter) Render() (Box, error) {
Top: labelPaddingTop,
Right: labelPaddingRight,
})).MultiText(MultiTextOption{
First: opt.FirstAxis,
First: opt.DataStartIndex,
Align: textAlign,
TextList: opt.Data,
Orient: orient,
Expand All @@ -266,7 +252,7 @@ func (a *axisPainter) Render() (Box, error) {
})

if opt.SplitLineShow { // show auxiliary lines
style.StrokeColor = opt.SplitLineColor
style.StrokeColor = theme.GetAxisSplitLineColor()
style.StrokeWidth = 1
top.OverrideDrawingStyle(style)
if isVertical {
Expand All @@ -280,14 +266,8 @@ func (a *axisPainter) Render() (Box, error) {
yValues = yValues[0 : len(yValues)-1]
for _, y := range yValues {
top.LineStroke([]Point{
{
X: x0,
Y: y,
},
{
X: x1,
Y: y,
},
{X: x0, Y: y},
{X: x1, Y: y},
})
}
} else {
Expand All @@ -299,21 +279,12 @@ func (a *axisPainter) Render() (Box, error) {
continue
}
top.LineStroke([]Point{
{
X: x,
Y: y0,
},
{
X: x,
Y: y1,
},
{X: x, Y: y0},
{X: x, Y: y1},
})
}
}
}

return Box{
Bottom: height,
Right: width,
}, nil
return Box{Bottom: height, Right: width}, nil
}
35 changes: 20 additions & 15 deletions axis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func TestAxis(t *testing.T) {
"Sat",
"Sun",
},
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
SplitLineShow: true,
}).Render()
return p.Bytes()
},
Expand All @@ -48,7 +47,7 @@ func TestAxis(t *testing.T) {
"Sat",
"Sun",
},
BoundaryGap: FalseFlag(),
BoundaryGap: False(),
}).Render()
return p.Bytes()
},
Expand Down Expand Up @@ -86,10 +85,9 @@ func TestAxis(t *testing.T) {
"Sat",
"Sun",
},
Position: PositionLeft,
BoundaryGap: FalseFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
Position: PositionLeft,
BoundaryGap: False(),
SplitLineShow: true,
}).Render()
return p.Bytes()
},
Expand All @@ -108,10 +106,9 @@ func TestAxis(t *testing.T) {
"Sat",
"Sun",
},
Position: PositionRight,
BoundaryGap: FalseFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
Position: PositionRight,
BoundaryGap: False(),
SplitLineShow: true,
}).Render()
return p.Bytes()
},
Expand Down Expand Up @@ -139,13 +136,21 @@ func TestAxis(t *testing.T) {
},
}

testThemeName := "axisTestTheme"
InstallTheme(testThemeName, ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 110, G: 112, B: 121, A: 255},
AxisSplitLineColor: drawing.ColorBlack,
BackgroundColor: drawing.ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
})
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
p, err := NewPainter(PainterOptions{
Type: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(GetTheme(ThemeLight)))
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(GetTheme(testThemeName)))
require.NoError(t, err)
data, err := tt.render(p)
require.NoError(t, err)
Expand Down
39 changes: 20 additions & 19 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ func NewBarChart(p *Painter, opt BarChartOption) *barChart {
}

type BarChartOption struct {
// The theme
// Theme specifies the colors used for the bar chart.
Theme ColorPalette
// The font to use
// Padding specifies the padding of bar chart.
Padding Box
// Font is the font used to render the chart.
Font *truetype.Font
// The data series list
// SeriesList provides the data series.
SeriesList SeriesList
// The x-axis options
// XAxis are options for the x-axis.
XAxis XAxisOption
// The padding of line chart
Padding Box
// The y-axis options
YAxisOptions []YAxisOption
// The option of title
// YAxis are options for the y-axis (at most two).
YAxis []YAxisOption
// Title are options for rendering the title.
Title TitleOption
// The legend option
Legend LegendOption
// Legend are options for the data legend.
Legend LegendOption
// BarWidth specifies the width of each bar.
BarWidth int
}

Expand Down Expand Up @@ -78,7 +79,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
}
for index := range seriesList {
series := seriesList[index]
yRange := result.axisRanges[series.AxisIndex]
yRange := result.axisRanges[series.YAxisIndex]
seriesColor := theme.GetSeriesColor(series.index)

divideValues := xRange.AutoDivide()
Expand Down Expand Up @@ -187,13 +188,13 @@ func (b *barChart) Render() (Box, error) {
opt.Theme = getPreferredTheme(p.theme)
}
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxisOptions: opt.YAxisOptions,
TitleOption: opt.Title,
LegendOption: opt.Legend,
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxis: opt.YAxis,
Title: opt.Title,
Legend: opt.Legend,
})
if err != nil {
return BoxZero, err
Expand Down
8 changes: 4 additions & 4 deletions bar_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func makeBasicBarChartOption() BarChartOption {
"Nov",
"Dec",
}),
YAxisOptions: NewYAxisOptions([]string{
YAxis: NewYAxisOptions([]string{
"Rainfall",
"Evaporation",
}),
Expand Down Expand Up @@ -94,9 +94,9 @@ func TestBarChart(t *testing.T) {

for _, tt := range tests {
painterOptions := PainterOptions{
Type: ChartOutputSVG,
Width: 600,
Height: 400,
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
}
if tt.defaultTheme {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 6b74fa0

Please sign in to comment.