Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
Fixing background drawing for stacked bar charts
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Jan 10, 2024
1 parent c9c9042 commit b282c24
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
3 changes: 0 additions & 3 deletions examples/horizontal_stacked_bar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
)

func main() {
chart.DefaultBackgroundColor = chart.ColorTransparent
chart.DefaultCanvasColor = chart.ColorTransparent

barWidth := 80

var (
Expand Down
Binary file modified examples/horizontal_stacked_bar/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/stacked_bar/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions examples/stacked_bar_labels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
)

func main() {
chart.DefaultBackgroundColor = chart.ColorTransparent
chart.DefaultCanvasColor = chart.ColorTransparent

barWidth := 120

var (
Expand Down
Binary file modified examples/stacked_bar_labels/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion stacked_bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func (sbc StackedBarChart) Render(rp RendererProvider, w io.Writer) error {
}
r.SetDPI(sbc.GetDPI(DefaultDPI))

sbc.drawBackground(r)

var canvasBox Box
if sbc.IsHorizontal {
canvasBox = sbc.getHorizontalAdjustedCanvasBox(r, sbc.getDefaultCanvasBox())
Expand Down Expand Up @@ -150,6 +152,25 @@ func (sbc StackedBarChart) drawBars(r Renderer, canvasBox Box) {
}
}

func (sbc StackedBarChart) drawBackground(r Renderer) {
Draw.Box(r, Box{
Right: sbc.GetWidth(),
Bottom: sbc.GetHeight(),
}, sbc.getBackgroundStyle())
}

func (sbc StackedBarChart) getBackgroundStyle() Style {
return sbc.Background.InheritFrom(sbc.styleDefaultsBackground())
}

func (sbc StackedBarChart) styleDefaultsBackground() Style {
return Style{
FillColor: sbc.GetColorPalette().BackgroundColor(),
StrokeColor: sbc.GetColorPalette().BackgroundStrokeColor(),
StrokeWidth: DefaultStrokeWidth,
}
}

func (sbc StackedBarChart) drawHorizontalBars(r Renderer, canvasBox Box) {
yOffset := canvasBox.Top
for _, bar := range sbc.Bars {
Expand Down Expand Up @@ -471,7 +492,6 @@ func (sbc StackedBarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box) Box {
Right: canvasBox.Left + totalWidth,
Bottom: canvasBox.Bottom,
}

}

func (sbc StackedBarChart) getHorizontalAdjustedCanvasBox(r Renderer, canvasBox Box) Box {
Expand Down

0 comments on commit b282c24

Please sign in to comment.