feat: support bar margin

This commit is contained in:
vicanso 2024-06-07 20:35:03 +08:00
parent 32e6dd52d0
commit e7dc4189d5
4 changed files with 20 additions and 4 deletions

View file

@ -23,6 +23,7 @@
package charts package charts
import ( import (
"fmt"
"math" "math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
@ -63,6 +64,8 @@ type BarChartOption struct {
// The legend option // The legend option
Legend LegendOption Legend LegendOption
BarWidth int BarWidth int
// Margin of bar
BarMargin int
} }
func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) { func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
@ -88,6 +91,10 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
margin = 5 margin = 5
barMargin = 3 barMargin = 3
} }
if opt.BarMargin > 0 {
barMargin = opt.BarMargin
}
fmt.Println(barMargin)
seriesCount := len(seriesList) seriesCount := len(seriesList)
// 总的宽度-两个margin-(总数-1)的barMargin // 总的宽度-两个margin-(总数-1)的barMargin
barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount

View file

@ -67,6 +67,8 @@ type ChartOption struct {
LineStrokeWidth float64 LineStrokeWidth float64
// The bar with of bar chart // The bar with of bar chart
BarWidth int BarWidth int
// The margin of each bar
BarMargin int
// The bar height of horizontal bar chart // The bar height of horizontal bar chart
BarHeight int BarHeight int
// Fill the area of line chart // Fill the area of line chart

View file

@ -379,6 +379,7 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
Font: opt.font, Font: opt.font,
XAxis: opt.XAxis, XAxis: opt.XAxis,
BarWidth: opt.BarWidth, BarWidth: opt.BarWidth,
BarMargin: opt.BarMargin,
}).render(renderResult, barSeriesList) }).render(renderResult, barSeriesList)
return err return err
}) })
@ -391,6 +392,7 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
Theme: opt.theme, Theme: opt.theme,
Font: opt.font, Font: opt.font,
BarHeight: opt.BarHeight, BarHeight: opt.BarHeight,
BarMargin: opt.BarMargin,
YAxisOptions: opt.YAxisOptions, YAxisOptions: opt.YAxisOptions,
}).render(renderResult, horizontalBarSeriesList) }).render(renderResult, horizontalBarSeriesList)
return err return err

View file

@ -50,6 +50,8 @@ type HorizontalBarChartOption struct {
// The legend option // The legend option
Legend LegendOption Legend LegendOption
BarHeight int BarHeight int
// Margin of bar
BarMargin int
} }
// NewHorizontalBarChart returns a horizontal bar chart renderer // NewHorizontalBarChart returns a horizontal bar chart renderer
@ -81,6 +83,9 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
margin = 5 margin = 5
barMargin = 3 barMargin = 3
} }
if opt.BarMargin > 0 {
barMargin = opt.BarMargin
}
seriesCount := len(seriesList) seriesCount := len(seriesList)
// 总的高度-两个margin-(总数-1)的barMargin // 总的高度-两个margin-(总数-1)的barMargin
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount