Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BarChart RoundedBarCaps option #11

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type AxisOption struct {

func (a *axisPainter) Render() (Box, error) {
opt := a.opt
if isFalse(opt.Show) {
if flagIs(false, opt.Show) {
return BoxZero, nil
}
top := a.p
Expand Down Expand Up @@ -99,7 +99,7 @@ func (a *axisPainter) Render() (Box, error) {
}
dataCount := len(opt.Data)

centerLabels := !isFalse(opt.BoundaryGap)
centerLabels := !flagIs(false, opt.BoundaryGap)
isVertical := opt.Position == PositionLeft || opt.Position == PositionRight

// if less than zero, it means not processing
Expand Down
25 changes: 24 additions & 1 deletion bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type BarChartOption struct {
Legend LegendOption
// BarWidth specifies the width of each bar.
BarWidth int
// RoundedBarCaps set to `true` to produce a bar graph where the bars have rounded tops.
RoundedBarCaps *bool
}

func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
Expand Down Expand Up @@ -115,7 +117,28 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B

seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
}).Rect(chart.Box{
})
if flagIs(true, opt.RoundedBarCaps) {
radius := .5 * float64(barWidth)
top += int(radius)
seriesPainter.Circle(radius, x+int(radius), top)
if top+int(radius) > barMaxHeight {
// hide the part of the circle below the axis line
seriesPainter.OverrideDrawingStyle(Style{
FillColor: opt.Theme.GetBackgroundColor(),
}).Rect(chart.Box{
Top: barMaxHeight,
Left: x,
Right: x + barWidth,
Bottom: top + int(radius),
})
seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
})
top = barMaxHeight
}
}
seriesPainter.Rect(chart.Box{
Top: top,
Left: x,
Right: x + barWidth,
Expand Down
14 changes: 12 additions & 2 deletions bar_chart_test.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func NewLegendPainter(p *Painter, opt LegendOption) *legendPainter {

func (l *legendPainter) Render() (Box, error) {
opt := l.opt
if opt.IsEmpty() ||
isFalse(opt.Show) {
if opt.IsEmpty() || flagIs(false, opt.Show) {
return BoxZero, nil
}

Expand Down
4 changes: 2 additions & 2 deletions line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
opt := l.opt
seriesPainter := result.seriesPainter

boundaryGap := !isFalse(opt.XAxis.BoundaryGap)
boundaryGap := !flagIs(false, opt.XAxis.BoundaryGap)
xDivideCount := len(opt.XAxis.Data)
if boundaryGap && xDivideCount > 1 && seriesPainter.Width()/xDivideCount <= 10 {
// boundary gap would be so small it's visually better to disable the line spacing adjustment and just keep
Expand Down Expand Up @@ -164,7 +164,7 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (
}
drawingStyle.StrokeWidth = 1
seriesPainter.SetDrawingStyle(drawingStyle)
if !isFalse(opt.SymbolShow) {
if !flagIs(false, opt.SymbolShow) {
seriesPainter.Dots(points)
}
markPointPainter.Add(markPointRenderOption{
Expand Down
2 changes: 1 addition & 1 deletion title.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewTitlePainter(p *Painter, opt TitleOption) *titlePainter {
func (t *titlePainter) Render() (Box, error) {
opt := t.opt
p := t.p
if isFalse(opt.Show) {
if flagIs(false, opt.Show) {
return BoxZero, nil
}

Expand Down
9 changes: 5 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ func FloatPointer(f float64) *float64 {
return &f
}

func isFalse(flag *bool) bool {
if flag != nil && !*flag {
return true
// flagIs returns true if the flag is not-nil and matches the comparison argument.
func flagIs(is bool, flag *bool) bool {
if flag == nil {
return false
}
return false
return *flag == is
}

func containsInt(values []int, value int) bool {
Expand Down