Skip to content

Commit

Permalink
Deprecate and replace Painter.SetBackground
Browse files Browse the repository at this point in the history
`SetBackground` is being replaced with `FilledRect` which provides the same functionality plus the ability to specify any position for the box (as well as an optional stroke).
  • Loading branch information
jentfoo committed Jan 22, 2025
1 parent f36f629 commit 028fc3f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 52 deletions.
9 changes: 2 additions & 7 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,8 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
}, barWidth, true, false,
seriesColor, seriesColor, 0.0)
} else {
seriesPainter.filledRect(Box{
Top: top,
Left: x,
Right: x + barWidth,
Bottom: barMaxHeight - 1,
IsSet: true,
}, seriesColor, seriesColor, 0.0)
seriesPainter.FilledRect(x, top, x+barWidth, barMaxHeight-1,
seriesColor, seriesColor, 0.0)
}
// generate marker point by hand
points[j] = Point{
Expand Down
4 changes: 2 additions & 2 deletions charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
seriesList := opt.seriesList
seriesList.init()
if !opt.backgroundIsFilled {
p.SetBackground(p.Width(), p.Height(), opt.theme.GetBackgroundColor())
p.drawBackground(opt.theme.GetBackgroundColor())
}
if !opt.padding.IsZero() {
p = p.Child(PainterPaddingOption(opt.padding))
Expand Down Expand Up @@ -338,7 +338,7 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
p = p.Child(PainterBoxOption(opt.Box))
}
if !isChild {
p.SetBackground(p.Width(), p.Height(), opt.Theme.GetBackgroundColor())
p.drawBackground(opt.Theme.GetBackgroundColor())
}
seriesList := opt.SeriesList
seriesList.init()
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple_charts-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
Width: 800,
Height: 600,
})
p.SetBackground(800, 600, drawing.ColorWhite)
p.FilledRect(0, 0, 800, 600, drawing.ColorWhite, drawing.ColorWhite, 0.0)
// set the space and theme for each chart
topCenterPainter := p.Child(charts.PainterBoxOption(chartdraw.NewBox(0, 0, 800, 300)),
charts.PainterThemeOption(charts.GetTheme(charts.ThemeVividLight)))
Expand Down
10 changes: 1 addition & 9 deletions horizontal_bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"

"github.com/golang/freetype/truetype"

"github.com/go-analyze/charts/chartdraw"
)

type horizontalBarChart struct {
Expand Down Expand Up @@ -118,13 +116,7 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
w := xRange.getHeight(item)
fillColor := seriesColor
right := w
seriesPainter.filledRect(chartdraw.Box{
Top: y,
Left: 0,
Right: right,
Bottom: y + barHeight,
IsSet: true,
}, fillColor, fillColor, 0.0)
seriesPainter.FilledRect(0, y, right, y+barHeight, fillColor, fillColor, 0.0)
// if the label does not need to be displayed, return
if labelPainter == nil {
continue
Expand Down
8 changes: 1 addition & 7 deletions legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,7 @@ func (l *legendPainter) Render() (Box, error) {
var drawIcon func(top, left int, color Color) int
if opt.Icon == IconRect {
drawIcon = func(top, left int, color Color) int {
p.filledRect(Box{
Top: top - legendHeight + 8,
Left: left,
Right: left + legendWidth,
Bottom: top + 1,
IsSet: true,
}, color, color, 0)
p.FilledRect(left, top-legendHeight+8, left+legendWidth, top+1, color, color, 0)
return left + legendWidth
}
} else {
Expand Down
44 changes: 24 additions & 20 deletions painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ func (p *Painter) fill(fillColor Color) {
func (p *Painter) fillStroke(fillColor, strokeColor Color, strokeWidth float64) {
defer p.render.ResetStyle()
p.render.SetFillColor(fillColor)
p.render.SetStrokeColor(strokeColor)
p.render.SetStrokeWidth(strokeWidth)
p.render.FillStroke()
if strokeWidth > 0 && !strokeColor.IsTransparent() {
p.render.SetStrokeColor(strokeColor)
p.render.SetStrokeWidth(strokeWidth)
p.render.FillStroke()
} else {
p.render.Fill()
}
}

// Width returns the drawable width of the painter's box.
Expand Down Expand Up @@ -375,14 +379,24 @@ func (p *Painter) drawSmoothCurve(points []Point, tension float64, dotForSingleP
p.quadCurveTo(points[n-2].X, points[n-2].Y, points[n-1].X, points[n-1].Y)
}

// SetBackground fills the entire painter area with the given color.
// Deprecated: SetBackground is Deprecated, use FilledRect with zero for the stroke width.
func (p *Painter) SetBackground(width, height int, color Color) {
p.moveTo(0, 0)
p.lineTo(width, 0)
p.lineTo(width, height)
p.lineTo(0, height)
p.lineTo(0, 0)
p.fill(color)
p.FilledRect(0, 0, width, height, color, color, 0.0)
}

// drawBackground fills the entire painter area with the given color.
func (p *Painter) drawBackground(color Color) {
p.FilledRect(0, 0, p.Width(), p.Height(), color, color, 0.0)
}

// FilledRect will draw a filled box with the given coordinates.
func (p *Painter) FilledRect(x1, y1, x2, y2 int, fillColor, strokeColor Color, strokeWidth float64) {
p.moveTo(x1, y1)
p.lineTo(x2, y1)
p.lineTo(x2, y2)
p.lineTo(x1, y2)
p.lineTo(x1, y1)
p.fillStroke(fillColor, strokeColor, strokeWidth)
}

// MarkLine draws a horizontal line with a small circle and arrow at the right.
Expand Down Expand Up @@ -870,16 +884,6 @@ func (p *Painter) Dots(points []Point, fillColor, strokeColor Color, strokeWidth
p.render.FillStroke()
}

// filledRect will draw a filled box with the given coordinates.
func (p *Painter) filledRect(box Box, fillColor, strokeColor Color, strokeWidth float64) {
p.moveTo(box.Left, box.Top)
p.lineTo(box.Right, box.Top)
p.lineTo(box.Right, box.Bottom)
p.lineTo(box.Left, box.Bottom)
p.lineTo(box.Left, box.Top)
p.fillStroke(fillColor, strokeColor, strokeWidth)
}

// roundedRect is similar to filledRect except the top and bottom will be rounded.
func (p *Painter) roundedRect(box Box, radius int, roundTop, roundBottom bool,
fillColor, strokeColor Color, strokeWidth float64) {
Expand Down
2 changes: 1 addition & 1 deletion painter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func TestMultipleChartsOnPainter(t *testing.T) {
Width: 800,
Height: 600,
})
p.SetBackground(800, 600, drawing.ColorWhite)
p.FilledRect(0, 0, 800, 600, drawing.ColorWhite, drawing.ColorTransparent, 0.0)
// set the space and theme for each chart
topCenterPainter := p.Child(PainterBoxOption(chartdraw.NewBox(0, 0, 800, 300)),
PainterThemeOption(GetTheme(ThemeVividLight)))
Expand Down
10 changes: 5 additions & 5 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
}
opt := t.opt
if !opt.BackgroundColor.IsZero() {
p.SetBackground(p.Width(), p.Height(), opt.BackgroundColor)
p.drawBackground(opt.BackgroundColor)
}

if opt.HeaderBackgroundColor.IsZero() {
Expand All @@ -313,7 +313,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
opt.HeaderBackgroundColor = tableLightThemeSetting.headerColor
}
}
p.SetBackground(info.width, info.headerHeight, opt.HeaderBackgroundColor)
p.FilledRect(0, 0, info.width, info.headerHeight, opt.HeaderBackgroundColor, drawing.ColorTransparent, 0.0)

if opt.RowBackgroundColors == nil {
if opt.Theme.IsDark() {
Expand All @@ -330,7 +330,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
Top: currentHeight,
IsSet: true,
}))
child.SetBackground(p.Width(), h, color)
child.FilledRect(0, 0, p.Width(), h, color, color, 0.0)
currentHeight += h
}
// adjust the background color according to the set table style
Expand All @@ -355,7 +355,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
}))
w := info.columnWidths[j] - padding.Left - padding.Top
h := heights[i] - padding.Top - padding.Bottom
child.SetBackground(w, h, tc.FillColor)
child.FilledRect(0, 0, w, h, tc.FillColor, tc.FillColor, 0.0)
}
left += info.columnWidths[j]
}
Expand All @@ -376,7 +376,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
func (t *tableChart) Render() (Box, error) {
p := t.p
if !t.opt.BackgroundColor.IsZero() {
p.SetBackground(p.Width(), p.Height(), t.opt.BackgroundColor)
p.drawBackground(t.opt.BackgroundColor)
}

r := p.render
Expand Down

0 comments on commit 028fc3f

Please sign in to comment.