Fixing background drawing for stacked bar charts

This commit is contained in:
Kenneth Shaw 2024-01-11 03:44:02 +07:00
parent c9c9042154
commit b282c2445d
6 changed files with 21 additions and 7 deletions

View file

@ -8,9 +8,6 @@ import (
)
func main() {
chart.DefaultBackgroundColor = chart.ColorTransparent
chart.DefaultCanvasColor = chart.ColorTransparent
barWidth := 80
var (

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View file

@ -8,9 +8,6 @@ import (
)
func main() {
chart.DefaultBackgroundColor = chart.ColorTransparent
chart.DefaultCanvasColor = chart.ColorTransparent
barWidth := 120
var (

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View file

@ -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())
@ -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 {
@ -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 {