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

Fixing background drawing for stacked bar charts #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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