From 0a80e7056f69f21d1561f4426877ab7fa376dd4f Mon Sep 17 00:00:00 2001 From: vicanso Date: Wed, 28 Sep 2022 20:29:22 +0800 Subject: [PATCH] feat: support setting bar width for bar chart, #24 --- bar_chart.go | 10 ++++++++-- chart_option.go | 2 ++ charts.go | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/bar_chart.go b/bar_chart.go index 8826ffb..2addd17 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -59,7 +59,8 @@ type BarChartOption struct { // The option of title Title TitleOption // The legend option - Legend LegendOption + Legend LegendOption + BarWidth int } func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) { @@ -86,7 +87,12 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B } seriesCount := len(seriesList) // 总的宽度-两个margin-(总数-1)的barMargin - barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / len(seriesList) + barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount + if opt.BarWidth > 0 && opt.BarWidth < barWidth { + barWidth = opt.BarWidth + // 重新计算margin + margin = (width - len(seriesList)*barWidth - barMargin*(seriesCount-1)) / 2 + } barMaxHeight := seriesPainter.Height() theme := opt.Theme seriesNames := seriesList.Names() diff --git a/chart_option.go b/chart_option.go index 93b81ba..447ef52 100644 --- a/chart_option.go +++ b/chart_option.go @@ -66,6 +66,8 @@ type ChartOption struct { SymbolShow *bool // The stroke width of line chart LineStrokeWidth float64 + // The bar with of bar chart + BarWidth int // Fill the area of line chart FillArea bool // The child charts diff --git a/charts.go b/charts.go index f7e52e4..f8c94a3 100644 --- a/charts.go +++ b/charts.go @@ -354,9 +354,10 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) { if len(barSeriesList) != 0 { handler.Add(func() error { _, err := NewBarChart(p, BarChartOption{ - Theme: opt.theme, - Font: opt.font, - XAxis: opt.XAxis, + Theme: opt.theme, + Font: opt.font, + XAxis: opt.XAxis, + BarWidth: opt.BarWidth, }).render(renderResult, barSeriesList) return err })